有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

重复java中已经满足的循环

我期待着重复一个“游戏”,如果它已经在我的情况下,用户猜测随机数感到满意。我不知道从哪里返回到主游戏,除非我必须在其中创建另一个“do-while”循环,并在其中显示: System.out.println("you have tried: " + count + " times. Would you like to play again? y/n")的部分重新键入游戏。有没有一种方法可以直接回到实际的猜测循环,而不是创建另一个循环? 希望有意义

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class pass {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String pass = "password123";
        String input;
        int guess;
        int count;
        count = 0;
        int num;


        do {
            System.out.print("Enter your password: ");
            input = scanner.next();

        } while (!input.equals(pass));
        System.out.println("Correct! Now play the guess game! Guess a number between 1 - 10.");
        do {
            num = ThreadLocalRandom.current().nextInt(1,10);
            guess = scanner.nextInt();
            count++;
            if (guess == num) {
                System.out.println(" Well done!");
                **System.out.println("you have tried: " + count + " times. Would you like to play again? y/n");**
            }
            else if (guess < num) {
                System.out.println("your number is smaller than the number given");
            }
            else {
                System.out.println("your guess is too high");
            }

        } while (guess != num);



    }
}

共 (3) 个答案

  1. # 1 楼答案

    如果您想重用代码,您可以创建函数(或方法,因为我们在一个类中)。它们可以用来封装代码,并从任何地方调用代码来使用它

    您可以定义这样的方法:

    public static void methodName() {
        // code go here
    }
    

    然后,你可以在任何地方这样称呼它:

    pass.methodName(); // It will execute the code inside methodName()
    

    实际上,这比这要复杂得多,您可以给方法赋值并返回其他方法,更改其范围使其仅为内部的或可由其他类访问。但我想你是个初学者,所以我保持简单。我强烈建议您快速研究一下面向对象编程

    对于您的代码,您可以将游戏的while循环放入一个方法中,并在开始时和每次玩家想要重新启动游戏时调用它。祝你的比赛好运

  2. # 2 楼答案

    我设法做到了这一点。这似乎是可行的,但有一件事让我失望,在最后一次,当我在“n”或其他键,而不是“y”Exception in thread "main" java.util.InputMismatchException。有没有更柔和的方法来完成它

    import java.util.Scanner;
    import java.util.concurrent.ThreadLocalRandom;
    
    public class pass {
    
    
        public static void randomnum(){
            Scanner scanner = new Scanner(System.in);
            int guess;
            int count;
            count = 0;
            int num;
            do {
                num = ThreadLocalRandom.current().nextInt(1,10);
                guess = scanner.nextInt();
                count++;
                if (guess == num) {
                    System.out.println(" Well done!");
                    System.out.println("you have tried: " + count + " times.");
                    String answer;
                    do{
                        System.out.println("Do you want to play again? y/n");
    
                        answer = scanner.next();
                        if (answer.equals("y")) {
                            System.out.println("let's play again");
                            randomnum();
                            System.out.println("Correct! Now play the guess game! Guess a number between 1 - 10.");
                        }
                        else {
                            System.out.println("you are logout!");
                            break;
                        }
    
                    }while (answer.equals("Y"));
                    randomnum();
                }
                else if (guess < num) {
                    System.out.println("your number is smaller than the number given");
                }
                else {
                    System.out.println("your guess is too high");
                }
    
            } while (guess != num);
        }
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String pass = "password123";
            String input;
    
    
    
            do {
                System.out.print("Enter your password: ");
                input = scanner.next();
    
            } while (!input.equals(pass));
            System.out.println("Correct! Now play the guess game! Guess a number between 1 - 10.");
            randomnum();
    
        }
    }
    
  3. # 3 楼答案

    最简单的解决方案是将整个“猜测循环”移动到一个单独的方法中。然后,如果希望它重复,只需递归调用该方法