Results 1 to 7 of 7
Thread: Help with Homework Problem
- 10-05-2010, 03:44 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
Help with Homework Problem
So I am running into a couple problems.
Here's part of my specs:
Now, you are to first prompt the user for his/her name and their classification (numeric) (1 = Freshman, 2 = Sophomore, …, 5 Graduate Student). You are to then print out his/her name and classification as follows:
This output is produced by Joe Schmuck (please use your name), who is a junior at UNF.
Note, JoeSchmuck is a String input and requires no conversion. ‘3’ is also a character that is inputted, but your Scanner object will take care of this for you.
Once this line is printed on the screen, you are to skip two lines (two blank lines). Then you are to prompt the user (me, again) for a number between 1 and 12.
Bad inputs: If the number the user submits does not lie between 1 and 12, you are to prompt the user to try again, such as providing the message, “Try Again. Enter a number between 1 and 12 inclusive and press Enter” and allow the misguided person (probably me again!) to re-enter another number. (This will require a loop). If the user fails to submit a number between 1 and 12 with three tries (total, including the first attempt and two repeats) then you are to display a message, “Get a Life! Program Terminated” and terminate your program. (Be certain to test this option. I will.)
So far I have most of the code done, However I'm not sure how to receive the users name and class # and put together.
Also I can't figure out how to get the part "Get A life..etc." to show before the program terminates.
- 10-05-2010, 03:45 AM #2
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
Here is my code so far:
Java Code:package project2; import java.util.Scanner; /** * * @author at */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int day = 0; int count = 0; while ((day < 1 || day > 12) && count < 3) { //start while count++; System.out.print("Enter number: "); day = keyboard.nextInt(); if (day > 12 || day < 1) {//start if System.out.print("Please Try Again \n"); }//end if else {//start else System.out.print("On the "); switch(day) { case 1: System.out.print("1st"); break; case 2: System.out.print("2nd"); break; case 3: System.out.print("3rd"); break; default: System.out.print(day+"th"); } System.out.println(" day of Christmas my true love gave to me,"); switch(day) { //start switch case 12: System.out.println("Twelve Drummers Drumming"); case 11: System.out.println("Eleven Pipers Piping" ); case 10: System.out.println("Ten Lords A-leaping" ); case 9: System.out.println("Nine Ladies Dancing" ); case 8: System.out.println("Eight Maids A-milking" ); case 7: System.out.println("Seven Swans A-swimming"); case 6: System.out.println("Six Geese A-laying" ); case 5: System.out.println("Five Golden Rings" ); case 4: System.out.println("Four Calling Birds" ); case 3: System.out.println("Three French Hens" ); case 2: System.out.println("Two Turtle Doves, and" ); case 1: System.out.println("A partridge in a pear tree"); } //end switch }//end else }//end while }//end main }//end classLast edited by Holy_Zombies; 10-05-2010 at 03:54 AM.
-
edit: the Scanner object, keyboard, will have methods that you can use to get user input. One, nextLine() will get the next line that the user enters. There's another one that I'll let you look up for getting int input (the number 3 for instance).
Hello and welcome to the java-forums!Also I can't figure out how to get the part "Get A life..etc." to show before the program terminates.
Let's see what you've tried so far in your attempt to solve these two problems above and also let us know just how you're stuck or what isn't working. The more we know the better we'll be able to help you. Also, if posting code, please read my first signature link on how to use code tags so that your code will retain its formatting and be readable.
Best of luck!
edit: I now see your code post and have taken the liberty of adding code tags, but if you pardon my being blunt, your formatting is bad and even with code tags your code is very hard to read. It is in your best interest and ours for you to correct the mal-indentation so that it conforms to standards. If your indentation is proper, then your code won't need all those distracting comments such as // start of if and // end of if... So get rid of them.
To do your little statement if count is > 3, use your if block that you already have in place.
Again, luck!Last edited by Fubarable; 10-05-2010 at 03:56 AM.
- 10-05-2010, 03:59 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
those //start things are just for me to keep track of things right now. im VERY new to java.
So how to I add a condition to the if for count >3 ? does it go with the day<1 >12 part?Last edited by Holy_Zombies; 10-05-2010 at 04:02 AM.
- 10-05-2010, 04:23 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
okay I made a little progress. Now it will loop 3 times and kick you with the correct message.
Still cant get the name part right.Java Code:package project2aaronturner; import java.util.Scanner; /** * * */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); Scanner input = new Scanner( System.in ); int day = 0; int count = 0; System.out.print("Enter name: "); String theName = input.nextLine(); // System.out.print("Enter year level: "); // ENTER YEAR LEVEL System.out.println(theName); // DISPLAY NAME + YEAR LEVEL while ((day < 1 || day > 12) && count < 3) { //start while count++; System.out.print("Enter number: "); day = keyboard.nextInt(); if ((day < 1 || day > 12) && count < 3) {//start if (day < 12) && count < 3 System.out.print("Try Again. Enter a number between 1 and 12 inclusive" + " and press Enter \n\n"); }//end if (day < 12) && count < 3 else if (day > 12) { System.out.print("Get a Life! Program Terminated \n\n"); } else {//start else (day >= 12) System.out.print("On the "); switch(day) { case 1: System.out.print("1st"); break; case 2: System.out.print("2nd"); break; case 3: System.out.print("3rd"); break; default: System.out.print(day+"th"); } System.out.println(" day of Christmas my true love gave to me,"); switch(day) { //start switch case 12: System.out.println("Twelve Drummers Drumming"); case 11: System.out.println("Eleven Pipers Piping" ); case 10: System.out.println("Ten Lords A-leaping" ); case 9: System.out.println("Nine Ladies Dancing" ); case 8: System.out.println("Eight Maids A-milking" ); case 7: System.out.println("Seven Swans A-swimming"); case 6: System.out.println("Six Geese A-laying" ); case 5: System.out.println("Five Golden Rings" ); case 4: System.out.println("Four Calling Birds" ); case 3: System.out.println("Three French Hens" ); case 2: System.out.println("Two Turtle Doves, and" ); case 1: System.out.println("A partridge in a pear tree"); } //end switch }//end else (day >= 12) }//end while }// }
-
1) you need just one Scanner object, not two. You just need to learn to use it. Again, use the method I gave you above plus look up the other method. Also check out the tutorial here: Scanning
2) Again, please please please correct your indentation as your code formatting is atrocious. If you want to encourage folks to read your code and help you, you should make that code easy to read.
- 10-05-2010, 06:26 AM #7
If you want to see how indentation should be done "properly," have a look at this. It will help you organize your code in such a way that it is readable by everyone, and you will NEVER have to guess whether or not a specific line of code is with a particular if/while/for/etc. block--you will just know.
Similar Threads
-
Homework help please
By chick in forum New To JavaReplies: 22Last Post: 03-19-2010, 07:39 AM -
Weird problem with my homework, statements out of order?
By mxrider in forum New To JavaReplies: 6Last Post: 02-20-2010, 03:27 AM -
Please Help with Homework
By theuser in forum Advanced JavaReplies: 2Last Post: 07-30-2009, 03:37 PM -
Problem using thread +rmi in my homework
By IbrahimAbbas in forum Threads and SynchronizationReplies: 10Last Post: 04-14-2008, 09:24 PM -
Homework PREREQUISITE Problem
By ChrisC in forum New To JavaReplies: 7Last Post: 11-27-2007, 05:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks