在千锋逆战学习的第24天
假如我不能,我一定要;假如我一定要,我就一定能!加油!
今天学习了泛型和Collections工具类。
作业题:
11.有如下代码
public class TestMyClass {
public static void main(String[] args) {
IA ia = new MyClass();
System.out.println(ia);
}
}
interface IA{
void ma();
}
class MyClass implements IA{
public void ma(){}
public String toString(){
return "MyClass toString()";
}
}
答案C
import java.util.Scanner;
public class TestRandom {
public static void main(String[] args) {
int a = 0;
StringBuilder sb = new StringBuilder();
java.util.Random r = new java.util.Random();
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
int length = sc.nextInt();
for (int j = 0; j < length; j++) {
// 大写65-90,小写97-122,数字48-57
int choice = r.nextInt(3);
switch (choice) {
case 0:
a = r.nextInt(26) + 65;// 大写字母对应的ASCII码
break;
case 1:
a = r.nextInt(26) + 97;// 小写字母
break;
case 2:
a = r.nextInt(10) + 48;// 数字
}
// 转成字符
char c = (char) a;
// 存入
sb.append(c);
}
String str = sb.toString();
System.out.println(str);
}
}
运行结果
import java.util.*;
public class TestCheckString {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入字符串:");
String s1 = input.next();
System.out.println("请输入子串:");
String s2 = input.next();
int b = 0;
for(int i = 0 ; i<s1.length(); i++){
int a = s1.indexOf(s2,i);
if(a != -1){
i = a;
b++;
System.out.println("该子串第"+b+"次出现的下标"+a);
}
}
}
}
运行结果
import java.util.*;
public class TestStudent {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("Tom",18,100,"class05"));
list.add(new Student("Jerry",22,70,"class04"));
list.add(new Student("Owen",25,90,"class05"));
list.add(new Student("Jim",30,80,"class05"));
list.add(new Student("Steve",28,66,"class06"));
list.add(new Student("Kevin",24,100,"class04"));
double scores = 0;
for (int i = 0; i < list.size(); i++) {
double score = list.get(i).score;
scores = scores + score;
}
System.out.println("所有学生的平均成绩为:" + scores/list.size());
double[][] classScore = new double[7][2];
for (int i = 0; i < list.size(); i++) {;
Character ch = list.get(i).classNum.charAt(6);
int ci = Integer.valueOf(ch.toString());
classScore[ci - 1][0]++;
classScore[ci - 1][1] = (classScore[ci - 1][1] +list.get(i).score);
}
for (int i = 0; i < classScore.length; i++) {
if(classScore[i][0] != 0) {
classScore[i][1] = classScore[i][1] / classScore[i][0];
System.out.println("class0" + (i + 1) + "的平均成绩:" + classScore[i][1]);
}else {
System.out.println("class0" + (i + 1) + "没有成绩");
}
}
}
}
class Student{
String name;
int age;
double score;
String classNum;
public Student(String name, int age, double score, String classNum) {
this.name = name;
this.age = age;
this.score = score;
this.classNum = classNum;
}
}
运行结果
欢迎指正