473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

while loop

2
In this program , I informed the user to enter 3 random numbers .. Each one of these numbers were taken and entered into an function (2x + 5) and after the 3rd number was inserted into this equation , the user was asked to figure out what function was used to ahieve that answer.

I have a while loop, that compares the selection to the function (2x + 5) . I gave the user 3 trys to figure it out , and if he failed on the 3rd round , I am going to insert more code. Why is it that while I am in the while loop and say on my second attempt in guessin the function I get right... But it still proceeds to the third attempt without telling me I'm correct.

I am fairly new to this site as well as programming as a whole. Didn't think I would have ever been able to post something like this here, but I guess if your really passionate about something you have to start somewhere... Any sort of help or advise would be grateful..

Thanks in advance...


Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2.  
  3.  
  4.  
  5. public class GuessingGameRevised
  6.  
  7. {
  8.  
  9.     public static void main (String [] arguments)
  10.  
  11.     {
  12.  
  13.  
  14.  
  15.     String   guess1, guess2, guess3, selection;
  16.     String   function1 = "2x + 5";
  17.  
  18.     double  answer1,answer2,answer3;
  19.  
  20.     int counter = 1;
  21.  
  22.  
  23.  
  24.  
  25.     guess1 = JOptionPane.showInputDialog(null, " Please select a number " );
  26.     guess2 = JOptionPane.showInputDialog(null, " Please select a second number " );
  27.     guess3 = JOptionPane.showInputDialog(null, " Please select a third number " );
  28.  
  29.  
  30.  
  31.  
  32.     answer1 = Double.parseDouble(guess1);
  33.     answer2 = Double.parseDouble(guess2);
  34.     answer3 = Double.parseDouble(guess3);
  35.  
  36.  
  37.  
  38.     selection = JOptionPane.showInputDialog(null, " You chose " + answer1 + "\nand when inserted into the function equals :\n" + function(answer1) +
  39.     " \n\nYou chose " + answer2 + "\nand when inserted into the function equals:\n" + function(answer2)+
  40.     " \n\nYou chose " + answer3 + "\nand when inserted into the function equals:\n" + function(answer3) +
  41.     "\n\nNow, can you determine what the function is?");
  42.  
  43.     if(function1.equals(selection))
  44.         {
  45.             JOptionPane.showInputDialog(null, "That is correct ... Round two ...");
  46.  
  47.         System.exit(0);
  48.  
  49.  
  50.         }
  51.  
  52.  
  53.  
  54.     while (!function1.equals(selection) )
  55.     {
  56.  
  57.  
  58.  
  59.     JOptionPane.showInputDialog(null, selection + " is not the right answer."
  60.                                 + " Sorry try again.");
  61.  
  62.     counter++;
  63.  
  64.  
  65.  
  66.     if(counter >=3)
  67.  
  68.         {
  69.             JOptionPane.showMessageDialog(null, " Please study more ... Goodbye" );
  70.             System.exit(0);
  71.         }
  72.  
  73.  
  74.     }
  75.  
  76.  
  77.  
  78.  
  79.     }
  80.  
  81.  
Mar 12 '08 #1
1 1345
BigDaddyLH
1,216 Expert 1GB
Basically, your code isn't working because the logic is wrong.

If you step through your code by hand this should be evident.

For example, if the first guess is correct you display "That is correct ... Round two ...". Now what happens if the first guess is not correct? Control flow moves past this first if statement to the while loop, correct? Once in the while loop there are two possible outputs:

"... is not the right answer. Sorry try again."
or
" Please study more ... Goodbye"

Note there is no output saying "correct". Therefore there is no output that can say "correct". Do you see the faulty logic?
Mar 12 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: jsaul | last post by:
Hi there, wouldn't it be useful to have a 'while' conditional in addition to 'if' in list comprehensions? foo = for i in bar: if len(i) == 0: break foo.append(i)
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
9
by: Ben | last post by:
I have two 'Do While Not' statements, that are getting information from the same recordset. If I comment out the first one I can get the results for the second one, and vice-versa. Why is this...
7
by: Mahesh Kumar Reddy.R | last post by:
Hi Can any body resolve this.. In what cases one of the loop constructs better than other interms of speed , space and any other (redability). thanks mahesh
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
12
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
10
by: rohitjogya | last post by:
Can anyone tell me the difference bet for loop and while loop execution? ____________________ for (i=0 ; i<10 ; i++) ; /* Do nothing*/ print i; ___________________ i=0;
5
by: Alex | last post by:
Hi I just want to clear something up in my head with while loops and exceptions. I'm sure this will probably be a no brainer for most. Check this simple pseudo-code out (vb.net): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.