Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 程序需要循环X次,无论输入多少都只运行一次。没有抛出错误_Java - Fatal编程技术网

Java 程序需要循环X次,无论输入多少都只运行一次。没有抛出错误

Java 程序需要循环X次,无论输入多少都只运行一次。没有抛出错误,java,Java,程序需要运行Dice.java,无论用户输入多少次,都只运行一次,不会抛出错误。我也不能证明什么,但感觉输出有一个模式 每样东西都试了一点 DiceGame1.java-充当主循环,获取用户希望一次掷多少骰子,并处理主循环 import java.util.Scanner; public class DiceGame1 { public static int mainloop; public static void main(String [] args) {

程序需要运行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();
            }
        }
}

}

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

在文件DiceGame1.java中,在这段代码中

//Create a for loop that will run X amount of times
for(int i =0; i< VAR_HOWMANY; i++)
{
   Dice.RollDie();
   RunAgain.RunAgain();
}
编辑:也可以修改您切换到以下内容:

 // 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;
        }

您在错误的位置调用runreach(),并且在开关状态中每次使用
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);
        }
    }
}
import java.util.Random;
导入java.util.Scanner;
类骰子游戏1
{
公共静态int主回路;
公共静态void main(字符串[]args)
{
mainloop=0;
Scanner OBJ_USERINPUT=新扫描仪(System.in);
System.out.println(“要掷多少骰子:”);
int VAR_HOWMANY=Integer.parseInt(OBJ_USERINPUT.nextLine());
while(主循环<1)
{
//创建将运行X次的for循环
for(int i=0;i
即使开关状态不一定正确,他也没有在使用案例后使用break@戴夫:你需要在你的案件陈述中逐一说明。看看我编辑过的答案。我已经测试过了,它现在可以工作了。但这并不能完全解决它,当你接受再次运行时,它会滚动你每次最初输入的骰子数。每次都需要提示。
 // 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;
        }

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