Solved

# Initialiser list and arrays

Posted on 2007-10-02
233 Views
Add 2 new methods which are called to display each result ie. Cut the display of the dialog box from the each of the methods addition(), subtraction() and multiplication() and replace then with a call to either of the following static void methods:

To make the program more interesting , each of these two methods should contain an array initialised with a number of responses (an initialiser list) which are selected at random. So if an answer is correct, the response may be

Very good!, Excellent!, You are hot! etc etc.

Similarly for incorrect reponses.

Now the program is multi-player. At the beginning of the main method prompt for number of players

Now create an array of this size to hold doubles. This will hold each players percentage correct.

Now put 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.

Having trouble with the above. I'm more than have way done, and would appreciate some help on getting rest incorporated into my code :).

Here's what I have so far

import javax.swing.JOptionPane; // Needed for JOptionPane

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

public static void main(String[] args)
{

int userChoice = 0;
String userChoiceStr;
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) )
{
JOptionPane.showMessageDialog(null,
"Very good!",
"Well done",
JOptionPane.INFORMATION_MESSAGE);
result  = result + 100;
}
else
if (isNum == true)
{
JOptionPane.showMessageDialog(null,
"Sorry that was incorrect. Better luck next time",
JOptionPane.INFORMATION_MESSAGE);
}
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) )
{
JOptionPane.showMessageDialog(null,
"Very good!",
"Well done",
JOptionPane.INFORMATION_MESSAGE);
result  = result + 100;
}
else
if (isNum == true)
{
JOptionPane.showMessageDialog(null,
"Sorry that was incorrect. Better luck next time",
JOptionPane.INFORMATION_MESSAGE);
}
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) )
{
JOptionPane.showMessageDialog(null,
"Very good!",
"Well done",
JOptionPane.INFORMATION_MESSAGE);
result  = result + 100;
}
else
if (isNum == true)
{
JOptionPane.showMessageDialog(null,
"Sorry that was incorrect. Better luck next time",
JOptionPane.INFORMATION_MESSAGE);
}
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;
}

}

{

}

{

}
}
0
Question by:DeafBox

LVL 20

Accepted Solution

String[] arrGood = { "Very good!",  "Well done", "Great!" };
String[] arrBad = {  "Sorry that was incorrect.", "Better luck next time",  "Bad luck" };

and two random number generators
Random randGood = new Random();

then in correctAnswer() you will pick out a random number between 0 and arrGood.length - 1 and print (in whatever form you need) the message at that array position
int new_rand_index = randGood.nextInt( arrGood.length );
String message = arrGood[ new_rand_index ];
to print the message, for example
JOptionPane.showMessageDialog(null,
message ,
JOptionPane.INFORMATION_MESSAGE);

And then when you need to call this function, you just do something like

if (result == correct)
else
0

Author Comment

Ok from your example I got it working. I think it's right?

I don't know why at my end results I'm getting a random arrBad message :S

Any ideas on how to do the rest of the code for the mutiplayers etc?

/**
* Kids additon, subtraction, multiplication calculator
* that generates 2 random numbers between 0 and 9.
* 21 September 2007
*/

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

if (result == correct)
else
// 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);

}
}
0

LVL 20

Expert Comment

to do the multiplayer, do exactly what the instructions say

-- At the beginning of the main method prompt for number of players
once you have that, do the following step:

-- Now create an array of this size to hold doubles. This will hold each players percentage correct.
(an array of length equal to the number of players). Actually in your case, create 3 arrays (result, count, correct)

--Now put a for loop around the rest of the main method to loop as many times as there are players.
for (player # 1 ... n)
repeat what you have in your main method now for one player

use the arrays that you created before,

--Just before the end of the for loop  store that players correct percentage in the array.
to save the information for that player

--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.
0

## Featured Post

### Suggested Solutions

linearIn  challenge 23 54
Jasper Report: Crosstab Report- Include Page Footer 2 34
array6 challenfge 6 48
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…