Question

In: Computer Science

in this code I have used array two times . I need a way to make...

in this code I have used array two times .

I need a way to make the program functional using a string to store the results and print it again later .

this game should be array free.

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

}

}

}

implementing it

Solutions

Expert Solution

import java.util.Scanner;
import java.util.Random;
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];
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

c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
I have a problem with the code for my project. I need the two clients to...
I have a problem with the code for my project. I need the two clients to be able to receive the messages but the code I have done only the server receives the messages. I need the clients to be able to communicate with each other directly not throught the server. The requirements of this "local chat" program must meet are: 1. The chat is performed between 2 clients and a server. 2. The server will first start up and...
i need the whole html code Make a layout template that contains a header and two...
i need the whole html code Make a layout template that contains a header and two paragraphs. Use float to line up the two paragraphs as columns side by side. Give the header and two paragraphs a border and/or a background color so you can see where they are.
For c++ Write the code to initialize an array such that each element gets two times...
For c++ Write the code to initialize an array such that each element gets two times the value of its index (e.g. 0, 2, 4, 6, …)
I need to make changes to code following the steps below. The code that needs to...
I need to make changes to code following the steps below. The code that needs to be modified is below the steps. Thank you. 1. Refactor Base Weapon class: a.            Remove the Weapon abstract class and create a new Interface class named WeaponInterface. b.            Add a public method fireWeapon() that returns void and takes no arguments. c.             Add a public method fireWeapon() that returns void and takes a power argument as an integer type. d.            Add a public method activate()...
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work...
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work ?
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
I need to be able to store 5000 names into an array. I currently have a...
I need to be able to store 5000 names into an array. I currently have a code that generates one random name. I just need to generate an array of 5000 the same way. #include <cstdlib> #include <ctime> #include <iostream> #include <iomanip> using namespace std; string RandomFirstGen(int namelength); int main(){ srand(time(NULL)); int namelength = rand()%(16-8+1)+8; cout<<"name is: "<<RandomFirstGen(namelength)<<endl; return 0; } string RandomFirstGen(int namelength){ for (int i=0; i<=5000 ; i++){ const int MAX = 26; char alphabet[MAX] = { 'a',...
I need to take the code i already have and change it to have at least...
I need to take the code i already have and change it to have at least one function in it. it has to include one function and one loop. I already have the loop but cant figure out how to add a function. I thought i could create a funciton to call to the totalCost but not sure how to do it. Help please. #include #include //main function int main(void) {    char userName [20];    char yesOrNo [10];   ...
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT