1、
①.编写一个类,类名为Test01
②.在main方法下定义变量s,并赋值为5,定义变量i,并赋值为12;
③.使用赋值运算符计算s+=i++的值,并把计算结果赋给变量s
④输出变量s的值
public class Test01 {
public static void main(String[] args) {
int s = 5;
int i = 12;
s += i++;
System.out.println("s = " + s);
}
}
2、利用if语句完成编程。
①编写一个类,类名为Test02
②从控制台输入两个整数m和n。
③比较两个数的大小,并输出最大值和最小值。
public class Test02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入m:");
int m = sc.nextInt();
System.out.println("请输入n:");
int n = sc.nextInt();
if (m > n) {
System.out.println("最大值m = " + m + "\n最小值n = " + n);
} else if (m < n) {
System.out.println("最大值n = " + n + "\n最小值m = " + m);
} else {
System.out.println("m = n = " + m);
}
}
}
3、利用循环语句完成编程
①编写一个类,类名为Test03
②计算2+4+6+8+…+100的结果
public class Test03 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 50; i++) {
sum += i*2;
}
System.out.println("sum = " + sum);
}
}
4、利用面向对象的知识编程
①创建一个Rectangle类,添加width和height两个成员变量。
② 编写一个测试类Test,创建一个Rectangle类的矩形对象,矩形的width值为20,height值为10。
public class Rectangle {
public float width;
public float height;
}
public class Test {
public static void main(String[] args) {
Rectangle r = new Rectangle();
r.width = 20;
r.height = 10;
}
}