0

我正在尝试使用java为比赛创建一个程序,每个参与者都会尝试3次,最终结果是所有这些尝试的加法。在每次尝试中,参与者可能会达到目标 1-10,其中目标 1=100 点,目标 10=10 点,默认值为 0。

该程序将请求主类的目标命中并将其插入到将发生切换情况的比赛类中以确定他们获得的分数。我对此感到困惑,我如何为所有 3 次尝试切换案例?我尝试在一个方法中单独制作它们中的每一个,然后从另一个方法调用它们以获取总数,但它要么显示 0,要么显示目标的总数。

这是我到目前为止所尝试的:

class Arrow {
    private int test1;
    private int test2;
    private int test3;
    private int end1;
    public int nl1, nl2, nl3;

    //constructor
    public Arrow() {
    }

    public Arrow(int test1, int test2, int test3) {
        this.test1 = test1;
        this.test2 = test2;
        this.test3 = test3;
        this.end1 = setEnd1();
    }

    //setter
    public void setScore(int test1, int test2, int test3) {
        this.test1 = test1;
        this.test2 = test2;
        this.test3 = test3;
        this.end1 = setEnd1();
    }

    public void setC1(int test1) {
        this.test1 = test1;
    }

    public void setC2(int test2) {
        this.test2 = test2;
    }

    public void setC3(int test3) {
        this.test3 = test3;
    }
    
    public int setScore(int i, int test1, int test2, int test3) {
        nl1 = switch (test1) {
            case 1 -> 100;
            case 2 -> 90;
            case 3 -> 80;
            case 4 -> 70;
            case 5 -> 60;
            case 6 -> 50;
            case 7 -> 40;
            case 8 -> 30;
            case 9 -> 20;
            case 10 -> 10;
            default -> 0;
        };
        nl2 = switch (test2) {
            case 1 -> 100;
            case 2 -> 90;
            case 3 -> 80;
            case 4 -> 70;
            case 5 -> 60;
            case 6 -> 50;
            case 7 -> 40;
            case 8 -> 30;
            case 9 -> 20;
            case 10 -> 10;
            default -> 0;
        };
        nl3 = switch (test3) {
            case 1 -> 100;
            case 2 -> 90;
            case 3 -> 80;
            case 4 -> 70;
            case 5 -> 60;
            case 6 -> 50;
            case 7 -> 40;
            case 8 -> 30;
            case 9 -> 20;
            case 10 -> 10;
            default -> 0;
        };
        return 0;
    }
    
    private int setEnd1() {
        return (nl1+nl2+nl3);
    }

    //getter
    public int getC1() {
        return test1;
    }

    public int getC2() {
        return test2;
    }

    public int getC3() {
        return test3;
    }

    //hitung nilai akhir
    public int getEnd1() {
        return end1;
    }
}

class Arrow1 extends Arrow{
    private Use ps;
    private Arrow[] score;

    public Arrow1() {

    }

    public Panah getScore(int i) {
        Arrow nl = new Arrow();
        nl.setScore(getC1(), getC2(), getC3());
        return nl;
    }
}

我尝试将其更改return (nl1+nl2+nl3);return (getC1() + getC2() + getC3())导致显示的尝试总数(例如,如果 test1=1、test2=2、​​test3=3,则显示为 6)。从那我相信主类已经很好,因为它已经插入了尝试的数量并正确显示了结果,它只是需要修复的开关盒。有人可以向我解释我在那里做错了什么吗?还是这个问题还是太模糊了?

static void Score1() {
        System.out.println("Scores");
        System.out.println("======================================================");
        Scanner objN = new Scanner(System.in);
        for (int i = 0; i < b; i++) {
            for (int j = 0; j < a; j++) {
                Use ps = lhs[j].getPs();
                String name = ps.getName();
                String nr = ps.getNr();
                System.out.println("Name: " + name + "   Number: " + nr);
                System.out.print("Input for try 1                : ");
                int test1= objN.nextInt();
                System.out.print("Input for try 2                : ");
                int test2= objN.nextInt();
                System.out.print("Input for try 3                : ");
                int test3= objN.nextInt();
                lhs[j].setScore(test1, test2, test3);
                System.out.println("======================================================");
            }
        }
    }
4

1 回答 1

2

错误的地方很多,我不完全确定是什么误解造成的,所以我将我看到的列出来:

  1. 你的setScoresetEnd1方法实际上并没有设置任何东西。它们应该被称为类似calculateScoreor的东西calculateEnd
  2. 您的setScore方法采用test1,test2test3作为参数,即使它们已经是字段。通常你想做一个或另一个,同时做这两个是令人困惑的。
  3. 你的setScore方法被定义为返回int,但不会返回任何东西0
  4. setEnd1您从构造函数调用该方法,该方法添加和nl1,但此时尚未设置nl2nl3
  5. 您的Arrow1类扩展Arrow了这似乎是错误的:没有明显的理由复制所有这些字段,这可能是一个错误。
  6. 您调用getC1(),getC2()getC3()inArrow1.getScore()但这些方法背后的变量从未被初始化(除非在我们看不到的代码中发生这种情况)。
于 2021-10-16T11:10:48.117 回答