help in java program |
help in java program |
2 Aug 2009, 15:49
Post
#1
|
|
Teh SWR Productions Greeting Committee Group: Members Posts: 48 Joined: 8 June 2009 Member No.: 91 I own bootleg copies of pirated games. |
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 This post has been edited by silence: 2 Aug 2009, 16:07 -------------------- |
|
|
3 Aug 2009, 6:57
Post
#2
|
|
Group: Members Posts: 363 Joined: 16 June 2009 From: Irving, TX Member No.: 167 |
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); } } |
|
|
3 Aug 2009, 12:22
Post
#3
|
|
Teh SWR Productions Greeting Committee Group: Members Posts: 48 Joined: 8 June 2009 Member No.: 91 I own bootleg copies of pirated games. |
Thanks very much C.o.m.m.a.n.d.e.r
-------------------- |
|
|
7 Aug 2009, 22:08
Post
#4
|
|
Group: Members Posts: 5 Joined: 3 August 2009 From: colorado Member No.: 316 |
Im the one that is commander's friend and i hope there were no other issues with that fix.
|
|
|
7 Aug 2009, 22:58
Post
#5
|
|
Group: Members Posts: 105 Joined: 4 July 2009 Member No.: 236 |
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 post has been edited by CardBoardBoxProcessor: 7 Aug 2009, 23:01 |
|
|
7 Aug 2009, 23:59
Post
#6
|
|
Group: Members Posts: 5 Joined: 3 August 2009 From: colorado Member No.: 316 |
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 post has been edited by Deathwarror: 8 Aug 2009, 0:00 |
|
|
Lo-Fi Version | Time is now: 18 April 2024 - 5:06 |