| | |
Super Newb Troubleshooting
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 0
Hi all,
I'm supposed to write a small program that asks the user for a number of a month( 1= Jan, 2=Feb, etc) and then outputs the month name and the month number. If the number entered is invalid (for example, > 12 or equal 0), then the user receives an error message. Perhaps there is a better way to write this, but based on my limited knowledge, this is what I have put together. However, when I run the program it prints ALL the months of the year, and then the single number input, like this:
Give me a number
10
January10
February 10
March10
April10
May10
June10
July10
August10
September10
Can anyone tell me what I've done incorrectly?
Here is the code:
I'm supposed to write a small program that asks the user for a number of a month( 1= Jan, 2=Feb, etc) and then outputs the month name and the month number. If the number entered is invalid (for example, > 12 or equal 0), then the user receives an error message. Perhaps there is a better way to write this, but based on my limited knowledge, this is what I have put together. However, when I run the program it prints ALL the months of the year, and then the single number input, like this:
Give me a number
10
January10
February 10
March10
April10
May10
June10
July10
August10
September10
Can anyone tell me what I've done incorrectly?
Here is the code:
Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class Exercise3 { public static void main (String [] args) { Scanner scanner = new Scanner(System.in); { System.out.println("Give me a number"); } int x = scanner.nextInt(); while(x == 1); { System.out.println("January" + x); } while(x == 2); { System.out.println("February " + x); } while(x == 3); { System.out.println("March" + x); } while(x == 4); { System.out.println("April" + x); } while(x == 5); { System.out.println("May" + x); } while(x == 6); { System.out.println("June" + x); } while(x == 7); { System.out.println("July" + x); } while(x == 8); { System.out.println("August" + x); } while(x == 9); { System.out.println("September" + x); } while(x == 10); { System.out.println("October" + x); } while(x == 11); { System.out.println("November" + x); } while(x == 12); { System.out.println("December" + x); } while ((x > 12) || (x == 0)); { System.out.println("Error. Number must be 12 or less"); } } }
•
•
Join Date: Oct 2009
Posts: 92
Reputation:
Solved Threads: 8
0
#2 Nov 9th, 2009
instead of while, use "if"
Java Syntax (Toggle Plain Text)
if(x == 12) { System.out.println("December " + x);//put a space after the month also. }
•
•
Join Date: Oct 2009
Posts: 92
Reputation:
Solved Threads: 8
0
#4 Nov 9th, 2009
Without giving too much away...
I would also test for invalid numbers first before testing for the correct months.
Java Syntax (Toggle Plain Text)
if(x==1) { //January } else if (x==2) { //February }
I would also test for invalid numbers first before testing for the correct months.
•
•
Join Date: Oct 2009
Posts: 92
Reputation:
Solved Threads: 8
0
#5 Nov 9th, 2009
...notice there are no semi-colons after my if statements
Java Syntax (Toggle Plain Text)
if(x==1) { System.out.println("January " + x ); } else ...
![]() |
Similar Threads
- Scan a pixel for a color in C++? (C++)
- Please Help I am gettin Lvalue required error however I cant fix it? (C)
- Computer is super slow (Troubleshooting Dead Machines)
- What super power would you most likely want? (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: java
- Next Thread: Consolidate Accounts - Java
Views: 209 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle os pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






