Java猜测游戏统计数据扩展

时间:2013-12-13 01:05:33

标签: java

我正在为学校制作一个猜谜游戏计划。到目前为止,这是我的代码:

package chapter5;
import java.util.*;
public class ProgrammingProject3 {

@SuppressWarnings("resource")
public static void main(String[] args) {
    Random rand = new Random();
    int num = rand.nextInt(100) + 1;
    int tries = 0;
    Scanner scanner = new Scanner (System.in);
    int guess = 0;
    boolean win = false;
    System.out.println("Guess a number between 1 and 100.");
    while (win == false){
        guess = scanner.nextInt();
        tries++;
        if (guess > 100 || guess < 1){
            System.out.println("Your number must be between 1 and 100!");
        }else if(guess == num){
            win = true;
        } else if(guess < num){
            System.out.println("Higher! Guess again.");
        } else if(guess > num){
            System.out.println("Lower! Guess again.");
        }
    }
    if (win == true){
        if (tries <= 5){
            System.out.println("Amazing! You got it in " + tries + " guesses! The number was " + num + ".");
        } else if (tries <= 10){
            System.out.println("Pretty good, you guessed the number in " + tries + " guesses. The number was " + num + ".");
        } else if (tries <= 15){
            System.out.println("Try harder next time. You guessed the number in " + tries + " guesses. The number was " + num + ".");
        } else {
            System.out.println("Ouch! you guessed the number in " + tries + " guesses. The number was " + num + ".");
    }
    }
}

}

它适用于我,但我还需要扩展程序,使其接受播放多个程序,直到用户选择停止(使用哨兵),然后提供有关总数和平均猜测数的统计数据。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

您可以围绕两个while循环实现另一个while循环,并且每次都询问用户是否要再次播放。根据此选择,您可以再次进入循环或转到结尾。对于你的统计数据,你需要另一个整数来计算游戏的数量。