Friday, March 6, 2015

Program terminates when i try to run it
































































I have to create a class that asks the user to answer a multiplication problem. It randomly pulls to integers. If the answer is correct it displays a response if its wrong it displays a response and tells them to try again. at the end it displays all the questions with the answers. For some reason when i run the program it automatically terminates but doesnt give me an error. Any help would be appreciated. Thank you
































































































Java Code:






































































import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

//import javax.swing.*;

public class StudentMath {

private int num[] = new int[2]; // array of numbers for random numbers
private int ans;
private boolean right = true;
private boolean wrong = false;
private int arr[] = new int[2];
private ArrayList<String> results = new ArrayList<String>(); // array of random questions
private String choice;
Random r = new Random(); //instantize Random
Scanner sc = new Scanner(System.in);// instantize Scanner

public void generateQuestion() {//Method creates 2 random numbers
arr[0] = r.nextInt(9);
arr[1] = r.nextInt(9);
num = arr;
}

public void createQuestion() { //Method creates question using random integers

while (ans != (num[0] * num[1])) {
generateQuestion();
do {

System.out.print("\nHow much is " + num[0] + " times " + num[1]
+ " ? :");
ans = sc.nextInt();
results.add(num[0] + " times " + num[1] + "=" + num[0] * num[1]);//adds to result array
checkAnswer();
System.out.print("\nDo you want more questions(y/n) :");
sc.nextLine();
choice = sc.nextLine();

} while (choice.equalsIgnoreCase("y"));
}
}
public void checkAnswer(){ //checks if user input is correct
if (ans == (num[0] * num[1]))
createResponse(right);
else
createResponse(wrong);
}

public void createResponse(boolean b) { //generates response if right or wrong answer

final String[] correct = { "Very good!", "Excellent!", "Nice work!",
"Keep up the good work!" };
final String[] wrong = { "No. Please try again",
"Wrong. Try once more", "Don't give up!", "No. Keep trying" };
int index;

if (b == true) {
index = r.nextInt(correct.length);
System.out.print(correct[index]);
} else {
index = r.nextInt(wrong.length);
System.out.print(wrong[index]);
}
}

public void printResults() { //prints results of questions asked
String output = "";
for (int i = 0; i < results.size(); i++) {
String everything = results.get(i).toString();
output += everything + "\n";
}
System.out.print(output);
sc.close();
}

public static void main(String args[]) {

StudentMath sm = new StudentMath(); //instantize class StudentMath

sm.createQuestion();

sm.printResults();
}
}



































































































































No comments:

Post a Comment