I need to convert the attached code so that it works using the following methods
ID: 3548211 • Letter: I
Question
I need to convert the attached code so that it works using the following methods:
public static int roll();
o Generates a random dice roll value and returns it.
public static int playTurn();
o Plays a single turn
public static void printRollResult(introllValue, int turnTotal)
o Display the rolled value and turn total.
public static boolean isHold();
o Prompt the user to 1. Roll again or 2. Hold
import java.util.Scanner;
public class Pig {
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);
int totalScore = 0;
{
for (int turn = 1; turn <=5; turn++)
{
String response = "";
System.out.println();
System.out.println("Turn: " + turn);
int turnScore = 0;
do
{ int rollScore = roll();
System.out.println(" Roll Value: " + rollScore);
if (rollScore == 1)
{ turnScore = 0;
System.out.println(" Turn total: " + turnScore);}
else
{ turnScore = turnScore + rollScore;
System.out.println(" Turn total: " + turnScore);
System.out.print(" 1) Roll again or 2) Hold? ");
response = scan.nextLine();
}
}
while (response.equals("1") && turnScore>0);
totalScore = totalScore + turnScore;
}
System.out.print("Game total: " + totalScore);
}
}
static int roll()
{
return 1 + (int)(6*Math.random());
}
}
Explanation / Answer
please rate - thanks
any questions ask
import java.util.Scanner;
public class Pig {
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);
int totalScore = 0;
for (int turn = 1; turn <=5; turn++)
{totalScore=totalScore+playTurn(turn,scan);
}
System.out.print("Game total: " + totalScore);
}
public static int roll()
{
return 1 + (int)(6*Math.random());
}
public static int playTurn(int turn,Scanner scan)
{
System.out.println();
System.out.println("Turn: " + turn);
int turnScore = 0;
do
{int rollScore = roll();
if (rollScore == 1)
{ turnScore = 0;
}
else
{ turnScore = turnScore + rollScore;
}
printRollResult(rollScore,turnScore);
} while (isHold(scan) );
return turnScore;
}
public static void printRollResult(int rollValue, int turnTotal)
{System.out.println(" Roll Value: " + rollValue);
System.out.println(" Turn total: " + turnTotal);
}
public static boolean isHold(Scanner scan)
{System.out.print(" 1) Roll again or 2) Hold? ");
String response = scan.nextLine();
if(response.equals("1"))
return true;
else
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.