Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

if else statements

Posted on 2013-12-06
5
Medium Priority
?
286 Views
Last Modified: 2013-12-08
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
Comment
Question by:aminkeith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 1800 total points
ID: 39702763
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39702765
50 points? If i had seen I would have not spent a second of my time... :)
0
 

Author Comment

by:aminkeith
ID: 39703780
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
 

Author Comment

by:aminkeith
ID: 39703785
I think you should have 450 more points up there now.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39704001
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question