Solved

if else statements

Posted on 2013-12-06
5
267 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 30

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 30

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 30

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 improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deletiā€¦
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.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now