Question

In: Computer Science

/* I been trying to solve this problem . this is my own coding /here is...

/* 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);



}

}

Solutions

Expert Solution

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+" !!");

}

}

}


Related Solutions

I am trying to create my own doubly linkedlist class and here is what I have...
I am trying to create my own doubly linkedlist class and here is what I have so far. my addfirst is adding elements forever and ever, and I could use some help. This is in C++. Please help me complete my doubly linkedlist class. the addfirst method is what I would like to complete. #ifndef DLLIST_H #define DLLIST_H #include "node.h" #include using namespace std; template class DLList { private:     Node* head = nullptr;     Node* tail = nullptr;    ...
Here is a Problem I need to solve: 5) Write a function to determine is a...
Here is a Problem I need to solve: 5) Write a function to determine is a given word is legal. A word is illegal if it contains no vowels. For this problem, the letter Y is considered to be a legal vowel. Pass in a word to this function and it will determine if the word is legal or not. You can make the following assumptions about the word you a passing to this function. 1) The string being passed...
I am trying to solve this problem, but I don't know which test to use: For...
I am trying to solve this problem, but I don't know which test to use: For average risk funds only, test the proportion of funds with Sales Charges is not 50% use α = 0.10. Explain your conclusion. Here is the contingency table with the data: Sales charge Yes No total Risk Low 6 8 14 Avg 42 34 76 High 24 23 47 Total 72 65 137 Context of the problem: Mutual funds are the most common way people...
HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
I am trying to solve a c++ problem over c strings where I have to input...
I am trying to solve a c++ problem over c strings where I have to input an email address and check if it is valid. After completing my code I keep getting errors that my program will not run, specifically the lines with my for loops. Can you please look at my code and tell me what is wrong and how I can fix the code? I included below the assignment instructions and my current code. Assignment Instructions Write a...
Please solve this case problem: SunLife insurance has been trying to penetrate the mass market and...
Please solve this case problem: SunLife insurance has been trying to penetrate the mass market and educate them on the benefits and importance of life and other investment insurance. They have used several TV personalities like Charo Santos and Piolo Pascual to penetrate the segment, with limited success. People are aware of the brand, but are still not taking insurance due to various issues like affordability and distribution. Use the Ansoff Matrix and recommend how they can expand their market...
In trying to apply my knowledge in the real world, I am trying to create a...
In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 25 and 70, in which I would deposit 20% of my income each year. The income starts at 80,000 with an annual growth rate of...
Coding in R Im trying to plot a histogram in R, and i know the basic...
Coding in R Im trying to plot a histogram in R, and i know the basic code for that, however, the code requires 2 plots coming from the same data set. for example, the voltage of an old process vs. a new process. is there any ways to seperate the 2 processes out from the data set to make 2 relative frequencyt histograms?
I am coding in MySQL and two of my tables are populating find but the other...
I am coding in MySQL and two of my tables are populating find but the other two "Employee" and "Assignment" won't. I keep getting an error code saying I can't alter a child table and I can't figure out what to fix. Here is my code: CREATE TABLE DEPARTMENT ( DepartmentName Char(35) NOT NULL, BudgetCode Char(30) NOT NULL, OfficeNumber Char(15) Not Null, DepartmentPhone Char(12) NOT NULL, CONSTRAINT DEPARTMENT_PK primary key(DepartmentName) ); CREATE TABLE EMPLOYEE( EmployeeNumber Int NOT NULL AUTO_INCREMENT, FirstName...
Professor, In trying to apply my knowledge in the real world, I am trying to create...
Professor, In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 22 and 68, in which I would deposit 25% of my income each year. The income starts at 80,000 with an annual growth rate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT