gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:04:23 26 4
gpt4 key购买 nike

我创建了一个游戏,打印 1 到 10 之间的随机卷号。然后系统会询问用户是否想猜测第二个卷号的较高 [1] 或较低 [2]。根据他们的猜测,系统将打印“抽奖!”、“你猜错了”。或“你猜对了。”使用两个嵌套的 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 循环来允许用户在第一次运行后重播?

最佳答案

您已经很接近了,但尝试使用 nextLine() 使用扫描仪对象抓取输入,然后将输入解析为整数。然后最后询问用户是否想再次玩。

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

关于java - 多个扫描仪 DoWhile HiLo(10 面骰子游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47818869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com