Welcome to Dream.In.Code
Become a Java Expert!

Join 149,840 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,441 people online right now. Registration is fast and FREE... Join Now!




IF ELSE and WHILE LOOPS

 
Reply to this topicStart new topic

IF ELSE and WHILE LOOPS

BudLight412
17 Feb, 2007 - 03:03 PM
Post #1

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 16


My Contributions
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.

CODE

//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);
  }
}

User is offlineProfile CardPM
+Quote Post

Jayman
RE: IF ELSE And WHILE LOOPS
17 Feb, 2007 - 03:28 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,313



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You need to increment the number_right variable inside your IF statement.
CODE

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.
CODE
int number_right = 0;  //number of questions answered correctly

User is offlineProfile CardPM
+Quote Post

BudLight412
RE: IF ELSE And WHILE LOOPS
17 Feb, 2007 - 03:52 PM
Post #3

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 16


My Contributions
[quote name='jayman9' date='17 Feb, 2007 - 04:28 PM' post='203377']
You need to increment the number_right variable inside your IF statement.
CODE

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.
CODE
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.

User is offlineProfile CardPM
+Quote Post

Jayman
RE: IF ELSE And WHILE LOOPS
17 Feb, 2007 - 04:18 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,313



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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.
CODE

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.
User is offlineProfile CardPM
+Quote Post

BudLight412
RE: IF ELSE And WHILE LOOPS
17 Feb, 2007 - 05:33 PM
Post #5

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 16


My Contributions
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

CODE

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;
   }

User is offlineProfile CardPM
+Quote Post

BudLight412
RE: IF ELSE And WHILE LOOPS
18 Feb, 2007 - 12:41 PM
Post #6

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 16


My Contributions
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.
CODE

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);

User is offlineProfile CardPM
+Quote Post

Jayman
RE: IF ELSE And WHILE LOOPS
18 Feb, 2007 - 01:40 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,313



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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.
CODE

        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.
CODE

    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.
CODE

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:
CODE

//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);
  }
}

User is offlineProfile CardPM
+Quote Post

BudLight412
RE: IF ELSE And WHILE LOOPS
18 Feb, 2007 - 02:19 PM
Post #8

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 16


My Contributions
Jayman9 thank you so much for all of your help, i appreciate it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:57AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month