System.out.print("Hello World");
The extra "ln" in the first statement moves the cursor to the next line after printing "Hello World".
There, that's it. There are several ways you can format your output, for example by using escape characters/sequences such as "\n" (moves to the next line), "\t" (to insert tabs (4 spaces)) etc.
Input
To take a value from user, i.e., from the console, we need to import a package as :
import java.util.Scanner;
We now create an object of class Scanner in the following manner :
Scanner input = new Scanner(System.in);
where input is the object created and can have any valid name, like infile, in1, etc. The input object can now be used as :
or,
Processing
Processing variables is simple. If I want to get the sum of two variables, namely 'a' and 'b', i'll simply write :
where 'sum' is another variable that stores the value of 'a+b'.
We will now create a Calculator that takes two values from the user, then choice of operations and finally prints the result.
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
int a,b,c=0,n;
Scanner input = new Scanner(System.in);
System.out.print("Enter both numbers");
a = input.nextInt();
b = input.nextInt();
System.out.print("Enter choice :\n1.Add\n2.Subtract\n3.Multiply\n4.Divide\n");
n = input.nextInt();
switch(n)
{ case 1: c = a + b;
break;
case 2: c = a - b;
break;
case 3: c = a * b;
break;
case 4: c = a / b;
break;
default: System.out.println("Invalid choice");
}
System.out.println("c = " + c);
}
}