所以这是我的主要课程。
import java.util.Scanner;
import java.util.Random;
public class seu03
{
public static void main (String[] args)
{
int option = 0;
option = seu03Methods.seu03Menu();
while(option != 9)
{
switch (option)
{
case 1:
seu02.calculator();
break;
case 2:
seu03Methods.sentenceAnalysis();
break;
case 3:
seu03Methods.rollDice();
break;
case 4:
seu03Methods.bigMoney();
break;
case 9:
System.out.println("Exiting seu03...");
System.exit(0);
break;
default:
System.out.println("Invalid selection.");
break;
}
}
} //end of main
} //end of class
这就是“方法”类。
import java.util.Scanner;
import java.util.Random;
public class seu03Methods
{
public static int seu03Menu()
{
int option = 0;
Scanner console = new Scanner(System.in);
System.out.println("-------------------------------------");
System.out.println("| Welcome to seu03! |");
System.out.println("|-----------------------------------|");
System.out.println("| Choose an option below: |");
System.out.println("| |");
System.out.println("| 1. Calculator |");
System.out.println("| 2. Sentence Analysis |");
System.out.println("| 3. Roll a 6-sided die |");
System.out.println("| 4. Play Big Money! |");
//System.out.println("| 5. randomRange |");
//System.out.println("| 6. stubMethod |");
System.out.println("| 9. Exit |");
System.out.println("-------------------------------------");
option = console.nextInt();
return option;
}
public static void sentenceAnalysis()
{
Scanner console = new Scanner(System.in);
String userString = new String("");
System.out.println("-------------------------------------");
System.out.println("Please enter a sentence for analysis:");
userString = console.nextLine();
System.out.println("The sentence to analyze is: " + userString);
System.out.println("The sentence is " + userString.length() + " characters long.");
System.out.println("The first character of the sentence is " + userString.charAt(0));
System.out.println("The middle charchter of the sentence is " + userString.charAt(userString.length()/2));
System.out.println("The last character of the sentence is " + userString.charAt(userString.length()-1));
System.out.println("Thanks for using our 1323 System!");
System.out.println("-------------------------------------");
return;
} //end of sentenceAnalysis
public static void rollDice()
{
Random gener = new Random();
int die = 0;
System.out.println("-------------------------------------");
System.out.println("Welcome to Roll Dice!");
System.out.println("The program will now roll a single six-sided die.");
die = gener.nextInt(6) + 1;
System.out.println("Result: " + die);
System.out.println("Thanks for using our 1323 System!");
System.out.println("-------------------------------------");
return;
} //end of rollDice
public static void bigMoney()
{
Scanner console = new Scanner(System.in);
Random gener = new Random();
int rnum = 0;
int guess1 = 0;
int guess2 = 0;
int guess3 = 0;
System.out.println("-------------------------------------");
System.out.println("Welcome to Big Money!");
rnum = gener.nextInt(10) + 1;
System.out.println("A whole number between 1 and 10 inclusive has been generated. You will have three chances to guess the number.");
System.out.println("Here is the random number for testing purposes: " + rnum);
System.out.println("What is your first guess?");
guess1 = console.nextInt();
if(guess1 == rnum)
{
System.out.println(" Big Money! You win $100!");
}
else
{
System.out.println("No good! What is your second guess?");
guess2 = console.nextInt();
if(guess2 == rnum)
{
System.out.println("Medium Money! You win $50!");
}
else
{
System.out.println("No good! What is your third guess?");
guess3 = console.nextInt();
if(guess3 == rnum)
{
System.out.println("Little Money! You win $1!");
}
else
{
System.out.println("Sorry! You didn't win any money this time. Better luck next time!");
}
}
}
System.out.println("Thanks for playing Big Money!");
System.out.println("Thanks for using our 1323 System!");
System.out.println("-------------------------------------");
return;
} //end of bigMoney
} //end of class
如果我选择一个选项,比如3,该选项将永远重复(直到我关闭命令行)。我星期二错过了课程(感谢你没有发出警报!)因此错过了关于循环的讲座。 :/如果有人可以提供一些帮助(可能是一些指示?),将不胜感激!我想要的是选择一个选项,按照该方法执行操作,然后返回菜单选择另一种方法,直到用户选择“9”并退出。而且如果以后我也需要摆脱那种可怕的嵌套。
答案 0 :(得分:2)
将option = seu03Methods.seu03Menu();
移动到while循环中(向下移动两行到while(option != 9){..
。)您永远不会在循环中更改option
,因此它始终为3
答案 1 :(得分:0)
while(option != 9)
{
switch (option)
{
case 1:
seu02.calculator();
break;
case 2:
seu03Methods.sentenceAnalysis();
break;
case 3:
seu03Methods.rollDice();
break;
case 4:
seu03Methods.bigMoney();
break;
case 9:
System.out.println("Exiting seu03...");
System.exit(0);
break;
default:
System.out.println("Invalid selection.");
break;
}
}
你永远无法得到你的情况:9,退出....因为while循环告诉它跳过开关,如果int是9(我假设会关闭应用程序)尝试更改此
while(option != 9)