while loop

Reply

Join Date: Mar 2008
Posts: 1
Reputation: sonyb is an unknown quantity at this point 
Solved Threads: 0
sonyb sonyb is offline Offline
Newbie Poster

while loop

 
0
  #1
Mar 11th, 2008
Hello. This is my first time programming in any language so please excuse my ignorance. I am trying write a program that asks the user to enter any number 3 times . I have assigned a function that takes the number and outputs an answer . After the 3rd number has been entered , the user is asked to guess what function was used to achieve that answer. The user is given 3 attempts to guess this answer , at which time I wan to move on to the next question. I am having trouble with the counter aspect of this program. Thanks for any assistance.

import javax.swing.*;
public class GuessingGameRevised

{

public static void main (String [] arguments)

{

String guess1, guess2, guess3, selection;
String function1 = "2x + 5";

double answer1,answer2,answer3, userselection;

int counter = 1 ;

guess1 = JOptionPane.showInputDialog(null, " Please select a number " );
guess2 = JOptionPane.showInputDialog(null, " Please select a second number " );
guess3 = JOptionPane.showInputDialog(null, " Please select a third number " );

answer1 = Double.parseDouble(guess1);
answer2 = Double.parseDouble(guess2);
answer3 = Double.parseDouble(guess3);



selection = JOptionPane.showInputDialog(null, " You chose " + answer1 + "\nand when inserted into the function equals :\n" + function(answer1) +
" \n\nYou chose " + answer2 + "\nand when inserted into the function equals:\n" + function(answer2)+
" \n\nYou chose " + answer3 + "\nand when inserted into the function equals:\n" + function(answer3) +
"\n\nNow, can you determine what the function is?");




while(!function1.equals(selection))

{
JOptionPane.showInputDialog(null, selection + " is not the correct function.\n\nSorry please try again");
counter++;


System.exit(0);

}



JOptionPane.showMessageDialog(null, "Wow your smarter than Dr. Nii!");

System.exit(0);






}

public static double function(double f)
{

double func;
func = (2*f) + 5;
return func;

}







}
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: while loop

 
0
  #2
Mar 11th, 2008
Originally Posted by sonyb View Post
while(!function1.equals(selection))
{
JOptionPane.showInputDialog(null, selection + " is not the correct function.\n\nSorry please try again");
counter++;
System.exit(0);
}
for starters, get rid of that System.exit(0); statement.
haven't read the rest, so there might be some other errors in it.
you might want to consider putting your code in code-tags next time, makes it easier for us to read it
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: while loop

 
0
  #3
Mar 11th, 2008
You want to say "while the guess is not correct and counter is <= 3, do something" right? You need to change your line that reads:
  1. while( ! function1.equals( selection ))
to:
  1. while( ! function1.equals( selection ) && counter <= 3 )
The && means "and". This means that both statements must be true in order for the code inside the while loop to execute.

Another handy one is || which means "or", meaning that either statement can be true (or both) for the code inside the loop to execute.

EDIT: Too slow, and I didn't see the System.exit() statement...
Last edited by darkagn; Mar 11th, 2008 at 9:16 am. Reason: Acknowledging the post at the same time.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC