Help - Search - Members - Calendar
Full Version: help in java program
SWR Productions Forum > Community > The Databank
silence
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')){
            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
C.o.m.m.a.n.d.e.r
lucky for you i was talking to a friend who knows java while reading this. told me to post this

CODE
import javax.swing.JOptionPane;

public class OMG {

public static void main(String[] args) {
int num=0;
char option='Y';

while ((option=='Y')||(option=='y')){
num++;
JOptionPane.showMessageDialog(null, "Count: "+num);
String ops=JOptionPane.showInputDialog("Do you want to count again? Y/N?");
option=ops.charAt(0);

}
JOptionPane.showMessageDialog(null, "Final Count "+num);
}
}
silence
Thanks very much C.o.m.m.a.n.d.e.r
Deathwarror
Im the one that is commander's friend and i hope there were no other issues with that fix.
CardBoardBoxProcessor
yeah basically whether you hit yes or no you still had it doing the add thing. because both options had " num++;" and thus num + 1. Is this a trick question?

Also, you should probably make it an else if ((option=='N')||(option=='n')) and add something for all other options input such as to print "this is not an option" or something of the sort. Commander's friends works though but it does not account for n & N and other possible input mistakes. So if your teacher wants to make you add-on to this project (which HS teachers like to do in future projects) you will have to do it anyway.
Deathwarror
QUOTE (CardBoardBoxProcessor @ 7 Aug 2009, 12:58) *
yeah basically whether you hit yes or no you still had it doing the add thing. because both options had " num++;" and thus num + 1. Is this a trick question?

Also, you should probably make it an else if ((option=='N')||(option=='n')) and add something for all other options input such as to print "this is not an option" or something of the sort. Commander's friends works though but it does not account for n & N and other possible input mistakes. So if your teacher wants to make you add-on to this project (which HS teachers like to do in future projects) you will have to do it anyway.

This is true. it also checked the response after you added to number(originally). i would add a statement group "if((option!='Y')&&(option!='y')){ System.out.println("The answer that you stated is not a valid response or you have answered not to count anymore."); } That would basically fix the issue.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2018 Invision Power Services, Inc.