Assignemnt #35 Else and If
Code
/// Tyler Hart
/// Period 5
/// Program name: Else And If
/// File name: ElseAndIf.java
/// Date finished: 10/1/2015
public class ElseAndIf
{
public static void main( String[] args )
{
int people = 30, cars = 40, buses = 15;
if ( cars > people )
{
System.out.println( "We should take the cars." );
}
else if ( cars < people )// if the if statement is false do this if the else if statement is true.
{
System.out.println( "We should not take the cars." );
}
else
{
System.out.println( "We can't decide." );
}
if ( buses > cars )
{
System.out.println( "That's too many buses." );
}
else if ( buses < cars )//it allows another else statement to be executed, the else's only act if the if statement above it is false, so the else if is a link in a chain
{
System.out.println( "Maybe we could take the buses." );
}
else
{
System.out.println( "We still can't decide." );
}
if ( people > buses )
{
System.out.println( "All right, let's just take the buses." );
}
else
{
System.out.println( "Fine, let's stay home then." );
}
}
}
Picture of the output