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