当用户输入“1”时,如何编写基于控制台的Java游戏来重播?

当用户输入“1”时,如何编写基于控制台的Java游戏来重播?,java,types,console,replay,Java,Types,Console,Replay,首先,我想说的是,我对这一切都很陌生。。。我已经尽可能多地学习了,所以如果我的任何代码看起来很可笑或到处都是,我表示歉意,但我需要从某个地方开始。顺便说一句,这段代码的基础归功于CrossCoastGaming: 现在谈谈手头的事情。我已经改进了,因为视频中的那个人在编码方面没有更好的词,通过添加几个不同的短语,利用变量和添加一个try计数器。这是我的密码: import java.util.Random; import java.util.Scanner; public class Main

首先,我想说的是,我对这一切都很陌生。。。我已经尽可能多地学习了,所以如果我的任何代码看起来很可笑或到处都是,我表示歉意,但我需要从某个地方开始。顺便说一句,这段代码的基础归功于CrossCoastGaming:

现在谈谈手头的事情。我已经改进了,因为视频中的那个人在编码方面没有更好的词,通过添加几个不同的短语,利用变量和添加一个try计数器。这是我的密码:

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

public class Main {

public static int number, guess, tryCount, replay;
public static int maxValue =1;
public static Scanner scan;
public static Random rand;

public static void main(String args[]) {
    scan = new Scanner(System.in);
    rand = new Random();
    System.out.print("Enter a maximum number: ");
    while(maxValue < 2)
        maxValue = scan.nextInt();
    number = rand.nextInt(maxValue);
    System.out.print("Guess a number from 1 to " + maxValue + ": ");
    while (guess != number) {
        guess = scan.nextInt();
        tryCount++;
        if (guess < 1) {
            System.out.print("Guess is not positive. Try again: ");
        }else if (guess < number) {
            System.out.print("Too low! Try again: ");
        }
        if (guess > maxValue) {
            System.out.println("Guess is higher than " + maxValue + ". Try again: ");
        }else if (guess > number) {
            System.out.print("Too high! Try again: ");
        }
    }
    if (tryCount == 1) {
        System.out.println("Nailed it! It only took you 1 try!");
    }
    else {
        System.out.println("Nailed it! It took you " + tryCount + " tries.");
    }
    System.out.println("Type 0 to play again. Type 1 to quit.");
    if (replay == 1) {
        replay = scan.nextInt();

    }
}

}

我想写一个方法,让人们可以重放游戏,而不必重新启动文件。我已经有了一个想法,我想做什么,但我已经到处找了,似乎找不到在这一点之后继续做什么。我肯定我错过了什么。非常感谢您的帮助。

您可以使用do while循环来实现此目的:

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

public class Main {

    public static int number, guess, tryCount, replay;
    public static int maxValue = 1;
    public static Scanner scan;
    public static Random rand;

    public static void main(String args[]) {
        scan = new Scanner(System.in);
        rand = new Random();
        do { // start of do-while loop
            tryCount = 0; // reset tryCount
            System.out.print("Enter a maximum number: ");
            while(maxValue < 2) {
                maxValue = scan.nextInt();
            }
            number = rand.nextInt(maxValue);
            System.out.print("Guess a number from 1 to " + maxValue + ": ");
            while (guess != number) {
                guess = scan.nextInt();
                tryCount++;
                if (guess < 1) {
                    System.out.print("Guess is not positive. Try again: ");
                } else if (guess < number) {
                    System.out.print("Too low! Try again: ");
                }
                if (guess > maxValue) {
                    System.out.println("Guess is higher than " + maxValue + ". Try again: ");
                } else if (guess > number) {
                    System.out.print("Too high! Try again: ");
                }
            }
            if (tryCount == 1) {
                System.out.println("Nailed it! It only took you 1 try!");
            } else {
                System.out.println("Nailed it! It took you " + tryCount + " tries.");
            }

            do { // check the user's input
                System.out.println("Type 0 to play again. Type 1 to quit.");
                replay = scan.nextInt();
                if (replay != 0 && replay != 1) {
                    System.out.println("Input not recognized.");
                }
            } while (replay != 0 && replay != 1);
        } while (replay == 0); // end of do-while loop
    }
}

do while循环始终至少执行一次。循环结束时会检查条件。

那么,您想在不再次运行代码的情况下再次启动游戏吗?嗯,我想要一种方法使代码再次运行,但我不想再次键入代码。对不起,我没什么意思。你是想每次都向用户询问最大值,还是第一次问?每次都是,因为这样会有更多的变化。这有些效果。但是,第二次之后,它会出现小故障,不允许我选择新的maxValue。当它突然无缘无故地结束比赛时,情况会变得更糟。错过了。每次都需要将tryCount变量重置为0。我会更新我的答案,我更新了密码。我还在末尾添加了另一个do-while循环,以检查当询问玩家是否想再次玩时,输入是否为0或1。这几乎是正确的,除了maxValue保持不变这一事实,这使得两者都输入一个max值:并同时猜测一个从1到maxValue的数字,这意味着用户不能选择一个新的maxValue。我应该像你做tryCount那样重置maxValue吗?事实上,没关系。我实现了重置maxValue的代码,它成功了。非常感谢你的帮助。我发现,当你可以与人交谈,让事情发生时,编码活动是非常有益的。
import java.util.Random;
import java.util.Scanner;

public class Main {

    public static int number, guess, tryCount, replay;
    public static int maxValue = 1;
    public static Scanner scan;
    public static Random rand;

    public static void main(String args[]) {
        scan = new Scanner(System.in);
        rand = new Random();
        do { // start of do-while loop
            tryCount = 0; // reset tryCount
            System.out.print("Enter a maximum number: ");
            while(maxValue < 2) {
                maxValue = scan.nextInt();
            }
            number = rand.nextInt(maxValue);
            System.out.print("Guess a number from 1 to " + maxValue + ": ");
            while (guess != number) {
                guess = scan.nextInt();
                tryCount++;
                if (guess < 1) {
                    System.out.print("Guess is not positive. Try again: ");
                } else if (guess < number) {
                    System.out.print("Too low! Try again: ");
                }
                if (guess > maxValue) {
                    System.out.println("Guess is higher than " + maxValue + ". Try again: ");
                } else if (guess > number) {
                    System.out.print("Too high! Try again: ");
                }
            }
            if (tryCount == 1) {
                System.out.println("Nailed it! It only took you 1 try!");
            } else {
                System.out.println("Nailed it! It took you " + tryCount + " tries.");
            }

            do { // check the user's input
                System.out.println("Type 0 to play again. Type 1 to quit.");
                replay = scan.nextInt();
                if (replay != 0 && replay != 1) {
                    System.out.println("Input not recognized.");
                }
            } while (replay != 0 && replay != 1);
        } while (replay == 0); // end of do-while loop
    }
}