Solved

if else statements

Posted on 2013-12-06
5
276 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
  • 3
  • 2
5 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 450 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

840 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