Can i do multiple switch case in 1 method in java?

107 views Asked by At

I'm trying to create a program for a competition using java where every participant gets 3 tries and the final result is the addition of all of those tries. In every try the participants may hit target 1-10 where target 1=100 points and target 10=10 points with the default value of 0.

The program will request for the target hit at the main class and insert that into the competition class where switch cases will happen to determine the points they get. The point i'm confused about it, how do i do switch cases for all 3 tries? I've tried making each of them individually within a method and then calling them from another method to get the total but it either displays 0 or the total of the target instead.

Here is what i've tried so far:

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;
    }
}

I tried changing the return (nl1+nl2+nl3); to return (getC1() + getC2() + getC3()) which resulted in the total amount of tries being displayed instead (for example if test1=1, test2=2, test3=3, displayed will be 6). From that i believe the main class is already fine as it has inserted the amount of tries and displayed the result correctly, it's just the switch cases that needs fixing. Can someone explain to me what i did wrong there? Or is this question still too vague?

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("======================================================");
            }
        }
    }
1

There are 1 answers

6
Joachim Sauer On

There's many things wrong and I'm not entirely sure what kind of misunderstanding caused them, so I'll just list them as I see them:

  1. your setScore and setEnd1 methods don't actually set anything. They should be called something like calculateScore or calculateEnd.
  2. your setScore method takes test1, test2 and test3 as parameters even though those are already fields. Usually you want to do one or the other, doing both is confusing.
  3. your setScore method is defined to return int but doesn't ever return anything other than 0
  4. you call the setEnd1 method from your constructor, which adds nl1, nl2 and nl3, but those haven't been set at this point
  5. Your Arrow1 class extends Arrow which seems wrong: there is no obvious reason to duplicate all those fields and it's probably a mistake.
  6. You call getC1(), getC2() and getC3() in Arrow1.getScore() but the variables behind those methods have never been initialized (unless that happens in the code that we don't see).