import java.io.*; // import java.io package
public class Class1
{
// our main function
public static void main (String [] args) throws java.io.IOException
{
int value = 0;
System.out.println("How many cars do you have?"); //Question?
// inputBuffer
BufferedReader inKeyBoard;
inKeyBoard = new BufferedReader(new InputStreamReader(System.in));
value = Integer.valueOf(inKeyBoard.readLine()).intValue();
//This is a if else test
if(value > 0) // if Value more than 0
{
System.out.println("Ok.."); // Thats OK!
}
else // else value is less or equal to 0
{
System.out.println("So you don't have a car."); // then no car
}
// if test
if(value < 0)
{
//if the value is negative
System.out.println("And you have a loan..");
}
// if test
if(value == 1) // if the value = 1
{
System.out.println("You have a car!");
}
// This is called an in-line if test, with no brackets '{}'.
// if value = 2
if(value == 2) System.out.println("You have two cars!");
// You can also write an if in one line like this
// more than 3 cars shuld make you rich
if(value > 3) System.out.println("Are you rich?");
}
}
// The IF statement is the first thing i learned in programming.
// IF? is your questions to the application, so that you can
// decide what is going to happen next..
// if a counter is 10 then set it back to 0
// if you don't have any money left, you can't buy that dvd-player.
// if your character fall more than 3 meters in a game,
// then he loses energy etc.
//sometimes you just need one if statement
//
// if (?)
// {
//
// }
//sometimes you ned two or more if statements
//
// if (question1?)
// {
//
// }
//
// if (question2?)
// {
//
// }
//sometimes it is easier to use if else
//
// if(?)
// {
//
// }
// else
// {
//
// }
//sometimes it is easier to use if else if
// if(question1?)
// {
//
// }
// else if(question2?)
// {
//
// }
// I have tried to demonstrate different ways to write if tests
// It all depend on the "situation", and how you prefer to do your programming..
Download Java Source Code
I hope you found this java tutorial useful!
Don't forget to mention Apron Tutorials in your References!