Results 1 to 4 of 4
  1. #1
    vincentdotts is offline Member
    Join Date
    Mar 2015
    Posts
    1
    Rep Power
    0

    Default 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();
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Program terminates when i try to run it

    it automatically terminates but doesnt give me an error.
    Try debugging the code to see what it is doing. Add lots of println statements to show where it is executing and what the values of variables are as they are used and changed. The print out will help you find the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Buckeye is offline Member
    Join Date
    Mar 2015
    Posts
    1
    Rep Power
    0

    Default Re: Program terminates when i try to run it

    Have you figured this out yet?

    A question to ask yourself is what does Java initialize variables of primitive type int to as in your private int ans?
    The println suggestion above should help if you inspect what ans, num[0] and num[1] (Line 27) when you start createQuestion().


    So, this is an issue in design logic as opposed to code.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Program terminates when i try to run it

    Quote Originally Posted by Buckeye View Post
    A question to ask yourself is what does Java initialize variables of primitive type int to as in your private int ans?
    Good response, but the skunk who asked the question didn't bother to tell us it was already answered in Random Multiplication Class - Java | Dream.In.Code

    Didn't bother to thank andrewsw there either.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 7
    Last Post: 03-05-2013, 09:19 AM
  2. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  3. Replies: 3
    Last Post: 03-13-2012, 09:28 AM
  4. My program terminates before letting me enter an answer
    By soradogoof in forum New To Java
    Replies: 2
    Last Post: 09-22-2011, 01:01 AM
  5. Application automatically terminates
    By PaulT in forum Eclipse
    Replies: 4
    Last Post: 03-18-2009, 05:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •