Results 1 to 4 of 4
Thread: Help would be apprecited!
- 12-04-2011, 03:10 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 26
- Rep Power
- 0
Help would be apprecited!
How do I make it so that at the end of this programme you have the option to go again
I've cut out some of the code to make it shorter, wont effect answers.. Help please!Java Code:import java.util.Scanner; public class Mandatory { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner keyboard = new Scanner(System.in); int Mouth, Nose = 0, Eyes,Hair = 0; String MouthString, NoseString = null, EyesString = null, HairString = null; System.out.println("Automated Sketch Artist:\n\tPlease make your selections based on the numbers with each \"Style\": "); do { System.out.println("\nHairstyle = 1 Bald, 2 Crew-cut, 3 Curly, 4 Wearing a hat? "); while (! keyboard.hasNextInt()) { System.out.print("Invalid - Try again. Please enter the number which goes with your hairstyle selection. "); keyboard.nextLine(); } Hair = keyboard.nextInt(); keyboard.nextLine(); } while (Hair <= 0 || Hair >4); switch (Hair) { case 1: HairString = ""; break; case 2: HairString = "----------\n----------"; break; case 3: HairString = "??????????"; break; case 4: HairString = " ______\n/______\\"; break;} do { System.out.println("Eyes = 1 Beedy, 2 Cross-eyed, 3 Glasses, 4 closed? "); while (! keyboard.hasNextInt()) { System.out.print("Invalid - Try again. Please enter the number which goes with your eyes selection. "); keyboard.nextLine(); } Eyes = keyboard.nextInt(); keyboard.nextLine(); } while (Eyes <= 0 || Eyes >4); switch (Eyes) { case 1: EyesString = " . . "; break; case 2: EyesString = "(>) (<) "; break; case 3: EyesString = " __ __\n| |-| |\n -- -- "; break; case 4: EyesString = " __ __"; break;} System.out.println(HairString); System.out.println(); System.out.println(EyesString); System.out.println(); System.out.println(NoseString); System.out.println(MouthString); System.out.println(); } }
- 12-04-2011, 03:35 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Help would be apprecited!
I've removed the duplicate of this thread - I hope nothing is lost. If so, just say. Your thread didn't show up at once because it was "awaiting moderation". (No, I have no idea why some threads are held up like this. For some reason you're put on the "group W bench" - maybe you have unpaid parking fines or something...)
Yes, that's easy.How do I make it so that at the end of this programme you have the option to go again
The point is that main() is intended to do one thing only: call another method over and over again. You could apply this principle of breaking things up into multiple methods to the rest of your program. For instance you have a hairstyle do-while loop that goes round and round until it gets an int from 1->4. Why not have a separate method that does this and returns the (validated) value that was input? If methods do as little as possible it's easier to see what's happening.Java Code:public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String resp; do { run(); System.out.println("Again?"); resp = keyboard.nextLine(); } while(resp.equalsIgnoreCase("Y")); } private static void run() { // your code here }
- 12-04-2011, 04:00 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 26
- Rep Power
- 0
Re: Help would be apprecited!
Thanks for the reply and sorry for the duplicate.. I just assumed I didn't post it right when it didn't show up right away.
That has worked perfectly for what I needed!
- 12-04-2011, 04:07 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks