Java input with scanner class
1) Single input:
For example:
Import Java.util.Scanner;
public class AA {
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
int a = Sc.nextint ();//input 1
System.out.println (a);//Output 1
}
}
2) Unlimited Input:
For example:
Import Java.util.Scanner;
public class AA {
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
while (Sc.hasnext ()) {
int a = Sc.nextint ();
System.out.println (a);
}
}
}
3) Enter an integer and enter a character: (Java does not have to enter a single character statement, all defined as a string)
For example 1):
Import Java.util.Scanner;
public class AA {
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
int a = Sc.nextint ();//input 1
String B = Sc.next ();//Input 2
System.out.println ("A:" +a+ "--b:" +b);//Output A:1--b:2
}
}
For example 2):
Import Java.util.Scanner;
public class AA {
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
int a = Sc.nextint ();//input 1
Sc.nextline ();//Receive carriage return, here only with Sc.nextline ();
String B = sc.nextline ();//Input 2
System.out.println ("A:" +a+ "--b:" +b);//Output A:1--b:2
}
}
How to write the input rules of HDU topic in Java language