Zebra0.com

java variablesAssignment statements

Assignment statements

Variables can be given a value using an assignment statement:

age=12;
answer='Y';
cost=5.25;

The variable that is getting a value is always on the left.

The statement below will give both num1 and num1 a value of 5.

num1=num2=5;

You can also use the keyboard to read in a value:

Put the line below under the package statement:

import java.util.Scanner;

Put the line below under the public static void main(String[] args) { statement:

Scanner keyboard=new Scanner(System.in);
System.out.print("Enter the age:");
age=keyboard.nextInt();

When we read in from the keyboard we usually use System.out.print to print a prompt first so the user will know what is expected.

End of lesson, Next lesson: