2 Replies - 423 Views - Last Post: 02 February 2013 - 07:34 AM Rate Topic: -----

#1 Jpopto  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 04-April 12

For Loop never stops

Posted 31 January 2013 - 08:48 PM

My for loop never ends, I don't understand.

 for (int count = 0; count < NUM_QUESTIONS; count++)
		   {
		 //Generate two random singal digit numbers
		   num1 = gen.nextInt(RANGE);
		   num2 = gen.nextInt(RANGE);

		 //Display the equation and input user answer
		   System.out.print("\n\n\n\n\t\t\t\t\t\t\t\t" +
		                                   num1 + " + " + num2 + " = ");
		   answer = Keyboard.readInt();

		 //IF (answer is correct)
		   if (answer == num1 + num2)
			{

		 // Display Correct
		   System.out.println ( " CORRECT  " );

		 //Add 1 to number correct
		   numCorrect++;

		}

		 // ELSE Display answer is Incorrect
			else
		 	 {
				 System.out.println ( " INCORRECT " );
			 }


		}


I'm trying to get the next line of code after that statement to output but it's not working. I appreaciate any help.

public static void main (String [] args)
   {
       import java.util.Random;

	public class MathTest
	{
	  //local constants
	    final int RANGE         = 10;    //# of diff values to create
	    final int NUM_QUESTIONS = 5;
	    final int QUIT          = 2;

	  //local variables
	    int num1;            		  // 1st number
	    int num2;   				  // 2nd number
	    int answer;                   // answer to the addition problem
	    int userSelection;            // user selects to take test or to quit
	    int numCorrect;               // number of correct answers
	    int testScore;                // test score of user in percentage
        Random gen = new Random();    // Random class to generate random numbers

			   Library myLib = new Library ();

		 //Display test menu and input user choice
		   System.out.print("\n\n                          Simple Math Tests Made Easy\n\n" +
		                            "                                1. Take a Test\n" +
		                            "                                2. Quit\n\n" +
		                            "                                Enter Choice: ");
		   userSelection = Keyboard.readInt();

		 //WHILE (user selection is take test)
		   while(userSelection != QUIT)

		         {

		 //Clear the screen
		   myLib.clrscr();

		 //Init number correct to 0
		   numCorrect = 0;

		 //FOR (Each of the 5 questions)
		   for (int count = 0; count < NUM_QUESTIONS; count++)
		   {
		 //Generate two random singal digit numbers
		   num1 = gen.nextInt(RANGE);
		   num2 = gen.nextInt(RANGE);

		 //Display the equation and input user answer
		   System.out.print("\n\n\n\n\t\t\t\t\t\t\t\t" +
		                                   num1 + " + " + num2 + " = ");
		   answer = Keyboard.readInt();

		 //IF (answer is correct)
		   if (answer == num1 + num2)
			{

		 // Display Correct
		   System.out.println ( " CORRECT  " );

		 //Add 1 to number correct
		   numCorrect++;

		}

		 // ELSE Display answer is Incorrect
			else
		 	 {
				 System.out.println ( " INCORRECT " );
			 }


		}

			System.out.print ( " Hey " );

		      }



		   } //end main method

		} //end MathTest


Is This A Good Question/Topic? 0
  • +

Replies To: For Loop never stops

#2 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: For Loop never stops

Posted 31 January 2013 - 09:06 PM

Thats because you have your for loop inside of your
while(userSelection != QUIT)
loop, which will never terminate because you never ask for userSelection again.
Was This Post Helpful? 1
  • +
  • -

#3 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: For Loop never stops

Posted 02 February 2013 - 07:34 AM

Howdy friend; I was looking at your code and tweaked it a tad. However; it is still not fixed, it is close however. If you take the previous criticism you should be able to complete the code.

import java.util.*;
import java.io.*;
public class MathTest
{
	static Scanner console = new Scanner(System.in);
	public static void main (String [] args)
   {
	  //local constants
	    final int NUM_QUESTIONS = 5;
	    final int QUIT          = 2;

	  //local variables
	    int num1, num2, answer, userSelection, numCorrect, testScore;


		 //Display test menu and input user choice
		 System.out.print("\n\nSimple Math Tests Made Easy\n\n1). Take a Test\n2. Quit\n\nPlease enter choice: ");
		 
		 //Try to throw exception handler in here
		 userSelection = console.nextInt();

		 //WHILE (user selection is take test)
		   while(userSelection != QUIT)
		   {
		   //Clear the screen
		   try
			{
				if(System.getProperty("os.name").startsWith("Window"))
				{
					Runtime.getRuntime().exec("cls");
				}
				else
				{
				Runtime.getRuntime().exec("clear");
				}
			}
		catch(IOException e)
		{
			for(int i = 0; i < 1000; i++)
			{
				System.out.println();
			}
		}
		 //Init number correct to 0
		   numCorrect = 0;

		 //FOR (Each of the 5 questions)
		   for (int count = 0; count < NUM_QUESTIONS; count++)
		{
		 //Generate two random singal digit numbers
		   num1 = (int)(Math.floor(Math.random() * 11));
		   num2 = (int)(Math.floor(Math.random() * 11));

		 //Display the equation and input user answer
		   System.out.print("\n\n\n\n\t\t\t\t\t\t\t\t" +
		                                   num1 + " + " + num2 + " = ");
										   
			// exception handler
		   answer = console.nextInt();

		 //IF (answer is correct)
		   if (answer == num1 + num2)
			{

		 // Display Correct
		   System.out.println ( " CORRECT  " );

		 //Add 1 to number correct
		   numCorrect++;
		}
		 // ELSE Display answer is Incorrect
			else
		 	 {
				 System.out.println ( " INCORRECT " );
				 continue;
			 }
			 
		}
		System.out.print ( " Hey " );

		}
	} //end main method
} //end MathTest


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1