//Write a method named diceSum that prompts the user for a desired sum, then rep
ID: 3622654 • Letter: #
Question
//Write a method named diceSum that prompts the user for a desired sum, then repeatedly rolls two six-sided dice until their sum is the desired sum.
// not sum why it keeps repeating the same number for sum when sum should not be the same all the time since its a random roll.
import java.util.Scanner;
public class sumDie
{
public static void main(String [] args )
{
int dsum;
Scanner scan = new Scanner(System.in);
System.out.println( " Enter in a desired sum when rolling two die");
dsum=scan.nextInt();
Die d1= new Die();
Die d2 = new Die();
int sum= d1.roll()+ d2.roll();
int c=1;
while (c<=1000000){
if(sum!=dsum)
System.out.println(sum);
else
System.out.println();
c++;}
}
}
Explanation / Answer
please rate - thanks
you asked for a method, nothing about a class, so I got rid of your class. can add it if needed
import java.util.Scanner;
public class sumDie
{
public static void main(String [] args )
{diceSum();
}
public static void diceSum()
{
Scanner scan = new Scanner(System.in);
int dsum,d1,d2,sum,c=0;
System.out.println( " Enter in a desired sum when rolling two die");
dsum=scan.nextInt();
do
{
d1=(int)(Math.random() * 6) +1;
d2=(int)(Math.random() * 6) +1;
System.out.println("Your roll "+d1+" and "+d2);
sum= d1+ d2;
c++;
}while(sum!=dsum);
System.out.println("it took "+c+" rolls to get the desired amount");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.