7.1 指定学生成绩等级
import java.util.Scanner;
public class 数组 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students:");
int number = input.nextInt();
int[] myList = new int[number];
System.out.print("Enter " + number + " 4scores");
for (int i = 0; i < myList.length; i++) {
myList[i] = input.nextInt();
}
double best = myList[0];
for (int i = 0; i < number; i++) {
if (myList[i] > best)
best = myList[i];
}
for (int i = 0; i < number; i++) {
if (myList[i] >= best - 10)
System.out.println("Student " + i + "score is " + myList[i] + " and grade is A");
else if (myList[i] >= best - 20)
System.out.println("Student " + i + "score is " + myList[i] + " and grade is B");
else if (myList[i] >= best - 30)
System.out.println("Student " + i + "score is " + myList[i] + " and grade is C");
else if (myList[i] >= best - 40)
System.out.println("Student " + i + "score is " + myList[i] + " and grade is D");
else
System.out.println("Student " + i + "score is " + myList[i] + " and grade is F");
}
}
}
7.3
import java.util.Arrays;
import java.util.Scanner;
public class 数字出现顺序 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Enter the integers between 1 and 100");
int[] a = new int[100];
int count = 0;
while (true) {
int temp = input.nextInt();
if (temp == 0)
break;
a[count] = temp;
count++;
}
Arrays.sort(a, 0, count);
int time;
for (int i = 0; i < count; i++) {
time = 1;
for (int j = i + 1; j < count; j++) {
if (a[i] == a[j] && a[j] != 0) {
time++;
a[j] = 0;
}
}
if (a[i] != 0)
System.out.println(a[i] + " occurs " + time + (time > 1 ? " times" : " time"));
}
}
}