Jump to content


[JAVA] Help in this Program


3 replies to this topic

#1 Silence

    Semi-Pro

  • Member
  • 238 posts
  • Projects: Maintaning the N00Bity of people~

Posted 02 August 2009 - 22:29

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
Posted Image

#2 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 02 August 2009 - 23:00

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.

Posted Image
F O R T H E N S
Posted Image

#3 Dauth

    <Custom title available>

  • Gold Member
  • 11193 posts

Posted 02 August 2009 - 23:31

View PostSilence, on 2 Aug 2009, 23:29, said:

import javax.swing.JOptionPane;
 
 public class OMG {
		 
	 public static void main&#40;String&#91;&#93; args&#41; {
		  int num=1;
		  char option=&#39;Y&#39;;
		  
		  while &#40;&#40;option==&#39;Y&#39;&#41;||&#40;option==&#39;y&#39;&#41;&#41;{
			  if &#40;num==1&#41;{
				  
				  JOptionPane.showMessageDialog&#40;null, &#34;Count&#58; &#34;+num&#41;;
				  String ops=JOptionPane.showInputDialog&#40;&#34;Do you want to count again? Y/N?&#34;&#41;;
				  option=ops.charAt&#40;0&#41;;
  comment out me				 num++;
			  }
			  else {
				  JOptionPane.showMessageDialog&#40;null, &#34;Count&#58; &#34;+num&#41;;
				  String ops=JOptionPane.showInputDialog&#40;&#34;Do you want to count again? Y/N?&#34;&#41;;
				  option=ops.charAt&#40;0&#41;;
				  num++;
				  
				  
			  }
	  
		  }	 
			  JOptionPane.showMessageDialog&#40;null, &#34;Final Count &#34;+num&#41;;
	 }
 }


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.

#4 jnengland77

    Amateur

  • Gold Member
  • 138 posts

Posted 03 August 2009 - 06:36

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:

import javax.swing.JOptionPane;

public class OMG {
		
	public static void main&#40;String&#91;&#93; args&#41; {
 		int num=1;
 		char option=&#39;Y&#39;;
 		
 		while &#40;&#40;option==&#39;Y&#39;&#41;||&#40;option==&#39;y&#39;&#41;&#41;{
 			JOptionPane.showMessageDialog&#40;null, &#34;Count&#58; &#34;+num&#41;;
 			String ops=JOptionPane.showInputDialog&#40;&#34;Do you want to count again? Y/N?&#34;&#41;;
 			option=ops.charAt&#40;0&#41;;
 		
			if &#40;num != 1&#41;{
 				num++;
 			}
 		}
 	
 	} 	
 		JOptionPane.showMessageDialog&#40;null, &#34;Final Count &#34;+num&#41;;
}



~jnengland77
P.S. Ewww it's Java... :P

Edited by jnengland77, 03 August 2009 - 06:37.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users