In: Computer Science
Based upon the following code (or writing your own from scratch)
Do either of the following two: Moderately Detailed or Bare-Bones
MODERATELY DETAILED |
BARE-BONES |
import java.util.Random; import java.util.Scanner; //end goal/aim/ask; WAP high and low game //user choice H or L; and //single round //dice roll: generate a random number 0 and 20 //high is 11 to 20 and low is 1 to 10 public class Main { public static void main( String[] args ) { int score = 0; score += playRound(); gameResult( score ); }
public static int playRound() { // Create instance of Random class Random randGenerator = new Random();
// Generate random integers in range 1 to 20 int diceRoll = <TODO>; // Create a Scanner object Scanner myInput = new Scanner(System.in); //Acquire user input <TODO>; // Compare random number to user input // If the user won if( <TODO> ) { System.out.println( "Point awarded!" ); return 1; } // Otherwise else { System.out.println( "No point awarded..." ); return 0; } }
public static void gameResult( int score ) { // <TODO> - Report result based on user's score } } |
public class Main { public static void main( String[] args ) { // Play a round of the dice game <TODO> // Report the results <TODO> }
public static int playRound() { // Generate a random number <TODO> //Acquire user input <TODO>; // Compare random number to user input <TODO> // Return a score <TODO> } } |
Finalize the game logic to generate a working game.
Paste your code below:
Paste your output after playing 5 rounds:
//MODERATELY DETAILED
public class Main()
{
public static void main(String[] args) {
// TODO code application logic here
int i=0;
int score=0;
while(i<5)//for playing 5 time or you take input from user too
for how many times the //want to play
{
score += playRound();
i++;
}
gameResult( score );
}
public static int playRound() {
Random randGenerator=new Random();
int diceRoll
=randGenerator.nextInt(20-1+1)+1;//random.nextInt(max-min+1)+min(for
generating random number
//System.out.println(diceRoll);
Scanner myInput =new Scanner(System.in);
char ch;
System.out.print("Please enter you choice H for high L for low
");
ch=myInput.next().charAt(0);//taking input from user whether high
or low
//check if ch is Low and diceRoll value is low or not not.
//if ch is High whether diceRoll is high or not
if((ch=='L'&&diceRoll>=1&&diceRoll<=10)||(ch=='H'&&diceRoll>=11&&diceRoll<=20))
{System.out.println("Points awarded!");
return 1;
}
else
{
System.out.println("No points awarded!");
return 0;
}
}
public static void gameResult(int score)
{
System.out.println("Your score is "+score);
}
}
//Hope you like my answer please leave good rating thank you