| | |
while loop
![]() |
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
Solved Threads: 0
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;
}
}
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;
}
}
•
•
•
•
while(!function1.equals(selection))
{
JOptionPane.showInputDialog(null, selection + " is not the correct function.\n\nSorry please try again");
counter++;
System.exit(0);
}
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
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:
to:
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...
java Syntax (Toggle Plain Text)
while( ! function1.equals( selection ))
java Syntax (Toggle Plain Text)
while( ! function1.equals( selection ) && counter <= 3 )
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. ![]() |
Similar Threads
- Help with gui loop. (C)
- Loop...without the loop (Java)
Other Threads in the Java Forum
- Previous Thread: breaking down a string with a delimiter
- Next Thread: JMF: video format and individual frames
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows





