Question: Modify your Java program from last week in jGrasp to: - expand the existing one-dimensional array to a two-dimensional array that is 10 rows X
Modify your Java program from last week in jGrasp to:
- expand the existing one-dimensional array to a two-dimensional array that is 10 rows X 3 columns - generate a random number between 0 and 2 for each player and store the player's name in the column specified by the random number - use a switch statement on the random number when storing the name in the array - in the other loop, display the contents of the two-dimensional array
import java.util.Scanner;
//Class:InputVal
public class newInputVal {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
String nameArray[] = new String[10];
//generate fiveSpaces String String fiveSpaces = ""; for(int j= 1; j <= 5; j++){ fiveSpaces = fiveSpaces+" "; }
//initialize all rows of nameArray to five spaces for(int i =0;i < nameArray.length; i++){ nameArray[i] = fiveSpaces;
}
//run the game for 10 times using a for loop
for(int run = 1; run<=10; run++){
String isPlaying = "n"; String name = ""; //keep looping until user enter either y or n or Y or N
do{
System.out.println("Do you want to play?"); isPlaying = keyboard.nextLine().toLowerCase(); if(!isPlaying.equalsIgnoreCase("n") && !isPlaying.equalsIgnoreCase("y")){ System.out.println("Please entery or n or Y or N");
}
} while(!isPlaying.equalsIgnoreCase("n") &&
!isPlaying.equalsIgnoreCase("y")); if(isPlaying.equalsIgnoreCase("y")){ System.out.println("What is your first name?"); name = keyboard.nextLine(); System.out.println("Your first name is "+ name); nameArray[run-1] = name; //populate the name array
} else{
}
}
keyboard.close(); //print the name of users who are playing System.out.println("The users name who are playing are:"); for(int i =0;i < nameArray.length; i++){ //the array element which are still not containing //fiveSpaces, are basically the user who are playing if(!nameArray[i].equals(fiveSpaces)){ System.out.println(nameArray[i]);
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
