gpt4 book ai didi

java - 程序需要循环 X 次,无论输入如何,都只会运行一次。没有抛出错误

转载 作者:行者123 更新时间:2023-12-02 00:10:30 25 4
gpt4 key购买 nike

无论用户输入多少次,程序都需要运行 Dice.java,只会运行一次并且不会抛出任何错误。我也无法证明任何事情,但感觉输出有一个模式。

什么都尝试了一点。

DiceGame1.java - 充当主循环,获取用户想要一次滚动的骰子数量,并处理主循环。

import java.util.Scanner;

public class DiceGame1
{
public static int mainloop;
public static void main(String [] args)
{
mainloop = 0;
Scanner OBJ_USERINPUT = new Scanner(System.in);
System.out.println("How many dice to roll: ");
int VAR_HOWMANY = OBJ_USERINPUT.nextInt();
while (mainloop < 1)
{
//Create a for loop that will run X amount of times
for(int i =0; i< VAR_HOWMANY; i++)
{
Dice.RollDie();
RunAgain.RunAgain();
}
}
}

}

Dice.java - 旨在完成滚动骰子的实际肮脏工作,然后将其交回给 DiceGame1.java 以让它知道再次运行。

import java.util.Random;
public class Dice
{
public static void RollDie()
{
Random DIEROLL = new Random();
//the integer randomInteger equals whatever the DIEROLL object generates.
int randomInteger = DIEROLL.nextInt(6);
//Add one to the integer randomInteger so 0 doesn't appear
randomInteger = randomInteger+1;
// Prints out the generated number
switch (randomInteger)
{
case 1:
System.out.println("1");
case 2:
System.out.println("2");
case 3:
System.out.println("3");
case 4:
System.out.println("4");
case 5:
System.out.println("5");
case 6:
System.out.println("6");
}
}
}

RunAgain.java - 目的是让DieGame1.java无限循环运行多长时间。

import java.util.Scanner;
public class RunAgain
{
public static void RunAgain()
{
String userinput;
Scanner keyboard = new Scanner(System.in);
System.out.print("Run again? Y/N: ");
userinput = keyboard.nextLine();
if (userinput.equals ("Y"))
{
System.out.println("======================================");
}
else
{
keyboard.close();
System.exit(0);
}
}
}

最佳答案

您在错误的位置调用 RunAgain(),并且在 switch 语句中每次使用 case 后都没有使用 break在这里它正在工作:

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

class DiceGame1
{
public static int mainloop;
public static void main(String [] args)
{
mainloop = 0;
Scanner OBJ_USERINPUT = new Scanner(System.in);
System.out.println("How many dice to roll: ");
int VAR_HOWMANY = Integer.parseInt(OBJ_USERINPUT.nextLine());
while (mainloop < 1)
{
//Create a for loop that will run X amount of times
for(int i =0; i < VAR_HOWMANY; i++)
{
Dice.RollDie();
}
RunAgain.RunAgain();
}
}
}
class Dice
{
public static void RollDie()
{
Random DIEROLL = new Random();
//the integer randomInteger equals whatever the DIEROLL object generates.
int randomInteger = DIEROLL.nextInt(6);
//Add one to the integer randomInteger so 0 doesn't appear
randomInteger = randomInteger+1;
// Prints out the generated number
switch (randomInteger)
{
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
case 3:
System.out.println("3");
break;
case 4:
System.out.println("4");
break;
case 5:
System.out.println("5");
break;
case 6:
System.out.println("6");
break;
}
}
}
class RunAgain
{
public static void RunAgain()
{
String userinput;
Scanner keyboard = new Scanner(System.in);
System.out.print("Run again? Y/N: ");
userinput = keyboard.nextLine();
System.out.println(userinput);
if (userinput.equalsIgnoreCase ("Y"))
{
System.out.println("======================================");
}
else
{
keyboard.close();
System.exit(0);
}
}
}

关于java - 程序需要循环 X 次,无论输入如何,都只会运行一次。没有抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58125352/

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