Results 1 to 5 of 5
Thread: problems with logical and
- 10-15-2012, 11:02 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
problems with logical and
I am working on an assignment for my high school java class and i want to make sure that the user enters a y or n to determine their marital status however i get stuck in the loop even when i enter a y or n
Java Code:import java.util.Scanner; public class Taxes { static Scanner scan = new Scanner(System.in); static String store; public static void main(String[] args) { System.out.println("are you married?(y/n)"); store = scan.nextLine(); for( ; store != "y" && store!= "n"; ){ System.out.println("please respond with a y for yes and n for no"); System.out.println("are you married?(y/n)"); store = scan.nextLine(); }
- 10-15-2012, 11:40 PM #2
Member
- Join Date
- Sep 2012
- Location
- Maryland, USA
- Posts
- 14
- Rep Power
- 0
Re: problems with logical and
I can't remember ever having seen a for loop used in this way. While it may work, there is probably another type of loop that would work better and be easier to understand. Also, a logical AND is not what you want to use in this case.
- 10-16-2012, 01:34 AM #3
Re: problems with logical and
I think using a while loop is a better option for what you want to do. Also to compare strings you need to probably do something like !(store.equals("y")) && !(store.equals("n"). Or try changing your variable to a char variable for the "y" and "n".
Sincerely, Joshua Green
Please REP if I help :)
- 10-16-2012, 02:25 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
- 10-16-2012, 02:43 AM #5
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Similar Threads
-
Logical Operators
By Everyman in forum New To JavaReplies: 9Last Post: 03-26-2012, 03:56 PM -
>>> and >> Logical operators
By Playagood in forum New To JavaReplies: 3Last Post: 02-19-2012, 04:07 AM -
Some Logical problem
By MuslimCoder in forum New To JavaReplies: 4Last Post: 03-01-2010, 08:12 AM -
Logical Gates
By lingz89 in forum New To JavaReplies: 1Last Post: 08-17-2009, 01:11 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks