7 Replies - 22198 Views - Last Post: 18 February 2007 - 03:19 PM Rate Topic: -----

#1 BudLight412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-January 07

IF ELSE and WHILE LOOPS

Posted 17 February 2007 - 04:03 PM

I cant figure out how to assign my variable "number_right" to the number of questions correct from my if else statement. I am assigning it to the same equation I am using for the true portion of the if else. can someone please help me. I also cant figure out how to make the program repeat itself. What I mean is when the quiz I made is finished I want the program to pop up an input window asking if the person wants to play again, and if the anwer is yes start the loop over again and if the answer is no pop a message box saying the person got so many right out of the entire total questions, then that goes back to my previous question. If there is any help that would be great.

//Importing the neccesary commands
import javax.swing.JOptionPane;
import java.util.Random;

public class Jay2 
{
  public static void main(String [] args)
  {
  
	int ques1;  //First random number
	int ques2;  // Second random number
	int number = 0;  //How many questions input
	int counter = 1;  //while counter
	int ques_sum;  //sum of the two random numbers
	int response;  //your answer
	int number_right = 1;  //number of questions answered correctly
	int number_total = 0;  //number of questions you wanted
	String input;  //Your input for the two numbers
	String message;  //Correct of Wrong massages
	Random questions = new Random();  //Random number selector
	
	
	
	//Asking how many questions 
	input = JOptionPane.showInputDialog("Welcome to Jay's simple addition quiz! \n\nHow many questions would you like?");
	number = Integer.parseInt(input);
	
   
	while(counter <= number)
	{
	  ques1 = (Math.abs(questions.nextInt())%20)+1;
	  ques2 = (Math.abs(questions.nextInt())%20)+1;
	  ques_sum = (ques1+ques2);
	  
	  input = JOptionPane.showInputDialog(ques1+ "+" +ques2);
	  response = Integer.parseInt(input);
	  
	  if(response == (ques_sum))
	  {
	  message =  "Correct!";
	  JOptionPane.showMessageDialog(null, message.toUpperCase());
	  }
	  else
	  {
	  message = "Wrong!";
	   JOptionPane.showMessageDialog(null, message.toUpperCase()); 
	  }
	  ++counter;
	}
	number_right = (response == ques_sum); [THIS IS WHERE MY FIRST PROBLEM IS!!!
	number_total = number;
	
	JOptionPane.showMessageDialog(null, "You correctly answered "+number_right+" out of "+number_total+" questions correctly. \nYour percentage is: "+number_right/number_total+"%");			 
   
	
	System.exit(0);
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: IF ELSE and WHILE LOOPS

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: IF ELSE and WHILE LOOPS

Posted 17 February 2007 - 04:28 PM

You need to increment the number_right variable inside your IF statement.
 if(response == (ques_sum))
	  {
	  message =  "Correct!";

	  number_right++; //increment it here

	  JOptionPane.showMessageDialog(null, message.toUpperCase());
	  }
	  else
	  {
	  message = "Wrong!";
	   JOptionPane.showMessageDialog(null, message.toUpperCase());
	  }
	  ++counter;
	}
	
	number_total = number;


Also you need to declare it with 0, not 1. As no questions have been answered correctly until the first time through the loop.
int number_right = 0;  //number of questions answered correctly

Was This Post Helpful? 0
  • +
  • -

#3 BudLight412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-January 07

Re: IF ELSE and WHILE LOOPS

Posted 17 February 2007 - 04:52 PM

[quote name='jayman9' date='17 Feb, 2007 - 04:28 PM' post='203377']
You need to increment the number_right variable inside your IF statement.
 if(response == (ques_sum))
	  {
	  message =  "Correct!";

	  number_right++; //increment it here

	  JOptionPane.showMessageDialog(null, message.toUpperCase());
	  }
	  else
	  {
	  message = "Wrong!";
	   JOptionPane.showMessageDialog(null, message.toUpperCase());
	  }
	  ++counter;
	}
	
	number_total = number;


Also you need to declare it with 0, not 1. As no questions have been answered correctly until the first time through the loop.
int number_right = 0;  //number of questions answered correctly




THANK YOU THAT WORKED FOR THE FIRST PART OF THE FINAL MESSAGE "CORRECTLY ANSWERED X OT OF X QUESTIONS" BUT THE SECOND PART WHEN I WANT TO PUT THE PERCENTAGE IN THE "number_right" VARIABLE IS NOT DIVIDING ITS ALWAYS COMING OUT ZERO, IM NOT SURE WHY? ALSO HOW DO CONTINUE MY LITTLE QUIZ AFTERWARDS IF THE USER TYPES IN "YES" FOR THE INPUT AFTER A MESSAGE BOX ASKS IF THE USER WANTS TO PLAY AGAIN AND IF THEY INPUT "NO" THERE WILL BE A BOX STATING HOW MANY RIGHT OUT OF THE ABSOLUTE TOTAL NUMBER OF QUESTIONS.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: IF ELSE and WHILE LOOPS

Posted 17 February 2007 - 05:18 PM

To answer your first question about why it is not displaying your percentage. That is because you are dividing integer data types so the answer will always result in a whole number.

In this case your division will result in a number less than zero (ie. 0.5, 0.25). So you need to cast your variables as type Double in order to perform the division correctly. Otherwise the displayed value will be truncated meaning it will always be 0 because it throws away everything after the decimal.
JOptionPane.showMessageDialog(null, "You correctly answered "+number_right+" out of "+number_total+" questions correctly. \nYour percentage is: "+(double)number_right/(double)number_total+"%");


Your second question requires two loops, one that is nested inside the other. The outer loop handles the question of whether or not to continue with the game. The inner loop will be the loop you currently have which takes the user input and keeps track of the score.
Was This Post Helpful? 0
  • +
  • -

#5 BudLight412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-January 07

Re: IF ELSE and WHILE LOOPS

Posted 17 February 2007 - 06:33 PM

THANK YOU THAT WAS WHAT WAS WRONG AND NOW I AM WORKING ON PUTTING THE OUTER LOOP IN ORDER TO ALLOW THE GAME TO RESTART IF "YES" IS TYPED IN. WHEN I TYPE IN YES OR NO THE MESSAGE BOX POPS UP AGAIN THEN GOES TO THE FINAL MESSAGE BOX. I WANT THE GAME TO RESTART IF I TYPE IN YES AND IF I TYPE IN NO I WANT THE FINAL MESSAGE BOX TO SHOW UP. HELP PLEASE

while(counter2 <= number)
   {
	while(counter <= number)
	{
	  ques1 = (Math.abs(questions.nextInt())%20)+1;
	  ques2 = (Math.abs(questions.nextInt())%20)+1;
	  ques_sum = (ques1+ques2);
	  
	  input = JOptionPane.showInputDialog(ques1+ "+" +ques2);
	  response = Integer.parseInt(input);
	  
	  if(response == (ques_sum))
	  {
	  message =  "Correct!";
	  number_right++;
	  JOptionPane.showMessageDialog(null, message.toUpperCase());
	  }
	  else
	  {
	  message = "Wrong!";
	  JOptionPane.showMessageDialog(null, message.toUpperCase()); 
	  }
	  ++counter;
	}
	input = JOptionPane.showInputDialog("Do you want to take the quiz again?");
	if(input == "yes")
	{
	  ques1 = (Math.abs(questions.nextInt())%20)+1;
	  ques2 = (Math.abs(questions.nextInt())%20)+1;
	  ques_sum = (ques1+ques2);
	  input = JOptionPane.showInputDialog(ques1+ "+" +ques2);
	  response = Integer.parseInt(input);
	}
	else
	{
	}
	++counter2;
   }

Was This Post Helpful? 0
  • +
  • -

#6 BudLight412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-January 07

Re: IF ELSE and WHILE LOOPS

Post icon  Posted 18 February 2007 - 01:41 PM

HOW DO I START THE INNER LOOP OVER AGAIN USING AN OUTER LOOP. ONCE MY INNER LOOP IS DONE A MESSAGE BOX WILL POP UP AND ASK IF THE USER WANTS TO PLAY AGAIN, AND IF YES IS INPUTED I WANT THE INNER LOOP TO START OVER, BUT IF NO IS INPUTED I WANT IT TO EXIT THE LOOPS AND CONTINUE TO MY FINAL JOPTIONPANE MESSAGE BOX.
while(counter2 <= number)
   {
	while(counter <= number)
	{
	  ques1 = (Math.abs(questions.nextInt())%20)+1;
	  ques2 = (Math.abs(questions.nextInt())%20)+1;
	  ques_sum = (ques1+ques2);
	  
	  input = JOptionPane.showInputDialog(ques1+ "+" +ques2);
	  response = Integer.parseInt(input);
	  
	  if(response == (ques_sum))
	  {
	  message =  "Correct!";
	  number_right++;
	  JOptionPane.showMessageDialog(null, message.toUpperCase());
	  }
	  else
	  {
	  message = "Wrong!";
	  JOptionPane.showMessageDialog(null, message.toUpperCase()); 
	  }
	  ++counter;
	}
	input = JOptionPane.showInputDialog(null, "Do you want to take the quiz again?", "yes or no");
	if(input == "yes")
	{
	  ques1 = (Math.abs(questions.nextInt())%20)+1;
	  ques2 = (Math.abs(questions.nextInt())%20)+1;
	  ques_sum = (ques1+ques2);
	  input = JOptionPane.showInputDialog(ques1+ "+" +ques2);
	  response = Integer.parseInt(input);
	}
	else
	{
	
  
	
	// Declaring more variables that have to be declared after the loop
	number_total = number;
	float percentage = (((float)number_right/number_total)*100);
	
	if(number_right == number)
	 {
	 JOptionPane.showMessageDialog(null, "Congratulations! \nYou got all the questions right! \nYour percentage is 100%", "CONGRATS", JOptionPane.WARNING_MESSAGE);
	 }
	 else
	 {
	 JOptionPane.showMessageDialog(null, "You correctly answered "+number_right+" out of "+number_total+" questions correctly. \nYour percentage is: "+formatter.format(percentage)+"%", "ALMOST", JOptionPane.ERROR_MESSAGE);			 
	 }
	}
   ++counter2;
   }
	
	System.exit(0);

Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: IF ELSE and WHILE LOOPS

Posted 18 February 2007 - 02:40 PM

Please don't use all caps in your posts and post your code between [ code] tags. Thank you.

First thing, there is no need to duplicate your code that prompts the user with the quiz questions. It defeats the purpose of a loop.

Lets start by establishing your inner loop, since you have that working correctly already.
		while(counter <= number)
		{
		  ques1 = (Math.abs(questions.nextInt())%20)+1;
		  ques2 = (Math.abs(questions.nextInt())%20)+1;
		  ques_sum = (ques1+ques2);
	  
   	 	  input = JOptionPane.showInputDialog(ques1+ "+" +ques2);
			response = Integer.parseInt(input);
	  
		  if(response == (ques_sum))
		  {
			  message =  "Correct!";
			  number_right++;
			  JOptionPane.showMessageDialog(null, message.toUpperCase());
		  }
		  else
		  {
			  message = "Wrong!";
	   		JOptionPane.showMessageDialog(null, message.toUpperCase());
		  }
		  ++counter;
		}


Now this code needs to be wrapped in a second loop. In this case I would use a Do/While loop, although a While loop will work equally well. I just prefer not to have to ask the same question twice in code when you only have to do it once with a Do/While loop.
	do
	{ 
		//Asking how many questions
		input = JOptionPane.showInputDialog("Welcome to Jay's simple addition quiz! \n\nHow many questions would you like?");
		number = Integer.parseInt(input);
	 
		while(counter <= number)
		{
		  ques1 = (Math.abs(questions.nextInt())%20)+1;
		  ques2 = (Math.abs(questions.nextInt())%20)+1;
		  ques_sum = (ques1+ques2);
	  
   	 	  input = JOptionPane.showInputDialog(ques1+ "+" +ques2);
			response = Integer.parseInt(input);
	  
		  if(response == (ques_sum))
		  {
			  message =  "Correct!";
			  number_right++;
			  JOptionPane.showMessageDialog(null, message.toUpperCase());
		  }
		  else
		  {
			  message = "Wrong!";
	   		JOptionPane.showMessageDialog(null, message.toUpperCase());
		  }
		  ++counter;
		}
	
	number_total = number;
	
	JOptionPane.showMessageDialog(null, "You correctly answered "+number_right+" out of "+number_total+" questions correctly. \nYour percentage is: "+(double)number_right/(double)number_total+"%");			
  
	   input = JOptionPane.showInputDialog("Do you want to take the quiz again?");
	   
 	 
	}while(input.toUpperCase().equals("YES"));


To test the condition of a string variable you cannot simply use the == for the equality check. You must use the String method equals to compare strings. But because the user can enter the word several different ways, meaning with caps or without, you will need to convert it to upper or lowercase or test for all possibilities. It simpler to just convert to one or the other and compare one condition.
while(input.toUpperCase().equals("YES"));

So in this bit of code we are stacking methods to get this done in one line. Input is your string variable, which is then converted to uppercase, and the last step is to use the equals method to test the condition.


The last step that needs to be taken care of to get this working properly is that you have a couple of variables that are keeping track of things. So you will need to reset those variables before starting a new game.

When all is said and done you will end up with code like this:
//Importing the neccesary commands
import javax.swing.JOptionPane;
import java.util.Random;

public class Jay2
{
  public static void main(String [] args)
  {
  
	int ques1;  //First random number
	int ques2;  // Second random number
	int number = 0;  //How many questions input
	int counter = 1;  //while counter
	int ques_sum;  //sum of the two random numbers
	int response;  //your answer
	int number_right = 0;  //number of questions answered correctly
	int number_total = 0;  //number of questions you wanted
	String input;  //Your input for the two numbers
	String message;  //Correct of Wrong massages
	Random questions = new Random();  //Random number selector
	
	   
	do
	{ 
		//Asking how many questions
		input = JOptionPane.showInputDialog("Welcome to Jay's simple addition quiz! \n\nHow many questions would you like?");
		number = Integer.parseInt(input);
	 
		while(counter <= number)
		{
		  ques1 = (Math.abs(questions.nextInt())%20)+1;
		  ques2 = (Math.abs(questions.nextInt())%20)+1;
		  ques_sum = (ques1+ques2);
	  
   	 	  input = JOptionPane.showInputDialog(ques1+ "+" +ques2);
			response = Integer.parseInt(input);
	  
		  if(response == (ques_sum))
		  {
			  message =  "Correct!";
			  number_right++;
			  JOptionPane.showMessageDialog(null, message.toUpperCase());
		  }
		  else
		  {
			  message = "Wrong!";
	   		JOptionPane.showMessageDialog(null, message.toUpperCase());
		  }
		  ++counter;
		}
	
	number_total = number;
	
	JOptionPane.showMessageDialog(null, "You correctly answered "+number_right+" out of "+number_total+" questions correctly. \nYour percentage is: "+(double)number_right/(double)number_total+"%");			
  
	   input = JOptionPane.showInputDialog("Do you want to take the quiz again?");
	   
	   counter = 1;
	   number_right = 0;
	   
	}while(input.toUpperCase().equals("YES"));
	
	System.exit(0);
  }
}

Was This Post Helpful? 0
  • +
  • -

#8 BudLight412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-January 07

Re: IF ELSE and WHILE LOOPS

Posted 18 February 2007 - 03:19 PM

Jayman9 thank you so much for all of your help, i appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1