Java中的指定方法

Java中的指定方法,java,Java,我已经为自己创建了一个简单的项目,现在我正在尝试集成一个代码,当我在控制台中输入“restart”时,该代码将使程序重新启动。为此,我在我的程序中创建了第二个方法,它包含与我的主方法完全相同的代码,但现在我想指定第二个方法,我不知道该怎么做。感谢您的帮助! 以下是我目前的代码: public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.printl

我已经为自己创建了一个简单的项目,现在我正在尝试集成一个代码,当我在控制台中输入“restart”时,该代码将使程序重新启动。为此,我在我的程序中创建了第二个方法,它包含与我的主方法完全相同的代码,但现在我想指定第二个方法,我不知道该怎么做。感谢您的帮助! 以下是我目前的代码:

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println("guess the number between 1 and 9");

    int random = (int)(Math.random() * 9 + 1);

    int value = 0;
    do{
        value = scanner.nextInt();

        if (value != random){
            System.out.println("Try again");
        }

    }


    while(value != random);

    System.out.println("You guessed the number");   

    if(value == random){
        System.out.println("Would you like to restart?");
        String reset = scanner.nextLine();
        if (reset.equals("restart")){
            /*
             *I need some code here

            */
        }
    }   
}

public static void restart(String[] args){

    Scanner scanner = new Scanner(System.in);

    System.out.println("guess the number between 1 and 9");

    int random = (int)(Math.random() * 9 + 1);

    int value = 0;
    do{
        value = scanner.nextInt();

        if (value != random){
            System.out.println("Try again");
        }

    }


    while(value != random);

    System.out.println("You guessed the number");   

    if(value == random){
        System.out.println("Would you like to restart?");
        String reset = scanner.nextLine();
        if (reset.equals("restart")){

        }
    }   
}
您应该在“if(reset.equals(“restart”){…”中调用main()

或使用以下代码:

public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  do{

    System.out.println("guess the number between 1 and 9");

    int random = (int)(Math.random() * 9 + 1);

    int value = 0;
    do{
        value = scanner.nextInt();

        if (value != random){
            System.out.println("Try again");
        }    
    } 
    while(value != random);

    System.out.println("You guessed the number");       
    System.out.println("Would you like to restart?");
    String reset = scanner.nextLine();
  } 
  while(reset.equals("restart")); 
}

试试这样的方法,

在代码中

  • 你已经把代码弄乱了,需要根据需求进行分解
  • 此外,需要注意的是,若用户想重新启动,那个么游戏将再次播放
  • 下面的代码克服了这个问题


    为什么有两个方法做同样的事情?一个方法如何,只是重新开始?也不确定你使用“任命”这个词,你的意思是“调用”还是“运行”?我试图在不停止程序的情况下重新开始(仅在控制台中使用用户输入)。是的,我的意思是“运行”,围绕main:
    do的内容的最外层循环如何{…}while(reset.equals(“restart”);
    递归
    main
    函数似乎是个坏主意。为什么?主函数是公共静态函数,它等于任何其他静态函数(只需首先调用DoJava)。当然,您可以创建名为work()的新函数或者使用do while循环,但是为了研究或测试使用递归主函数没有问题。这比将一些代码复制到其他函数要好得多。你应该将
    新扫描器
    从循环中取出,否则你会在同一个输入流上创建多个扫描器。为什么在不需要递归的情况下进行递归呢?为什么这样做很奇怪对于本主题的作者来说,就我而言,我从来没有在这个任务中使用递归,但是作者可能在研究递归方法吗?
    public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      do{
    
        System.out.println("guess the number between 1 and 9");
    
        int random = (int)(Math.random() * 9 + 1);
    
        int value = 0;
        do{
            value = scanner.nextInt();
    
            if (value != random){
                System.out.println("Try again");
            }    
        } 
        while(value != random);
    
        System.out.println("You guessed the number");       
        System.out.println("Would you like to restart?");
        String reset = scanner.nextLine();
      } 
      while(reset.equals("restart")); 
    }
    
    static int value;
    static int random  = (int)(Math.random() * 9 + 1);
    static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
    
        do{
            System.out.println("Enter Your Guesses :");
            value = scanner.nextInt();
            if(value == random){
                System.out.println("Exactly Match : " + value  + " == " + random);
                System.out.println("Would you like to restart?");
                String reset = scanner.next();
                if (reset.equals("restart")){
                    restart();
                }
            }else{
                System.out.println("Try again");
            }
    
        }while(value != random);
    
        System.out.println("You guessed the number");   
    
    
    }
    public static void restart(){
        do{
            random = (int)(Math.random() * 9 + 1);
        }while(value == random);
    }