【发布时间】:2018-04-09 13:16:51
【问题描述】:
我正在为大学的编程考试练习 Java,我几乎完成了这个程序。
我有 2 节课:
public class Recording {
public int height;
public int diameter;
public int weight;
public Recording (int height, int diameter, int weight) {
this.height = height;
this.diameter = diameter;
this.weight = weight;
}
}
和所有动作都在进行的那个:
import java.io.IOException;
public class Leergut {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int[] recordings = new int[4];
total(recordings);
summary(recordings);
int total = recordings[0]+recordings[1]+recordings[2]+recordings[3]+recordings[4];
int count = recordings[0]*9+recordings[1]*10+recordings[2]*10+recordings[3]*8+recordings[4]*15;
System.out.println("Type 0:"+recordings[0]+" pcs x 9c");
System.out.println("Type 0:"+recordings[1]+" pcs x 10c");
System.out.println("Type 0:"+recordings[2]+" pcs x 10c");
System.out.println("Type 0:"+recordings[3]+" pcs x 8c");
System.out.println("Type 0:"+recordings[4]+" pcs x 15c");
System.out.println("total: "+total+"c ("+count+" pcs)");
}
public static int classify(Recording r) {
if (r.height == 250 && r.diameter == 67 && r.weight == 365) {int code = 0; return code;}
if (r.height == 300 && r.diameter == 80 && r.weight == 62) {int code = 1; return code;}
if (r.height == 300 && r.diameter == 85 && r.weight == 152) {int code = 2; return code;}
if (r.height == 250 && r.diameter == 67 && r.weight == 62) {int code = 3; return code;}
if (r.height == 250 && r.diameter == 80 && r.weight == 152) {int code = 4; return code;}
else { int code = -2; return code ;}
}
public static int deposit(int code) {
int pfand;
if(code == 0) {pfand = 9; return pfand;}
if(code == 1) {pfand = 10; return pfand;}
if(code == 2) {pfand = 10; return pfand;}
if(code == 3) {pfand = 8; return pfand;}
if(code == 4) {pfand = 15; return pfand;}
else { pfand = 0; return pfand;}
}
public static int[] total(int[] recordings) throws IOException {
int code = 5;
while (code != -2) {
Recording r = new Recording(System.in.read(), System.in.read(), System.in.read());
code = Leergut.classify(r);
int pfand = Leergut.deposit(code);
}
if (code == -2) {
System.out.println("FehlerCode -1");
}
return recordings;
}
public static int[] summary(int[] recordings) throws IOException {
int code = 5;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
while (code != -1) {
Recording r = new Recording(System.in.read(), System.in.read(), System.in.read());
code = Leergut.classify(r);
if(code == 0) {count1++;}
if(code == 1) {count2++;}
if(code == 2) {count3++;}
if(code == 3) {count4++;}
if(code == 4) {count5++;}
}
recordings[0] = count1;
recordings[1] = count2;
recordings[2] = count3;
recordings[3] = count4;
recordings[4] = count5;
return recordings;
}
}
现在的任务是创建这些:
int classify(Recording r) ->对输入进行分类,否则返回负值int deposit(int code) ->计算瓶子值多少或返回0int[] total(Recording[] r) ->获取值的总和,使用存款+分类 对于无效参数,返回 nullint[] summary(Recording[] r) ->获取分布每个代码出现的频率,索引 0 是第一个代码,依此类推... 对于无效参数,返回 null
现在我的问题是我想这样System.in.read():
250、67、365
而且它不起作用。也许有人可以告诉我我做错了什么?
还有问题:
如果我需要返回一个数组,我如何为无效参数返回“-1”或
null(抱歉,如果翻译关闭)?建议使用
Scanner吗?以前从未使用过它,所以它可能会把我搞砸,但见鬼..
我需要学习,所以我现在就开始吧!
【问题讨论】:
-
无效参数是什么意思?