gpt4 book ai didi

java - 为什么我的类显示为未使用,因此无法编译我的程序?

转载 作者:行者123 更新时间:2023-12-01 19:28:17 25 4
gpt4 key购买 nike

对于java来说非常新,在我的类(class)(在学校没有这个程序)中,我们将设计一个存储重量的类和一个返回运费的方法。我显然对自己在这里所做的事情感到非常困惑。我有这段代码,但甚至无法编译,因为它显示我的类未被使用。任何帮助都会很棒。

import javax.swing.JOptionPane;

public class ShippingCharges {
public float weight;

public void myPublicMethod() {
weight = Float.parseFloat(JOptionPane.showInputDialog(null, "What is the weight in kg?: ", JOptionPane.QUESTION_MESSAGE));
}

public static double main(String[] args) {
ShippingCharges charge = new ShippingCharges();
charge.myPublicMethod();

if (charge.weight >= 2) {
System.out.println("The shipping charge for 2kg or less for every 500 miles is $1.10");
return 1.10;
}
if (charge.weight > 2 && charge.weight <= 6) {
System.out.println("The shipping charge for over 2kg up to 6kg " +
"for every 500 miles is $2.20");
return 2.20;
}
if (charge.weight > 6 && charge.weight <= 10) {
System.out.println("The shipping charge for over 6kg up to 10kg " +
"for every 500 miles is $2.20");
return 3.70;
} else {
System.out.println("The shipping charge for anything over 10kg" +
"for every 500 miles is $4.80");
return 4.80;
}
}
}

如果之前有人问过这个问题,我也很抱歉。好像没找到

最佳答案

main 不返回 double 值。双返回应该在你的方法上。 main 的返回类型为 void。

试试这个:我冒昧地将 float 更改为双倍。如果需要,您可以将它们改回来。

public class ShippingCharges {
public double weight;

public static double myPublicMethod() {
ShippingCharges charge = new ShippingCharges();
double weight = Double.parseDouble(JOptionPane.showInputDialog(null, "What is the weight in kg?: ", JOptionPane.QUESTION_MESSAGE));
if (charge.weight >= 2) {
System.out.println("The shipping charge for 2kg or less for every 500 miles is $1.10");
return 1.10;
}
if (charge.weight > 2 && charge.weight <= 6) {
System.out.println("The shipping charge for over 2kg up to 6kg " +
"for every 500 miles is $2.20");
return 2.20;
}
if (charge.weight > 6 && charge.weight <= 10) {
System.out.println("The shipping charge for over 6kg up to 10kg " +
"for every 500 miles is $2.20");
return 3.70;
} else {
System.out.println("The shipping charge for anything over 10kg" +
"for every 500 miles is $4.80");
return 4.80;
}
}


public static void main(String[] args) {

myPublicMethod();

}
}

关于java - 为什么我的类显示为未使用,因此无法编译我的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60725582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com