本文是为了记录自己学习java过程的 才开始认真学java 代码可能写的比较挫 如果你是抱着学习的心态来看我的这篇博客 那我建议你去网上找找其他写的更好的人的~~
因为不熟悉这种语言 所以在做这道非常简单的题的时候也让我觉得很麻烦
在这道题中我尝试用foreach方法来访问这个类数组的时候发生了错误 运行过程出错了 我不知道为什么 先留着这个问题吧 我至今还是不知道为什么
foreach方法遍历这个数组 然后我也没有改变数组中的内容 只是用来维护了一个最大值和最小值
然后这道题其实可以在空间复杂度上进一步优化 简化掉所有数组 但是因为是为了熟悉语言来的 我并没有这么做~~~
当然如果你看到我的代码了 并且发现了我代码中写的有什么不好的地方 还请不吝赐教 万分感谢~~
点击打开链接 <-----------我尝试去读懂这个人写的代码 但是我并没有看懂 留着以后来改进这个代码的时候在看吧
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
Integer student_nums;
Student[] Goods= new Student[200];
Scanner cin = new Scanner(System.in);
while (true) {
student_nums = cin.nextInt();
if(student_nums == -1)break;
// System.out.println("The value of stedent_nums is " + student_nums);
for (int i = 0; i < student_nums; i ++) {
Goods[i] = new Student(cin.nextInt() , cin.nextInt() , cin.nextInt(), cin.next(), i);
}
// System.out.println("Here it is ~~~~");
Integer maxx = -0x7fffffff, max_id = 0;
Integer minn = 0x7fffffff, min_id = 0;
/* for (Student i : Goods) {
if(i.value > maxx) {
maxx = i.value;max_id = i.id;
}
if (i.value < minn) {
minn = i.value;min_id = i.id;
}
}*/
for (int i = 0; i < student_nums; i ++) {
if (Goods[i].value > maxx) {
maxx = Goods[i].value;max_id = i;
}
if (Goods[i].value < minn) {
minn = Goods[i].value;min_id = i;
}
}
System.out.println(Goods[max_id].name + " took clay from " + Goods[min_id].name + ".");
}
}
}
class Student {
Integer length;
int id;
Integer wide;
Integer high;
Integer value;
String name;
Student(Integer length , Integer wide, Integer high , String name,int id) {
this.length = length; this.wide = wide;
this.high = high; this.name = name;
this.value = length * wide * high;
this.id = id;
}
}