In: Computer Science
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+" !!");
}
}
}
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 !!