Intellij Idea 我用java创建了一个程序,它应该有一个构造函数,这个构造函数接受参数“形状”和“重量”,但是我搞砸了,用一个[关闭]创建了这个程序,

8mmmxcuj  于 2022-12-17  发布在  Java
关注(0)|答案(1)|浏览(190)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
2天前关闭。
Improve this question
我做了一个带参数的构造函数,现在我很难重构来取两个参数,这样我的主类才能运行。我应该调整什么,Intellij idea告诉我的是要么删除第二个参数,要么做一个带参数“Shape”的新构造函数,这基本上是一样的事情。如果你想更好地解释程序应该做什么,请马上询问,先谢了!
所以最终的目标是基本上运行完全相同的代码,因为代码是有功能的,但是它没有经过codeGrade。

public class ChocolatePiece {
    String Shape;

    int weight;

    boolean eaten = false;

    public ChocolatePiece(String Shape) {
        this.Shape = Shape;
        switch (Shape) {
            case "Santa" -> weight = 8;
            case "tree" -> weight = 6;
            case "star" -> weight = 7;
        }
    }

    public int getWeight() {
        return weight;
    }

    public String getShape() {
        return Shape;
    }

    public boolean isEaten() {
        return eaten;
    }
}

import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Main meow= new Main();
        meow.openCalender(meow.storeChocolateObjects());
    }
    public ChocolatePiece[] storeChocolateObjects() {
        String[] strAr1 = new String[] {"Santa", "tree", "star"};
        ChocolatePiece[] arr = new ChocolatePiece[24];
        for (int i = 0; i < 24; i++) {
            Random rand = new Random();
            int j = rand.nextInt(3);
            arr[i] = new ChocolatePiece(strAr1[j]);
        }
        return arr;
    }
    public void openCalender(ChocolatePiece[] arr) {

        Scanner window = new Scanner(System.in);

        while (true) {

            System.out.println("Choose window: ");
            int num = Integer.parseInt(window.nextLine());
            if (num < 1 || num > 24) {
                System.out.println("Window is out of range");
                continue;
            }
            if (arr[num-1].isEaten()) {
                System.out.println("Chocolate Piece is eaten");
            }
            else {
                System.out.println(arr[num-1].getShape() + ", " + arr[num-1].getWeight());
                arr[num-1].eaten = true;
            }
            int count = 0;

            for (ChocolatePiece chocolatePiece : arr) {
                if (chocolatePiece.isEaten()) {
                    count++;
                }
            }
            if (count == arr.length) {
                break;
            }

        }
    }
}
yb3bgrhw

yb3bgrhw1#

添加重载/new构造函数:

public ChocolatePiece(String Shape, int weight) {
    this.Shape = Shape;
    this.weight = weight;
}

然后,在storeChocolateObjects()中,您需要以某种方式选择一个权重......可能是以类似于您已有的随机方式:

public ChocolatePiece[] storeChocolateObjects() {
    Random rand = new Random();
    String[] strAr1 = new String[] {"Santa", "tree", "star"};
    int[] weights = {8, 6, 7};
    ChocolatePiece[] arr = new ChocolatePiece[24];
    for (int i = 0; i < arr.length; i++) {
        int j = rand.nextInt(strAr1.length);
        arr[i] = new ChocolatePiece(strAr1[j], weights[j]);
    }
    return arr;
}

不确定是否需要去掉一个参数构造函数来通过codeGrade测试。
这实际上解决了参数的问题,但是权重分配给了不同的形状,星星是7 g,圣诞老人是8 g,树是6 g
我把权重按照类型的顺序放进数组,现在你只需要为随机选择获取相应的权重。

相关问题