[JAVA] Help in this Program
Silence 02 Aug 2009
import javax.swing.JOptionPane; public class OMG { public static void main(String[] args) { int num=1; char option='Y'; while ((option=='Y')||(option=='y')){ if (num==1){ JOptionPane.showMessageDialog(null, "Count: "+num); String ops=JOptionPane.showInputDialog("Do you want to count again? Y/N?"); option=ops.charAt(0); num++; } else { JOptionPane.showMessageDialog(null, "Count: "+num); String ops=JOptionPane.showInputDialog("Do you want to count again? Y/N?"); option=ops.charAt(0); num++; } } JOptionPane.showMessageDialog(null, "Final Count "+num); } }
What you see above is a Java Program that will ask if the User wants to Count, if the user inputs either a Yes Answer ('Y' or a 'y'), the integer "x" will increment, but when I input a NO answer ('N' or a 'n') the program should output the Final Count or the Last number. But the problem is when I put a NO answer the program still increment Once and output a wrong answer. For Example the Last number is 5, but the Output will become 6.
Please Help
Slightly Wonky Robob 02 Aug 2009
Well, I don't know Java, but from the look of things it's because you increment 'num' after you you ask the question, so regardless of what the answer it's gonna get increased. So I would say you either need to check the answer before increasing, or simply increase the number before you ask the question (and as a result num would need to start at 0, not 1)
Edited by Bob, 02 August 2009 - 23:01.
Edited by Bob, 02 August 2009 - 23:01.
Dauth 02 Aug 2009
Silence, on 2 Aug 2009, 23:29, said:
import javax.swing.JOptionPane; public class OMG { public static void main(String[] args) { int num=1; char option='Y'; while ((option=='Y')||(option=='y')){ if (num==1){ JOptionPane.showMessageDialog(null, "Count: "+num); String ops=JOptionPane.showInputDialog("Do you want to count again? Y/N?"); option=ops.charAt(0); comment out me num++; } else { JOptionPane.showMessageDialog(null, "Count: "+num); String ops=JOptionPane.showInputDialog("Do you want to count again? Y/N?"); option=ops.charAt(0); num++; } } JOptionPane.showMessageDialog(null, "Final Count "+num); } }
What you see above is a Java Program that will ask if the User wants to Count, if the user inputs either a Yes Answer ('Y' or a 'y'), the integer "x" will increment, but when I input a NO answer ('N' or a 'n') the program should output the Final Count or the Last number. But the problem is when I put a NO answer the program still increment Once and output a wrong answer. For Example the Last number is 5, but the Output will become 6.
Please Help
Put something in to show you where Bob means.
jnengland77 03 Aug 2009
As Bob and Dauth pointed out you increment num no matter whether num is equal to 1 or not.
To make the code more readable, shorter, and less code duplication, I would suggest the following code:
~jnengland77
P.S. Ewww it's Java...
Edited by jnengland77, 03 August 2009 - 06:37.
To make the code more readable, shorter, and less code duplication, I would suggest the following code:
import javax.swing.JOptionPane; public class OMG { public static void main(String[] args) { int num=1; char option='Y'; while ((option=='Y')||(option=='y')){ JOptionPane.showMessageDialog(null, "Count: "+num); String ops=JOptionPane.showInputDialog("Do you want to count again? Y/N?"); option=ops.charAt(0); if (num != 1){ num++; } } } JOptionPane.showMessageDialog(null, "Final Count "+num); }
~jnengland77
P.S. Ewww it's Java...
Edited by jnengland77, 03 August 2009 - 06:37.