Программа должна выполнить цикл X количество раз, будет запускаться только один раз, независимо от ввода.Ошибки не выброшены - PullRequest
0 голосов
/ 27 сентября 2019

Программа должна запускать Dice.java, однако, сколько раз пользователь заходит, запускается только один раз и не выдает ошибок.Я также ничего не могу доказать, но мне кажется, что у вывода есть шаблон.

Пробовал немного всего.

DiceGame1.java - Действует как основной цикл, занимаетсколько кубиков пользователь хочет бросить за раз, и обрабатывает основной цикл.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);
        }
    }
}

Ответы [ 2 ]

0 голосов
/ 27 сентября 2019

вы вызываете RunAgain () в неправильном месте, и вы не использовали break после каждого использования case в своем статусе коммутатора, здесь это работает:

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);
        }
    }
}
0 голосов
/ 27 сентября 2019

В файле 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();
}

не должен вызываться RunAgain.RunAgain() вне вас для цикла for, чтобы получить желаемое поведение.

Следовательно, должно быть что-то вроде этого,

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...