java.2.19
在IDEA中,新建项目,步骤如图1.1所示。
新建一个分类包。
在包下面新建一个java类**(类名首字母大写)**
里面书写主函数,简写为psvm,输出helloworld。对于java来说,最小单元是类。调用的输出是为静态static。"+"类似于C中的strcat()。
package test_02;
public class Test_02 {
public static void main(String[] args) {
// output
System.out.println("Hello World");
}
}
D:\java\1.8verson\bin\java.exe ...
Hello World
进程已结束,退出代码为 0
package test_02;
public class Test_02 {
public static void main(String[] args) {
// output string
// System.out.println("Hello World");
/* output int double float
* such as printf in C language */
System.out.printf("%d %.2f", 10, 11.5);
}
}
D:\java\1.8verson\bin\java.exe
10 11.50
进程已结束,退出代码为 0
package test_02;
public class Test_02 {
public static void main(String[] args) {
// output string
// System.out.println("Hello World");
/* output int double float
* such as printf in C language */
// System.out.printf("%d %.2f", 10, 11.5);
// result abc
System.out.println("a" + "b" + "c");
}
}
D:\java\1.8verson\bin\java.exe
abc
进程已结束,退出代码为 0
对于数组,分为静态和动态的初始化及输出。
package test_02;
public class Test_02 {
public static void main(String[] args) {
// output string
// System.out.println("Hello World");
/* output int double float
* such as printf in C language */
// System.out.printf("%d %.2f", 10, 11.5);
// result abc
// System.out.println("a" + "b" + "c");
// 动态数组
int []a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = i * 2; // input
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " "); // output
}
}
}
D:\java\1.8verson\bin\java.exe
0 2 4 6 8 10 12 14 16 18
进程已结束,退出代码为 0
package test_02;
public class Test_02 {
public static void main(String[] args) {
// output string
// System.out.println("Hello World");
/* output int double float
* such as printf in C language */
// System.out.printf("%d %.2f", 10, 11.5);
// result abc
// System.out.println("a" + "b" + "c");
// 动态数组
int []a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = i * 2; // input
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " "); // output
}
System.out.println(); // output to enter.
int []b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " "); // output
}
}
}
D:\java\1.8verson\bin\java.exe
0 2 4 6 8 10 12 14 16 18
1 2 3 4 5 6 7 8 9 10
进程已结束,退出代码为 0
输入
package test_02;
import java.util.Scanner;
public class Test_02 {
public static void main(String[] args) {
// output string
// System.out.println("Hello World");
/* output int double float
* such as printf in C language */
// System.out.printf("%d %.2f", 10, 11.5);
// result abc
// System.out.println("a" + "b" + "c");
// 动态数组
int []a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = i * 2; // input
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " "); // output
}
System.out.println(); // output to enter.
int []b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " "); // output
}
System.out.println(); // output to enter.
/* 对象方法,
开辟空间,
类声明一个对象,
新建一个,
调用
*/
Scanner sc = new Scanner(System.in); // input
int x; // 声明
System.out.println("input int number"); // 提示符
x = sc.nextInt(); // 输入的值赋值给x
System.out.println("x = " + x); // output
}
}
D:\java\1.8verson\bin\java.exe
0 2 4 6 8 10 12 14 16 18
1 2 3 4 5 6 7 8 9 10
input int number
20
x = 20
进程已结束,退出代码为 0