【问题标题】:Java - User-Input for If-Statement & several smaller questionsJava - If 语句的用户输入和几个小问题
【发布时间】: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) ->计算瓶子值多少或返回0

  • int[] total(Recording[] r) ->获取值的总和,使用存款+分类 对于无效参数,返回 null

  • int[] summary(Recording[] r) -> 获取分布每个代码出现的频率,索引 0 是第一个代码,依此类推... 对于无效参数,返回 null

现在我的问题是我想这样System.in.read()

250、67、365

而且它不起作用。也许有人可以告诉我我做错了什么?

还有问题:

  1. 如果我需要返回一个数组,我如何为无效参数返回“-1”或null(抱歉,如果翻译关闭)?

  2. 建议使用Scanner 吗?以前从未使用过它,所以它可能会把我搞砸,但见鬼..

我需要学习,所以我现在就开始吧!

【问题讨论】:

  • 无效参数是什么意思?

标签: java input methods return


【解决方案1】:

您问了 3 个问题,实际上是两个问题(问题 1 和问题 2 的根本原因相同):

1) 为什么当我期望“System.in.read() like this : 250, 67, 365”时我的代码不起作用。

2) 我应该使用 Scanner 类吗?

您的问题是您想读取 int 类型的输入。 int 是一个数字序列。这意味着必须从输入中按顺序读取这些数字,然后组合起来创建一个实际的 int。

有些功能会为您做到这一点,有些则不会。

System.in.read 读取单个字符,因此这不是您想要的。更糟糕的是,这些单独的字符被读取为 ASCII 码,因此如果你输入“9”,那么你会得到它的 ASCII 码 (57) 并感到困惑。

Scanner 类从输入中读取数据,然后进行错误检查并将输入组合成预期的类型,如下所示: 导入 java.util.Scanner;

 Scanner sc = new Scanner(System.in);
 int i = sc.nextInt();

3) 如何为无效参数返回 null?

据我所知,您的所有函数都返回数组,该数组可以有一个空值。所以你只需要在返回之前检查参数值是否正确。但是你没有说它们应该是正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多