package javaapplication10;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Tom
*/
public class JavaApplication10 {
static int randomWithRange(int min, int max)
{
int range = (max - min) + 1;
return (int)(Math.random() * range) + min;
}
public static void menu(){
System.out.println("MENU:");
System.out.println("1.Questions");
System.out.println("2.High Scores");
System.out.println("3.Exit");
}//creates the function "menu" with 3 different options
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String userName = scanner.next();//stores the user's name as the variable userName
System.out.println("What class are you in "+userName+"? (1, 2 or 3");
int userClass = scanner.nextInt();
menu();//outputs the function "menu"
System.out.println("Please choose an option from the menu:");
int menuChoice = scanner.nextInt();//stores the integer that the user has entered as the menuChoice
int[] highScores1 = new int[30];
int[] highScores2 = new int[30];
int[] highScores3 = new int[30];
String[] Names = new String[30];
boolean run = true;
ArrayList calc = new ArrayList();//stores all the operations in an array list
calc.add("+");
calc.add("-");
calc.add("*");
while(menuChoice != 3){
switch(menuChoice){
case 1://if the menuChoice = 1 it carries out te following code
System.out.println(" ");
System.out.println("Please answer the following questions:");
System.out.println("This test will involve some negative answers so watch out for them!");
System.out.println(" ");
int counter = 0;//starts the counter at 0
int counter1 = 0;
int score = 0;
Names[counter1] = userName;
for (int i = 1; i<11; i++){
int randomnr = randomWithRange(10,0);//creates a random number with the max number being 10
int randomnr2 = randomWithRange(10,0);
int randomcalc = randomWithRange(0,2);
System.out.println("Question "+i+": "+randomnr+" "+calc.get(randomcalc)+" "+randomnr2);
System.out.print("Enter the correct answer: ");
int answer = scanner.nextInt();
if (randomcalc == 0){
int calculation = randomnr+randomnr2;
if(answer == calculation){
System.out.println("Correct!");
counter++;
score++;
System.out.println(" ");
}
else{
System.out.println("Wrong, "+calculation+" is the correct answer");
System.out.println(" ");
}
}
if (randomcalc == 1){
int calculation = randomnr-randomnr2;
if(answer == calculation){
System.out.println("Correct!");
counter++;
score++;
System.out.println(" ");
}
else{
System.out.println("Wrong, "+calculation+" is the correct answer");
System.out.println(" ");
}
}
if (randomcalc == 2){
int calculation = randomnr*randomnr2;
if(answer == calculation){
System.out.println("Correct!");
counter++;
score++;
System.out.println(" ");
}
else{
System.out.println("Wrong, "+calculation+" is the correct answer");
System.out.println(" ");
}
}
}
System.out.println("Congratulations "+userName+", you have scored "+counter+"/10");
if(counter <= 3){
System.out.println("You need to work harder!");
}
else if(counter <= 6 && counter > 3){
System.out.println("Keep putting in the work and you will start getting those top grades!");
}
else if(counter <= 9 && counter > 8){
System.out.println("Unlucky! With a little more revision you can achieve those top marks!");
}
else if(counter <= 0){
System.out.println("Are you drunk?!");
}
else if(counter == 10){
System.out.println("Well done "+userName+", your work has clearly paid off, top marks!");
}
highScores1[counter1] = counter;
counter++;
highScores2[counter1] = counter;
counter++;
highScores3[counter1] = counter;
counter++;
break;
case 2:
if(userClass == 1){
System.out.println(Arrays.toString(Names));
System.out.println(Arrays.toString(highScores1));
}
if(userClass == 2){
System.out.println(Arrays.toString(Names));
System.out.println(Arrays.toString(highScores1));
}
if(userClass == 3){
System.out.println(Arrays.toString(Names));
System.out.println(Arrays.toString(highScores3));
}
break;
case 3:
System.out.println("Please come back soon!");
run = false;
}
}
}
}
我已经编写了这段代码,在代码执行完毕后我很难回到菜单中。还有我的情况2,它连续打印数组,我想知道如何打印一次数组的内容,然后再次返回菜单。任何帮助赞赏。
答案 0 :(得分:0)
您可以通过说“menu()”来调用方法菜单,就像使用任何其他方法一样。
if(x ==2){
menu();
}
答案 1 :(得分:0)
你需要在while()循环中重复菜单选择,就在switch()语句结束之后和while循环的右大括号之前。添加:
menu();
System.out.println("Please choose an option from the menu:");
menuChoice = scanner.nextInt();
......此时应该这样做。
注意,这也意味着没有必要使用&#34;案例3&#34;你的switch语句中的位 - 只需将那段代码放在while循环之外。如果您将某些代码从switch语句重构为自己的方法,您会发现更容易阅读这些代码块。