Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

if else statements

In the following code:

import java.util.Scanner;

public class play
{
   public static void main (String[] args)
   {
   Scanner scan = new Scanner(System.in) ;
   int apple = scan.nextInt() ;      
   
   if (apple == 1)
   System.out.println("there is only 1 apple left.");  
   
   if (apple ==100)
   System.out.println("there is plenty of apples here.");  
   
   else
   System.out.println("Something else.");
   
   }
   
}

If I type 1 into the scanner it prints out the statement for 1st if AND the statement for else.
If I type 100 into scanner it prints out the statement for 2nd if like it should.
If I type 2 (for example) into the scanner it prints out the else statement alone, as it should.
I don't understand why this is happening and how to print out only the 1st if statement.
I've played around with brackets and still couldn't figure it out.
0
aminkeith
Asked:
aminkeith
  • 3
  • 2
1 Solution
 
Marco GasiFreelancerCommented:
The correct syntax is:

public class play
{
   public static void main (String[] args)
   {
   Scanner scan = new Scanner(System.in) ;
   int apple = scan.nextInt() ;      
   
   if (apple == 1)
   {
       System.out.println("there is only 1 apple left.");  
   }
   else if (apple ==100)
   {
       System.out.println("there is plenty of apples here.");  
   } 
   else 
   {
       System.out.println("Something else.");
   }
   
}

Open in new window

0
 
Marco GasiFreelancerCommented:
50 points? If i had seen I would have not spent a second of my time... :)
0
 
aminkeithAuthor Commented:
sorry marqusG, I'm not quite sure how the point system on this site works and I would have put more if I'd have known.
0
 
aminkeithAuthor Commented:
I think you should have 450 more points up there now.
0
 
Marco GasiFreelancerCommented:
Hi aminkeith. Thanks for points. About this, if you take a tour through some quesiton posted here at EE you'll see the most part of them award 500 points. Don't misuanderstand me: I'm happy with 450 points: I want only help  you to understand how EE works :)
Personally, I don't see the reason to award less than 500 points: someone says his question is simple, the solution is easy, his problem is little, but if it were so simple, so easy and so littl whay ask a question at Experts-Exchange?
Me too, when I post some question always award 500 points, because if I didn't find a solution by myself the help I can find here is too mutch important to not give the maximum I can give as thanks to the expert who helps me. But this is just my opinion.

Finally, you might want to learn the guidlines on grading answers here: http://support.experts-exchange.com/customer/portal/articles/481419

Cheers and good luck with your project.
Marco
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now