In: Computer Science
/* I been trying to solve this problem . this is my own coding
/here is the HW INSTRUCTION
Let the user to try of maximum 8 times to find the random number
For every try display if the user find or not the number and the number of tries
At the end of each game display to the user the number of tries to find the number
Allow the user to play a maximum of 4 times, by asking him at the end of the game if he want to play again
<> “Do you want to play again (Yes/No)?
At the end of all games, display for each game the average of number of tries to find the random numbers, as follows:
int n = console.nextInt();
Game No Game 1 Game 2 Game 3 Game 4
Number of tries 5
3
8
8
Result Success Success Fail Success*/
//WHAT I'M STUCK AT IS HOW CAN I POSSIBLY FUNCTION THE GAME FOR SECOND TIME CORRECTLY WHILE KEEPING //CORRECT COUNTING FOR ATTEMPTS AND LIVES
import java.util.Scanner;
import java.util.Random;
public class inputLap
{
public static char roller;public static String playerName; public static int rounds=8,lives=0,randy;
public static String getName(String aString)
{Scanner sc= new Scanner(System.in);
System.out.println("enter player's Name:");aString=sc.next();
playerName=aString;
return playerName;
}
public static char menu()
{Scanner sc= new Scanner(System.in);
if(lives<=4&&rounds>0){
System.out.println("Do you want to continue?(y/Y):\n (x/X) to exit. ");roller=sc.next().charAt(0);
}
return roller;
}
public static int getGame() {
Scanner sc = new Scanner(System.in);
System.out.println("make a guess from 1-20");
int Guessed = sc.nextInt();
return Guessed;
}
public static void main(String[] args) {
getName(playerName); Random r = new Random();
int answer;
do {randy=r.nextInt(20);
answer=getGame();
rounds--;
if(rounds==7&&rounds>0)
{rounds--;
for (int i=0;i<=6;i++)
{
answer=getGame();
if(answer==randy)
{lives++;
System.out.println("congratulation you are right");break;
}
else
{System.out.println("you have "+(rounds+1
)+" remaining");}
switch(rounds){case 0:while(lives!=4){System.out.println("hard luck\nyou have"+(lives-4)+"lives left");
break; }break;}
}
}else if(answer==randy){lives++;System.out.println("congratulation you are right");}
menu();
switch( roller)
{
case 'y':
case 'Y':rounds=8;getGame();answer=getGame();break;
case'x':
case 'X':
lives=5;
System.out.println("bye bye "+playerName+" !!");
break;
}
} while(lives<=4);
}
}
Sample output:
The screenshots are attached below for reference.
Please follow them for proper indentation and output.
Program to copy:
import java.util.Scanner;
import java.util.Random;//starter code provided
public class inputLap
{
public static char roller;
public static String playerName;
public static int printed=0;
public static int rounds=8,lives=0,randy;
public static int tries[]=new int[4];//use arrays to store number
of tries in each life
public static int res[]=new int[4];
public static String getName(String aString){
Scanner sc= new Scanner(System.in);
System.out.println("enter player's Name:");aString=sc.next();
playerName=aString;
return playerName;
}
public static void menu()
{
Scanner sc= new Scanner(System.in);
if(lives<=4){
System.out.println("Do you want to continue?(y/Y):\n (x/X) to exit.
");roller=sc.next().charAt(0);
}
}
public static int getGame() {
Scanner sc = new Scanner(System.in);
System.out.println("make a guess from 1-20");
int Guessed = sc.nextInt();
return Guessed;
}
public static void main(String[] args) {
String name=getName(playerName);
Random r = new Random();
int answer=0;
int f=0;
while(true) {
randy=r.nextInt(20);
for (int i=0;i<=7;i++)
{
answer=getGame();
rounds--;
if(answer==randy)
{
lives++;
System.out.println("congratulation you are right");
tries[lives-1]=8-rounds;
res[lives-1]=1;
rounds=8;
break;
}
else
{
System.out.println("you have "+(rounds)+" remaining");
}
if(rounds==0){
if(lives!=4){
tries[lives]=8;
lives++;
System.out.println("hard luck\nyou have "+(4-lives)+" lives
left");
f=1;
}
}
if(f==1){
f=0;
break;
}
}
menu();
switch( roller)
{
case 'y':
case 'Y':rounds=8;break;
case'x':
case 'X':
lives=5;
System.out.println("Game No Game 1 Game 2 Game 3 Game 4");
System.out.print("Number of tries ");
printed=1;
for(int i=0;i<4;i++)
System.out.print(tries[i]+" ");
System.out.println();
System.out.print("Result \t");
for(int i=0;i<4;i++){
if(res[i]==1)
System.out.print("Success ");
else
System.out.print("Fail");
}
System.out.println("\nbye bye "+playerName+" !!");
break;
}
if(lives>4)
break;
}
if(printed!=1){
System.out.println("Game No Game 1 Game 2 Game 3 Game 4");
System.out.print("Number of tries ");
for(int i=0;i<4;i++)
System.out.print(tries[i]+" ");
System.out.println();
System.out.print("Result\t");
for(int i=0;i<4;i++){
if(res[i]==1)
System.out.print("Success ");
else
System.out.print("Fail");
}
System.out.println("\nbye bye "+playerName+" !!");
}
}
}