尝试使用数据创建菜单

时间:2019-10-04 00:46:30

标签: java menu

我正在尝试使用从另一则帖子Creating a console menu for user to make a selection中看到的数据创建一个菜单,用户可以在其中选择一个选项。当我运行程序时,它将引发一些错误。 也许我导入不正确,但这是唯一的方法 我能想到的。

import java.util.Random;
import java.util.Data;
public class Main{
public static void main(String[] args) {
System.out.println("Please enter 1 if you'd like to guess the computer's number or enter 2 to have the computer guess your number. If 2, please enter a number between 1-100 after you enter 2. ");
Data data = new Data();
    data.menu();

    Scanner scanner = new Scanner(System.in);
    int choice = scanner.nextInt();

    switch (choice) {
        case 1:
        double r = (Math.random()*((100-1)+1))+min;

        int win = 0;
        while (win != 1);
        System.out.println("The computer has guessed a number, please make a guess. ");
        int u1 = scanner.nextInt();  
        if (r > u1)
        System.out.println("Too low.");
        else if (r < u1)
        System.out.println("Too high.");
        else if (r == u1){
        System.out.println("You guessed it!!!!!!! Congratulations!!!!!!!!!!");
        int win = win + 1 ;}
        else
        System.out.println();
         break;
        case 2:
        System.out.println("You'd like myself, the computer to guess huh, well be prepared to get destroyed!");
        int un = scanner.nextInt();
        if (un == 36){
        System.out.println("Chandler I told you not to!!!!!!");
        break;}
        else
        System.out.println();

        System.out.println("Your number is " + un);
          break;


        default:
        System.out.println("You weren't supposed to do that!!!! Stop the program and run it again. Congrats! You broke it! Chandler, if you entered 36, ");
        TimeUnit.SECONDS.sleep(5);
        System.out.print("you've been banned!");
}

}

}

1 个答案:

答案 0 :(得分:0)

所有编译器错误均按顺序进行。

import java.util.Data; // Does not exist, @MadProgrammer is correct, causes error
Data data = new Data(); // Error, Data cannot be resolved as to a type

Scanner scanner = new Scanner(System.in); // You did not import java.util.Scanner.
// Scanner cannot be resolved to a type.

double r = (Math.random()*((100-1)+1))+min; // min cannot be resolved to a variable
// You didn't define it

System.out.println("The computer has guessed a number, please make a guess. ");
int win = win + 1 ;} // win was a variable that was previously defined. Just change it 
from "int win =" to "win = "

TimeUnit.SECONDS.sleep(5); // Did you mean to import java.util.concurrent.TimeUnit;

此外,如果您要“禁止”程序中的某人,那么为什么不制作一个.txt文件以进行读取,则可以帮助您的程序记住该文件之前是否已“损坏”。