当用户输入" 1"时,如何编写基于控制台的Java游戏进行重放以进行重放?

时间:2014-06-16 09:58:01

标签: java types console replay

首先,我想说我对这一切真的很新......我尽可能多地学习,所以如果我的任何代码看起来很荒谬或全是道歉,那么道歉这个地方,但我需要一个地方开始。 (顺便说一句,这段代码的基础归功于CrossCoastGaming:http://tinyurl.com/kktyq4e)。

现在就此事。我通过添加几个不同的短语,利用变量和添加试用计数器,对视频中的人显示的编码进行了改进(缺少更好的词)。这是我的代码:

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();

    }
}

}

好的,所以希望能让那些知道自己在做什么的人知道我的目标。现在,正如您所看到的那样:

if (replay == 1) {
        replay = scan.nextInt();

    }

我想写一种方式让人们可以重播游戏,而无需重启文件。我已经知道了我想做什么,但我到处搜索,似乎无法找到在此之后继续的内容。我确定我错过了一些东西。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以使用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循环总是至少执行一次。在循环结束时检查条件。