Question: im trying to use a while loop and have it repeat a question until zero is hit, then print all of the output at once
im trying to use a while loop and have it repeat a question until zero is hit, then print all of the output at once
import java.util.Scanner;
public class Main
public static void main
String
args
Scanner scnr
new Scanner
System
in
;
Boolean play
true;
int numDice;
int numSides;
int total
;
while
play
System.out.print
Enter the number of dice and number of sides
enter
to exit
:
;
numDice
scnr
nextInt
;
numSides
scnr
nextInt
;
DiceShooter dice
new DiceShooter
numSides
;
if
numDice
numSides
break;
System.out.print
D
numSides
:
;
for
int i
; i
numDice; i
dice.roll
;
System.out.print
dice
getNumRolled
;
if
i
numDice
System.out.print
;
total
dicegetNumRolled
;
System.out.println
;
System.out.println
Total:
total
;
ve tried to just have the results be printed once the loop is over but would get an error as it can't find the symbol dice
heres an example of what ive tried
import java.util.Scanner;
public class Main
public static void main
String
args
Scanner scnr
new Scanner
System
in
;
Boolean play
true;
int numDice;
int numSides;
int total
;
while
play
System.out.print
Enter the number of dice and number of sides
enter
to exit
:
;
numDice
scnr
nextInt
;
numSides
scnr
nextInt
;
DiceShooter dice
new DiceShooter
numSides
;
if
numDice
numSides
break;
System.out.print
D
numSides
:
;
for
int i
; i
numDice; i
dice.roll
;
System.out.print
dice
getNumRolled
;
if
i
numDice
System.out.print
;
total
dicegetNumRolled
;
System.out.println
;
System.out.println
Total:
total
;
im thinking i might need to make another java file or method to make this work but stuck on how to go about this.
for example i need it to :
Enter the number of dice and number of sides
enter
to exit
:
Enter the number of dice and number of sides
enter
to exit
:
Enter the number of dice and number of sides
enter
to exit
:
Enter the number of dice and number of sides
enter
to exit
:
and then print the output
the error i get is
Enter the number of dice and number of sides enter to exit:
output
D:
Enter the number of dice and number of sides enter to exit:
output
D:
etc
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
