We help IT Professionals succeed at work.

# Arrays

on
209 Views
Trying to make this program multi-player. At the beginning of the main method I want to prompt for number of players. Then create an array of this size to hold doubles. This will hold each players percentage correct.
Using a for loop around the rest of the main method to loop as many times as there are players. Just before the end of the for loop  store that players correct percentage in the array. After the for loop  at the end of the main method put another for loop that reads the array and displays the percentages for each player.

I'm also wondering why my non-numeric data entry code doesn't work properly once you select an option and after going to the main screen if you enter non-numeric data it goes straight to the addition screen.

Code so far without arrays.

import javax.swing.JOptionPane; // Needed for JOptionPane
import java.util.Random;

public class KidsCalc
{
//Initialise result & count to 0
public static int result = 0;
public static int count = 0;
public static int correct = 0;

public static void main(String[] args)
{

int userChoice = 0;
double playerChoice = 0;
String userChoiceStr;
String playerChoiceStr;

playerChoiceStr = JOptionPane.showInputDialog(null,
"How many players?");
playerChoice = Double.parseDouble(playerChoiceStr);

do
{
// user input prompt
userChoiceStr =  JOptionPane.showInputDialog(null,

+ "2. Practice subtraction\n"
+ "3. Practice multiplication\n"
+ "4. Quit the program\n"
+ "\n"

JOptionPane.QUESTION_MESSAGE);
// keeps track of wrong entry
try
{
userChoice = Integer.parseInt(userChoiceStr);
}
catch (NumberFormatException e)
{
}
// switch statement to count user score
switch (userChoice)
{
case 1:
++count;
break;
case 2:
++count;
subtraction ();
break;
case 3:
++count;
multiplication ();
break;
case 4:
break;
default:
// prompt if user doesn't enter a number between 1-4
JOptionPane.showMessageDialog(null,
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE);
}
}
// returns your percentage if input not equal to 4
while ( userChoice != 4 );

JOptionPane.showMessageDialog ( null ,"You got " +
(result / count) + "% right!!"  );

// Ends program
System.exit(0);
}

{
boolean isNum = true;
int genNumOne, genNumTwo;

// generates 2 random numbers between 0 and 9
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;

selectAddition = JOptionPane.showInputDialog("What is" + " " + genNumOne +
" " + "plus" + " " + genNumTwo + "?");
try
{

}
catch (NumberFormatException e)
{
isNum = false; //keeps track of wrong entry
}

if ( additionData == (genNumOne + genNumTwo) )
{

result = result + 100;
}
else
if (isNum == true)
{

}
else // if we have the wrong entry
{
JOptionPane.showMessageDialog(null,
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE);

isNum = true;
}
}

public static void subtraction()
{
boolean isNum = true;
int subtractionData = 0;
int genNumOne, genNumTwo;
String selectSubtraction;

genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * genNumOne) + 0;
// makes sure first random number is greater than second

selectSubtraction = JOptionPane.showInputDialog("What is" + " " + genNumOne +
" " + "minus" + " " + genNumTwo + "?");
try
{
subtractionData = Integer.parseInt(selectSubtraction);

}
catch (NumberFormatException e)
{
isNum = false; // keeps track of wrong entry
}

if ( subtractionData == (genNumOne - genNumTwo) )
{

result = result + 100;
}
else
if (isNum == true)
{

}
else // if we have the wrong entry
{
JOptionPane.showMessageDialog(null,
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE);

isNum = true;
}
}

public static void multiplication()
{
boolean isNum = true;
int multiplicationData = 0;
int genNumOne, genNumTwo;
String selectMultiplication;

genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;

selectMultiplication = JOptionPane.showInputDialog("What is" + " " + genNumOne +
" " + "times" + " " + genNumTwo + "?");
try
{
multiplicationData = Integer.parseInt(selectMultiplication);

}
catch (NumberFormatException e)
{
isNum = false; // keeps track of wrong entry
}

if ( multiplicationData == (genNumOne * genNumTwo) )
{

result = result + 100;
}
else
if (isNum == true)
{

}
else // if we have the wrong entry
{
JOptionPane.showMessageDialog(null,
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE);

isNum = true;
}
}

{

String[] arrGood = { "Very good!",  "Well done", "Great!" };
Random randGood = new Random();

int new_rand_index = randGood.nextInt( arrGood.length );
String message = arrGood[ new_rand_index ];
JOptionPane.showMessageDialog(null, message,
JOptionPane.INFORMATION_MESSAGE);

}

{

String[] arrBad = {  "Sorry that was incorrect.", "Better luck next time",  "Bad luck" };

String message = arrBad[ new_rand_index ];
JOptionPane.showMessageDialog(null, message,
JOptionPane.INFORMATION_MESSAGE);

}
}

Comment
Watch Question

## View Solution Only

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)

Commented:
Is there another way to stop non-numeric data being entered without try/catch block?

Commented:
I'm stuck on the array.

Commented:
OK this is what I have.

I still have a few bugs like when I press cancel i get a random error, and say you have 2 players.. the first player when you press 4 to quit and get score it comes up with another error but then works fine for the 2nd player. My main problem is with my menu not showing what player is now currently playing in the menu..all i have is the number of players showing in menu.

import javax.swing.JOptionPane;
import java.util.Random;
import java.util.Vector;

public class Wtf
{
//Initialise result & count to 0
public static int result = 0;
public static int count = 0;

public static void main(String[] args)
{
int  i=0;
int userChoice = 0;
double playerChoice = 0;
String userChoiceStr;
String playerChoiceStr;
//    Vector v = new Vector ();
double v[] = new double[50];

playerChoiceStr = JOptionPane.showInputDialog(null,
"How many players?");
playerChoice = Double.parseDouble (playerChoiceStr);

for (i =0; i < (int)playerChoice ; i++)
{

do
{
// user input prompt
userChoiceStr =  JOptionPane.showInputDialog (null,

"Player" + " " + playerChoiceStr  + "\n" +

+ "2. Practice subtraction\n"
+ "3. Practice multiplication\n"
+ "4. Quit the program\n"
+ "\n"

JOptionPane.QUESTION_MESSAGE);
// keeps track of wrong entry

userChoice = Integer.parseInt(userChoiceStr);

// switch statement to count user score
switch (userChoice)
{
case 1:
++count;
break;
case 2:
++count;
subtraction ();
break;
case 3:
++count;
multiplication ();
break;
case 4:
break;
default:
// prompt if user doesn't enter a number between 1-4
JOptionPane.showMessageDialog(null,
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE );
}
} while ( userChoice != 4 );  // returns your percentage if input not equal to 4

String str = "Got " + (result / count) + "% right!! \n";
v[i]= (result/count);
System.out.println ( v[i]);
JOptionPane.showMessageDialog ( null , "You " + str );

}

String showAll = "";
i=0;

/*java.util.Enumeration enum = v.elements ();

while ( enum.hasMoreElements())
{
showAll = showAll + "Player "+ ++i +" : " + enum.nextElement () ;
}
*/

for (i =0; i < (int)playerChoice ; i++)
{
showAll = showAll + "Player "+ (i+1) +" : got " + v[i] +" % right!! \n";
}

JOptionPane.showMessageDialog ( null , showAll );

// Ends program
System.exit(0);
}

{

int genNumOne, genNumTwo;

// generates 2 random numbers between 0 and 9
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;

selectAddition = JOptionPane.showInputDialog ("What is" + " " + genNumOne +
" " + "plus" + " " + genNumTwo + "?");

if (additionData == (genNumOne + genNumTwo) )
{
result = result + 100;

}
else
{

}
}

public static void subtraction()
{

int subtractionData = 0;
int genNumOne, genNumTwo;
String selectSubtraction;

genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) ( Math.random() * genNumOne) + 0;
// makes sure first random number is greater than second

selectSubtraction = JOptionPane.showInputDialog("What is" + " " + genNumOne +
" " + "minus" + " " + genNumTwo + "?");

subtractionData = Integer.parseInt(selectSubtraction);

if (subtractionData == (genNumOne - genNumTwo) )
{
result = result + 100;

}
else
{

}
}

public static void multiplication()
{

int multiplicationData = 0;
int genNumOne, genNumTwo;
String selectMultiplication;

genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;

selectMultiplication = JOptionPane.showInputDialog("What is" + " " + genNumOne +
" " + "times" + " " + genNumTwo + "?");

multiplicationData = Integer.parseInt(selectMultiplication);

if ( multiplicationData == (genNumOne * genNumTwo) )
{
result = result + 100;

}
else
{

}
}

{

String[] arrGood = { "Very good!",  "Well done", "Great!" };
Random randGood = new Random();

int new_rand_index = randGood.nextInt( arrGood.length );
String message = arrGood[ new_rand_index ];
JOptionPane.showMessageDialog(null, message,
JOptionPane.INFORMATION_MESSAGE);

}

{

String[] arrBad = {  "Sorry that was incorrect.", "Better luck next time",  "Bad luck" };

String message = arrBad[ new_rand_index ];
JOptionPane.showMessageDialog(null, message,
JOptionPane.INFORMATION_MESSAGE);

}
}

Commented:
Ah lol, i miss typed a variable name.

Commented:
Nope I was wrong, still not working..

Commented:
Works now just by adding "Player" + " " + (i+1)

Commented:
We're in very different time zones, so I wasn't around when you were working your way through further issues. Glad you got it sorted out.

Unlock the solution to this question.