基本语法
:material-circle-edit-outline: 约 78 个字 :fontawesome-solid-code: 20 行代码 :material-clock-time-two-outline: 预计阅读时间 1 分钟
Input/Output
Java, like all other languages, supports three standard input/output streams: System.in
(standard input device), System.out
(standard output device), and System.err
(standard error device).
inout
import java.util.Scanner; // Needed to use the Scanner for keyboard input
public class ScannerTest {
public static void main(String[] args) {
// Declare variables
int num1;
double num2;
String str;
Scanner in = new Scanner(System.in);
num1 = in.nextInt(); // Use nextInt() to read an int
num2 = in.nextDouble(); // Use nextDouble() to read a double
str = in.next(); // Use next() to read a String token, up to white space
str = in.nextLine(); //read in the entire line
in.close(); // Scanner not longer needed, close it
// Formatted output via printf()
System.out.printf("%s, Sum of %d & %.2f is %.2f%n", str, num1, num2, num1+num2);
}
}
output
System.out.println(*aString*)
(print-line) prints*aString*
, and advances the cursor to the beginning of the next line.- 换行
System.out.print(*aString*)
prints*aString*
but places the cursor after the printed string.- 不换行
printf
: 格式化输出
System.out.print("GPA is"+gpa);
类型
final
$\approx $ const