Question

In: Computer Science

HOW CAN I USE a string instead of array tries and res on this assignment, with...

HOW CAN I USE a string instead of array tries and res on this assignment, with out impacting the program or modifying too much on conditions  check code bellow

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

}

}

}

Solutions

Expert Solution

package Game;

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;
   // use arrays to store number of tries in each life
   public static String tries;
   public static String res;
  
   public static String getName(String aString)
   {      
       Scanner sc = new Scanner(System.in);
       res = "";
       tries = "";
      
       System.out.print("\n 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.print("\n 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.print("\n 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.print("\n Congratulation you are right.");
                   int val = 8 - rounds;
                   tries += val + "\t";
                   res += "Success" + "\t";
                   rounds = 8;
                   break;
               }
               else
               {
                   System.out.print("\n You have " + (rounds) + " remaining.");
               }
               if(rounds == 0)
               {
                   if(lives != 4)
                   {
                       tries += "8" + "\t";
                       res += "Fail" + "\t";
                       lives++;
                       System.out.print("\n Hard luck \n You 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.print("\n Game No Game 1 Game 2 Game 3 Game 4");
                   System.out.print("\n Number of tries ");
                   printed = 1;
                   //for(int i = 0; i < 4; i++)
                   System.out.print(tries + "\t");
                   System.out.println();
                   System.out.print("\n Result \t");
                   //for(int i = 0; i < 4; i++)
                   System.out.print(res + "\t");
                      
                   System.out.println("\n Bye bye " + playerName + " !!");
               break;
           }
           if(lives > 4)
               break;
       }
       if(printed != 1)
       {
           System.out.print("\n Game No Game 1 Game 2 Game 3 Game 4");
           System.out.print("\n Number of tries ");
           for(int i = 0; i < 4; i++)
               System.out.print(tries + "\t");
           System.out.println();
           System.out.print("\n Result\t");
           for(int i = 0; i < 4; i++)
               System.out.print(res + "\t");
           System.out.println("\n Bye bye " + playerName + " !!");
       }
   }
}

Sample Output:

Enter player's Name: Pyari
Make a guess from 1 - 20: 2

You have 7 remaining.
Make a guess from 1 - 20: 3

You have 6 remaining.
Make a guess from 1 - 20: 4

You have 5 remaining.
Make a guess from 1 - 20: 5

You have 4 remaining.
Make a guess from 1 - 20: 6

You have 3 remaining.
Make a guess from 1 - 20: 11

You have 2 remaining.
Make a guess from 1 - 20: 22

You have 1 remaining.
Make a guess from 1 - 20: 33

You have 0 remaining.
Hard luck
You have 3 lives left.
Do you want to continue?(y/Y):
(x/X) to exit. y

Make a guess from 1 - 20: 1

You have 7 remaining.
Make a guess from 1 - 20: 2

You have 6 remaining.
Make a guess from 1 - 20: 3

You have 5 remaining.
Make a guess from 1 - 20: 4

You have 4 remaining.
Make a guess from 1 - 20: 5

You have 3 remaining.
Make a guess from 1 - 20: 6

You have 2 remaining.
Make a guess from 1 - 20: 7

You have 1 remaining.
Make a guess from 1 - 20: 8

You have 0 remaining.
Hard luck
You have 2 lives left.
Do you want to continue?(y/Y):
(x/X) to exit. y

Make a guess from 1 - 20: 1

You have 7 remaining.
Make a guess from 1 - 20: 10

Congratulation you are right.
Do you want to continue?(y/Y):
(x/X) to exit. y

Make a guess from 1 - 20: 1

You have 7 remaining.
Make a guess from 1 - 20: 5

You have 6 remaining.
Make a guess from 1 - 20: 6

You have 5 remaining.
Make a guess from 1 - 20: 13

Congratulation you are right.
Do you want to continue?(y/Y):
(x/X) to exit. x

Game No Game 1 Game 2 Game 3 Game 4
Number of tries 8   8   2   4      

Result    Fail   Fail   Success   Success      
Bye bye aa !!


Related Solutions

how can I save a character stack to a string and then save that string into...
how can I save a character stack to a string and then save that string into a new string arraylist in java? so if the character stack is h, e, l, l, o i want it to save "hello" to a string and then put that string into an array list.
This assignment requires you to use at least one loop and an array. You can refer...
This assignment requires you to use at least one loop and an array. You can refer to our book and any programs you have written so far. Make sure that your work is your own. Write a comment at the beginning of your program with your name and your birthday (month and day, does not need to include year). For example: John Doe, May 23 Create an array that will store whole numbers. The size of the array should be...
This assignment requires you to use at least one loop and an array. You can refer...
This assignment requires you to use at least one loop and an array. You can refer to our book and any programs you have written so far. Make sure that your work is your own. Write a comment at the beginning of your program with your name and your birthday (month and day, does not need to include year). For example: John Doe, May 23. Create an array that will store whole numbers. The size of the array should be...
Create and initialize a string array, write and use 3 functions.Write a program. Create a string...
Create and initialize a string array, write and use 3 functions.Write a program. Create a string array in the main(),called firstNameArray, initialize with7 first namesJim, Tuyet, Ann, Roberto, Crystal, Valla, Mathilda Write a first function, named searchArray, that passes two arguments: a single person’s name and the array reference, into a function,This function should then search the array to see if the name is in the firstNameArray. If found, it will return the array index where the name is found,...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if we want! Choice either vectors or arrays either one is fine We will implement a classic Inheritance hierarchy. A simple console based interface is all that is needed. Build your classes first, each in their own .h and .cpp files, then test them with the simple main method provided below. Phase 1 : Here is the following set of classes you will implement and...
How can i bubble sort a sentence in a char array in c++ This is the...
How can i bubble sort a sentence in a char array in c++ This is the prototype of the function: char* sort(char string[], int numOfWords, int lengthOfWord); This is the testing code in the main file: char words[] = "CAT FAT BAT HAT RAT"; printf("Before sort: t%s\n";words); char result = sort(words; 5; 3); printf("After sort : t%s\n"; result); Expected output: Before sort: CAT FAT BAT HAT RAT After sort: BAT CAT FAT HAT RAT
Write a class to implement HeadTailListInterface. Instead of using an array, use a List object as...
Write a class to implement HeadTailListInterface. Instead of using an array, use a List object as your instance data variable. (List (Links to an external site.) from the Java standard library- not ListInterface!). Instantiate the List object to type ArrayList. Inside the methods of this class, invoke methods on the List object to accomplish the task. Note: some methods might look very simple... this does not mean they are wrong! There is one difference in how this class will work...
In building a regression tree, instead of the mean we can use the median, and instead...
In building a regression tree, instead of the mean we can use the median, and instead of minimizing the squared error we can minimize the absolute error. Why does this help in the case of noise?
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array...
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array of single        words which are then are edited based on the command        Possible Commands:        remove: for the sent words, remove those from the text        replace: replace the word in an even element of words with a word in an odd element of words        add: add the word given word in the index indicated after...
How can I check that if a string only make from specific character? For example, I...
How can I check that if a string only make from specific character? For example, I want to check if a string only make from ICDM characters. It pass the test if it makes from ICMD. It fail the test if it makes from any special character like @#$& or lower case. Example inputs: if string = CIM, it passed if string = AIM, it failed if string = MCDI, it passed if string = ICDF, it failed This can...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT