简体   繁体   English

不间断地在java中创建一个猜数游戏,继续循环返回

[英]Create a guessing number game in java without break, continue and return in a loop

I am writing a guessing game.我正在写一个猜谜游戏。 The game is supposed to pick a random number between 1 and 99 (inclusive) and then after you pick the good number you get a "success" message.游戏应该选择一个介于 1 和 99(含)之间的随机数,然后在您选择合适的数字后,您会收到“成功”消息。 I can only do 5 tries.我只能尝试 5 次。 I think I need to do some try-catch , if-else and loops to make sure to get all the exceptions.我想我需要做一些try-catchif-else和循环以确保获得所有异常。 Not writing an int gives you an InputMismatchException and lets you try again but you don't lose a "life" you still got 5/5 or 3/5 tries (it depends on the progress you've done in the game).不写一个int会给你一个InputMismatchException并让你再试一次,但你不会失去“生命”你仍然有 5/5 或 3/5 次尝试(这取决于你在游戏中完成的进度)。 Same thing with numbers over the limits.数字超过限制也是一样。

Example:例子:

(1/5) 0 < ? < 100 : 50
(2/5) 50 < ? < 100 : 75
(3/5) 50 < ? < 75 :

(Some parts are in French) (部分为法文)

Thank you for your time!感谢您的时间!

https://i.stack.imgur.com/ovt1m.png https://i.stack.imgur.com/ovt1m.png

https://i.stack.imgur.com/abtPa.png https://i.stack.imgur.com/abtPa.png

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random r = new Random();
        int nbGenere = r.nextInt(100); 
        int nb1; // first number from user
        int nb2; // second number from user
        int borneMini; // minimum
        int borneMax; // max
        boolean valide1 = false; // variable do #1
        boolean valide2 = false; // variable do #2
        System.out.println(nbGenere);
        System.out.print("You got 5 tries to find a number 1 and 99 (inclusive)\n(1/5) 0 < ? < 100: ");
        do { // do #1
            try {
                nb1 = scan.nextInt();
                borneMini = 0;
                borneMax = 100;
                valide1 = nb1 > borneMini && nb1 < borneMax;
                if (!valide1)
                    System.out.print("Enter a valide number 1\n(1/5)" + borneMini + " < ? < " + borneMax + ": ");
                else if (nb1 == nbGenere)
                    System.out.print("Success!");
                else if (nb1 < nbGenere)
                    System.out.print("(2/5) " + nb1 + " < ? < 100: ");
                else
                    System.out.print("(2/5) 0 < ? < " + nb1 + ": ");
            } catch (InputMismatchException e) {
                System.out.print("Enter a valide number 2\n(1/5)\" + borneMini + \" < ? < \" + borneMax + \": ");
                scan.nextLine();
            }
            do { // #2
                try {
                    nb2 = scan.nextInt();
                    if (nb1 < nbGenere)
                        borneMini = nb1;
                    else
                        borneMini = 0;

                    if (nb1 > nbGenere)
                        borneMax = nb1;
                    else
                        borneMax = 100;

                    valide2 = ((nb1 > nb2 || nb1 < nb2) && nb1 > borneMini && nb1 < borneMax);
                    if (!valide2)
                        System.out.print("Enter a valide number 3\n(2/5)" + borneMini + " < ? < " + borneMax + ": ");
                    else if (nb2 == nbGenere)
                        System.out.print("success!");
                    else if (nb1 > nb2 )
                        System.out.print();
                } catch (InputMismatchException f) {
                    System.out.print("Enter a valide number 4\n(2/5");
                }
            } while (!valide2);
        } while (!valide1);
    }
}
int nbGenere = r.nextInt(100);

According to the documentation this method may return 0 (zero) which is not what you want.根据文档,此方法可能返回 0(零),这不是您想要的。 Better to use method nextInt(int, int) , ie最好使用方法nextInt(int, int) ,即

int nbGenere = r.nextInt(1, 100);

You only need one do-while loop and you need a variable to keep track of the number of guesses.您只需要一个do-while循环,并且需要一个变量来跟踪猜测的次数。

If the user-entered number is not within the valid range, you can throw an exception since you use the same handling for both a non-number and a number that is not within the acceptable range.如果用户输入的数字不在有效范围内,您可以抛出异常,因为您对非数字和不在可接受范围内的数字使用相同的处理。

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random r = new Random();
        int nb1; // number from user
        int borneMini = 0; // minimum
        int borneMax = 100; // maximum
        int nbGenere = r.nextInt(borneMini + 1, borneMax); 
        boolean valide1 = false;
        boolean valide2 = false; // variable do #1
        System.out.println(nbGenere);
        System.out.println("You have 5 tries to find a number between 1 and 99 (inclusive).");
        int tries = 5; // total guesses allowed
        int attempt = 1; // guess number
        do { // do #1
            System.out.printf("(%d/%d) %2d < ? < %3d : ", attempt, tries, borneMini, borneMax);
            try {
                nb1 = scan.nextInt();
                valide1 = nb1 > borneMini && nb1 < borneMax;
                if (!valide1) {
                    throw new InputMismatchException();
                }
                else if (nb1 == nbGenere) {
                    System.out.println("Success!");
                    valide2 = true;
                }
                else if (nb1 < nbGenere) {
                    attempt++;
                    borneMini = nb1 - 1;
                }
                else {
                    attempt++;
                    borneMax = nb1 + 1;
                }
            } catch (InputMismatchException e) {
                System.out.printf("Enter a valid number between %d and %d.%n", borneMini, borneMax);
                scan.nextLine();
            }
        } while (!valide2 && attempt <= tries);
        if (!valide2) {
            System.out.println("Better luck next time. The mystery number was " + nbGenere);
        }
    }
}

Output from a sample run:样本运行中的 Output:

65
You have 5 tries to find a number between 1 and 99 (inclusive).
(1/5)  0 < ? < 100 : 500
Enter a valid number between 0 and 100.
(1/5)  0 < ? < 100 : 50
(2/5) 49 < ? < 100 : 75
(3/5) 49 < ? <  76 : 62
(4/5) 61 < ? <  76 : 68
(5/5) 61 < ? <  69 : 65
Success!

Another sample run:另一个示例运行:

64
You have 5 tries to find a number between 1 and 99 (inclusive).
(1/5)  0 < ? < 100 : 50
(2/5) 49 < ? < 100 : foo
Enter a valid number between 49 and 100.
(2/5) 49 < ? < 100 : 75
(3/5) 49 < ? <  76 : 8
Enter a valid number between 49 and 76.
(3/5) 49 < ? <  76 : 62
(4/5) 61 < ? <  76 : 67
(5/5) 61 < ? <  68 : 66
Better luck next time. The mystery number was 64

not super sure why you have two do while loop but what you are looking for probably look sth like this不太确定为什么你有两个 do while 循环但你正在寻找的可能看起来像这样

        public void gameLoop(Scanner scanner, int life){
        int input;
        int number;
        int lifeRemaining = life;
        do {
            try {
                input = Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.err.println("Wrong input! Input only integer numbers please: " + e.getMessage());
            }
            //check if valid based on game rule
            if(!inputCheck(input)){
                //do sth
            }else{
                //only comes here if input is valid
                if(input > number){
                    //print sth
                    lifeRemaining--;
                }
                if(input<number){
                    //print sth
                    lifeRemaining--;
                }
                if(input==number){
                    //print sth
                    lifeRemaining--;
                }
            }
        }while(lifeRemaining>0);
        //game over
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM