Reputation:
I have a program that will help the user to learn a multiplication table and then show results of right/wrong answers. The first step is to simply ask the user for which multiplication table it want to work on (1-9). And then the user will get a random sequence of number multiplied by the chosen multiplication table. If the user answers correctly then that number won't be shown again, but if incorrectly then it will be shown until the correct answer is made.
One scenario could be that the user chooses "3", and it will then be displayed in a random sequence such as (3x7 =, 3x1 =, 3x9 =...). And the user will answer after each "=". Right now, I can only print it all in ascending order, should I use Random multiplied with the chosen table in a while loop instead?.
My second issue, is how I can ask the incorrectly answered numbers again, until correctly answered? Am I right to think that a for loop isn't the best choice in this case?
Here is my code so far:
public class Multiplication {
public static void main (String[] args) {
Scanner inread = new Scanner (System.in);
int answer;
System.out.println("Choose multiplication table (1-9)");
int num1= inread.nextInt();
for (int i=1; i<11; i++) {
System.out.println("Write answer after = ");
System.out.println(num1 + " x " + (i) + " = ");
answer=inread.nextInt();
if (answer == (num1 * i) ) {
System.out.println("Correct answer");
// Do not show that number again
}
else {
System.err.println("Wrong answer");
//Show this number again.
}
}
}
}
New code after int num1 = inread.nextInt();
unanswered.add(1);
unanswered.add(2);
unanswered.add(3);
unanswered.add(4);
unanswered.add(5);
unanswered.add(6);
unanswered.add(7);
unanswered.add(8);
unanswered.add(9);
unanswered.add(10);
Collections.shuffle(unanswered);
while (!unanswered.isEmpty()) {
System.out.println(num1 + "*" + "unanswered" + " = "); //?
answer = inread.nextInt();
if (answer == (num1 * unanswered)) { //?
unanswered.remove(unanswered); //?
}
}
So, I think this is almost the way you suggested? However I'm sure I could add the numbers in a more beautiful way. I am used to looping through lists with a for loop in order to then use the counter to display the list. So where I putted a "?" is because I am not sure how to specify where in the list I am trying, for example to remove a number.
Or should I have the while loop, inside the for loop that I originally had? So that I could use the (i) in the for loop to specify where in the list I will display and perhaps remove?
Upvotes: 0
Views: 456
Reputation: 1061
A good question and a good start on the coding.
One way of asking for input until all the multiplication questions have been solved would be a while
loop.
As @Easton pointed out an ArrayList to store the numbers and Collections.shuffle will help with the setup. By creating the ArrayList ahead of time then using a while loop until it is empty to prompt the user to keep answering.
EDIT
Heading in the right direction. To simplify the creation of unanswered numbers make use of the for loop, Something like: for(i=1, i<=10,i++)
then add(i) to unanswered.
In the while loop, grab the first index: unanswered[0]
and set that to num1
then if the answer is correct, remove it (as you have now). If not use Collections.rotate on unanswered by 1. Which will move the unanswered question to the end of the array for another attempt later.
Upvotes: 1
Reputation: 102
Bellow you can find a solution for your problem with Hash Tables, you can do modifications to it so that the user can not type a number larger than 9 or smaller than 0 but this should work for your purpose:
import java.util.Random;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Multiplication {
public static void main (String[] args) {
Map<Integer, Integer> list = new HashMap<Integer, Integer>();
Scanner inread = new Scanner (System.in);
int answer;
System.out.println("Choose multiplication table (1-9)");
int num1= inread.nextInt();
for (int i = 1; i<10; i++) {
list.put(i, num1*i);
}
System.out.println(list);
while (!list.isEmpty()) {
System.out.println("Write answer after = ");
Random generator = new Random();
Object[] keys = list.keySet().toArray();
Object randomValue = keys[generator.nextInt(keys.length)];
int next = (Integer) randomValue;
System.out.println(num1 + " x " + (next) + " = ");
answer=inread.nextInt();
if (answer == (num1 * next)) {
System.out.println("Correct answer");
list.remove(next);
} else {
System.err.println("Wrong answer");
}
}
System.out.println("Congrats!!!");
}
}
Upvotes: 0