i wrote the method under public static void main(String[] args) please help me write it under public static int rollDie(). I do not know how to write it under the public static please help.
Write a separate method to accomplish this function, and call it rollDie() to return the randomly generated number.
public static int rollDie()
Call the rollDie() method twice to generate two random numbers for two dice. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (i.e., 4, 5, 6, 8, 9, or 10), a point is established.
import java.util.Random;
import java.util.Scanner;
public class CrapsGame //main method
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int dice1;
int dice2;
int dice3;
int dice4;
dice1=(int)(Math.random()*6+1);
dice2=(int)(Math.random()*6+1);
int sum = dice1 + dice2;
boolean game = true;
System.out.println("You rolled " +dice1+ " + " +dice2+ " = " +sum);
if ((sum==2)||(sum==3)||(sum==12))
{
game = false;
System.out.println("You lose");
}
else
if ((sum==7)||(sum==11)){
game = false;
System.out.println("You win");
}
else System.out.println("point is "+sum);
int newSum;
while (true)
{
newSum = dice1+dice2;
dice3=(int)(Math.random()*6+1);
dice4=(int)(Math.random()*6+1);
System.out.println("You rolled "+dice1+" + "+dice2+" = "+newSum);
if (newSum==sum)
{
game = false;
System.out.println("You win");
}
else
if (newSum==7)
{
game =false;
System.out.println("You lose");
}
else {
System.out.println("point is "+sum);
}
System.out.println();
System.out.print("Would you like to go again? ");
String nextInput = scanner.next();
if ("yes".equalsIgnoreCase(nextInput))
{
continue;
}
else if ("no".equalsIgnoreCase(nextInput))
{
break;
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire