I.e., that's the terminal window where you type in the command java ProgramName
One of these variables is the Java system variable:
System.in
|
which represents the console input
The variable System.in is included in every Java program (you don't need to define it).
In other words:
|
All you need to know is:
|
|
(Remember that a class is a container for methods)
|
java.util.Scanner
|
(see: click here)
|
import java.util.Scanner;
|
It is too early to explain what this means... I will only tell you how to do it
Scanner varName = new Scanner(System.in); |
The name varName is an identifier
Example: constructing a Scanner object named in
Scanner in = new Scanner(System.in); |
a = in.nextDouble() ;
|
You must save (store) the number read in by "in.nextDouble()" in a double typed variable with an assignment statement
|
|
import java.util.Scanner; // Import Scanner class (contains methods
// for reading keyboard input)
public class Abc2
{
public static void main(String[] args)
{
double a, b, c, x1, x2; // Define 5 variable
Scanner in = new Scanner(System.in); // Construct a Scanner object
a = in.nextDouble(); // Read in next number and store in a
b = in.nextDouble(); // Read in next number and store in b
c = in.nextDouble(); // Read in next number and store in c
x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);
x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);
System.out.print("a = ");
System.out.println(a);
System.out.print("b = ");
System.out.println(b);
System.out.print("c = ");
System.out.println(c);
System.out.print("x1 = ");
System.out.println(x1);
System.out.print("x2 = ");
System.out.println(x2);
}
}
|
How to run the program:
|
In other words:
|
|
import java.util.Scanner; // Import Scanner class (contains methods
// for reading keyboard input)
public class Abc2
{
public static void main(String[] args)
{
double a, b, c, x1, x2; // Define 5 variable
Scanner in = new Scanner(System.in); // Construct a Scanner object
System.out.print("Enter a = "); // ******* Prompt message
a = in.nextDouble(); // Read in next number and store in a
System.out.print("Enter b = ");
b = in.nextDouble(); // Read in next number and store in b
System.out.print("Enter c = ");
c = in.nextDouble(); // Read in next number and store in c
x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);
x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);
System.out.print("a = ");
System.out.println(a);
System.out.print("b = ");
System.out.println(b);
System.out.print("c = ");
System.out.println(c);
System.out.print("x1 = ");
System.out.println(x1);
System.out.print("x2 = ");
System.out.println(x2);
}
}
|
This one discussed is used to read in a floating point number:
|
To read in a different kind of item, we need to use a different method in the Scanner class that read the correct type of data.
|
Note: you also need to use an int typed variable to store an integer value !!!