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
=
0
;
while
(
play
)
{
System.out.print
(
"
Enter the number of dice and number of sides
(
enter
0
to exit
)
:
"
)
;
numDice
=
scnr
.
nextInt
(
)
;
numSides
=
scnr
.
nextInt
(
)
;
DiceShooter dice
=
new DiceShooter
(
numSides
)
;
if
(
numDice
=
=
0
|
|
numSides
=
=
0
)
{
break;
}
System.out.print
(
"
D
"
+
numSides
+
"
:
"
)
;
for
(
int i
=
0
; i
<
numDice; i
+
+
)
{
dice.roll
(
)
;
System.out.print
(
dice
.
getNumRolled
(
)
)
;
if
(
i
<
numDice
-
1
)
{
System.out.print
(
"
,
"
)
;
}
total
+
=
dice.getNumRolled
(
)
;
}
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
=
0
;
while
(
play
)
{
System.out.print
(
"
Enter the number of dice and number of sides
(
enter
0
to exit
)
:
"
)
;
numDice
=
scnr
.
nextInt
(
)
;
numSides
=
scnr
.
nextInt
(
)
;
DiceShooter dice
=
new DiceShooter
(
numSides
)
;
if
(
numDice
=
=
0
|
|
numSides
=
=
0
)
{
break;
}
}
System.out.print
(
"
D
"
+
numSides
+
"
:
"
)
;
for
(
int i
=
0
; i
<
numDice; i
+
+
)
{
dice.roll
(
)
;
System.out.print
(
dice
.
getNumRolled
(
)
)
;
if
(
i
<
numDice
-
1
)
{
System.out.print
(
"
,
"
)
;
}
total
+
=
dice.getNumRolled
(
)
;
}
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
0
to exit
)
:
2
2
0
Enter the number of dice and number of sides
(
enter
0
to exit
)
:
1
1
0
Enter the number of dice and number of sides
(
enter
0
to exit
)
:
3
6
Enter the number of dice and number of sides
(
enter
0
to exit
)
:
0
0
and then print the output
the error i get is
Enter the number of dice and number of sides (enter 0 to exit): 220
output
D20: 15,1
Enter the number of dice and number of sides (enter 0 to exit): 110
output
D10: 7
etc

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!