多个扫描仪DoWhile HiLo(10面骰子游戏)

时间:2017-12-14 17:17:17

标签: java windows-7 java.util.scanner do-while bluej

我创建了一款游戏,可以在1&之间打印一个随机卷号。 10.然后,系统询问用户是否想要为第二个卷号猜测更高[1]或更低[2]。根据他们的猜测,系统将打印“Draw!”,“你错误地猜到了”。或者“你猜对了。”它使用两个嵌套的if子句正确地工作:

public class Short {
public static void main(String args[]) {

        //roll one
        int rNum1 = (int)(Math.random() * 11); //declare random roll one,  gives result between 1 and 10

        //print roll one
        System.out.println("You roll the 10-sided die."); 
        System.out.println("It lands on " + rNum1 + ".");
        System.out.println(); //space

        //User guess
        Scanner choice = new Scanner (System.in);
        System.out.println("Will a second die roll higher or lower?");
        System.out.println("To guess higher enter [1], or to guess lower, enter [2].");

        //roll two
        int rNum2 = (int)(Math.random() * 11); //declare random roll two
        int guess = choice.nextInt(); //user's guess / HiLo

        //print roll two
        System.out.println(); //space
        System.out.println("You roll the die again."); 
        System.out.println("It lands on " + rNum2 + ".");
        System.out.println(); //space

    if (guess == 1) //guessed higher
    {
        if (rNum1 == rNum2)
        {
            System.out.println("Draw!");
        }
        else if (rNum1 > rNum2)
        {
            System.out.println("You guessed wrongly.");
        }
        else
        {
            System.out.println("You guessed correctly!");
        }
    }

    if (guess == 2) //guessed lower
    {
        if (rNum1 == rNum2)
        {
            System.out.println("Draw!");
        }
        else if (rNum1 > rNum2)
        {
            System.out.println("You guessed correctly!");
        }
        else
        {
            System.out.println("You guessed wrongly.");
        }
    }
}

虽然我希望通过输入[y]或[n]让用户有机会再次玩游戏。我试图使用do-while循环来实现这个声明第二个扫描程序(“重放”):

import java.util.*;
import java.util.Random; 
import java.util.Scanner;
public class Short {
public static void main(String args[]) {
    Scanner in = new Scanner (System.in);
    String replay = in.next(); //game replay scanner

    do{

        //roll one
        int rNum1 = (int)(Math.random() * 11); //declare random roll one,  gives result between 1 and 10

        //print roll one
        System.out.println("You roll the 10-sided die."); 
        System.out.println("It lands on " + rNum1 + ".");
        System.out.println(); //space

        //User guess
        Scanner choice = new Scanner (System.in);
        System.out.println("Will a second die roll higher or lower?");
        System.out.println("To guess higher enter [1], or to guess lower, enter [2].");

        //roll two
        int rNum2 = (int)(Math.random() * 11); //declare random roll two
        int guess = choice.nextInt(); //user's guess / HiLo

        //print roll two
        System.out.println(); //space
        System.out.println("You roll the die again."); 
        System.out.println("It lands on " + rNum2 + ".");
        System.out.println(); //space

    if (guess == 1) //guessed higher
    {
        if (rNum1 == rNum2)
        {
            System.out.println("Draw!");
        }
        else if (rNum1 > rNum2)
        {
            System.out.println("You guessed wrongly.");
        }
        else
        {
            System.out.println("You guessed correctly!");
        }
    }

    if (guess == 2) //guessed lower
    {
        if (rNum1 == rNum2)
        {
            System.out.println("Draw!");
        }
        else if (rNum1 > rNum2)
        {
            System.out.println("You guessed correctly!");
        }
        else
        {
            System.out.println("You guessed wrongly.");
        }
    }
    System.out.println("Would you like to play again? Enter [y] for yes.");
}while(replay == "y");
}

然而,整个循环作为无限循环初始化。如何使用多个扫描仪修复我的dowhile?我意识到我对java很不错。我是否想使用do while循环来允许用户在第一次运行后重放?

3 个答案:

答案 0 :(得分:0)

你很接近但是尝试使用nextLine()使用scanner对象抓取输入,然后将输入解析为整数。然后在最后询问用户是否想再次播放。

        String replay = "y";
        Scanner choice = new Scanner (System.in);
        while(replay.equals("y"))
        {

            //roll one
            int rNum1 = (int)(Math.random() * 11); //declare random roll one,  gives result between 1 and 10

            //print roll one
            System.out.println("You roll the 10-sided die."); 
            System.out.println("It lands on " + rNum1 + ".");
            System.out.println(); //space

            //User guess
            System.out.println("Will a second die roll higher or lower?");
            System.out.println("To guess higher enter [1], or to guess lower, enter [2].");

            //roll two
            int rNum2 = (int)(Math.random() * 11); //declare random roll two
            int guess = Integer.parseInt(choice.nextLine()); //user's guess / HiLo

            //print roll two
            System.out.println(); //space
            System.out.println("You roll the die again."); 
            System.out.println("It lands on " + rNum2 + ".");
            System.out.println(); //space

            if (guess == 1) //guessed higher
            {
                if (rNum1 == rNum2)
                {
                    System.out.println("Draw!");
                }
                else if (rNum1 > rNum2)
                {
                    System.out.println("You guessed wrongly.");
                }
                else
                {
                    System.out.println("You guessed correctly!");
                }
            }

            if (guess == 2) //guessed lower
            {
                if (rNum1 == rNum2)
                {
                    System.out.println("Draw!");
                }
                else if (rNum1 > rNum2)
                {
                    System.out.println("You guessed correctly!");
                }
                else
                {
                    System.out.println("You guessed wrongly.");
                }
            }
            System.out.println("Would you like to play again? Enter [y] for yes.");
            replay = choice.nextLine();
        }

答案 1 :(得分:0)

关于要求再次播放,将代码封装在'while'循环中,例如

boolean playAgain = true;

while (playAgain == true)
  {
      your code...
  ......
  ""Ask the user if they wish to play again (y/n), and change this to true or false accordingly.

这将允许游戏循环直到玩家不想再玩一次。

答案 2 :(得分:-1)

提示用户后您没有输入。在检查之前阅读他的回复。 无限循环是由于用户的初始回复是' y'因此循环将无限执行或根本不执行(当第一个回复是' n&# 39)