第一周作业
-
Java三大特性:封装,继承,多态;
-
定义三个参数, a,b; 交换 a,b的值:
例如:
输入: 1 5
输出:a = 5 ; b = 1
import java.util.Scanner;
public class ShiYiShi {
public static void main(String[] args) {
int a;
int b;
int tmp;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextInt();
tmp = a;
a = b;
b = tmp;
System.out.println("a = "+a+" ; "+"b = "+b);
}
}
-
定义 一个数组; 输入10个数(x>=0&&x<=100);将这10个数分为3个等级;
统计A,B,C等级个数; (A>=90;B>=60&&B<90;C<60)
例如 :
输入: 59 60 70 80 88 45 90 100 95 96
输出: A: 4 B: 4 C:2
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class ShiYiShi {
public static void main(String[] args) {
int[] array = new int[10];
int countA = 0;
int countB = 0;
int countC = 0;
Map<Character,Integer> hashmap = new HashMap<>();
hashmap.put('A',countA);
hashmap.put('B',countB);
hashmap.put('C',countC);
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
array[i] = sc.nextInt();
if (array[i] >= 90) {
hashmap.put('A',++countA);
}else if (array[i] >= 60) {
hashmap.put('B',++countB);
} else if (array[i] < 60) {
hashmap.put('C',++countC);
}
}
System.out.println("A: "+hashmap.get('A')+" "+" B: "+hashmap.get('B')+" C: "+hashmap.get('C'));
}
}
- 求1-100 的和
public class ShiYiShi {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 100; i++) {
count += i;
}
System.out.println(count);
}
}
- 定义一个数组,输入 5 个数,输出大于50的数
例如:
输入: 40 50 45 62 50
输出: 62
import java.util.Scanner;
public class ShiYiShi {
public static void main(String[] args) {
int[] array = new int[5];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
array[i] = sc.nextInt();
}
for (int i = 0; i < 5; i++) {
if (array[i] > 50){
System.out.print(array[i]+" ");
}
}
}
}