`import java.util.Scanner;
public class Shipping
{
public static void main (String []args)
{
Scanner kb= new Scanner (System.in);
System.out.print("What is the weight of the package in pounds? ");
double wop= kb.nextDouble();
System.out.print("How far is the package being shipped in miles? ");
double d= kb.nextDouble();
double fc;
int sc;
if(d%500 !=0) sc= (int)d/500+1;
else sc = (int)d/500;
if(wop<=2)
fc= 1.10*sc;
else if (wop>2&&wop<=6)
fc= 2.20*sc;
else if (wop>6&&wop<=10)
fc= 3.70*sc;
else if (wop>10)
fc= 3.80*sc;
System.out.print("The final shipping price is $"+fc+"");
}
}`
我一直得到的结果是变量fc可能没有被初始化,有人可以帮忙
答案 0 :(得分:0)
只需将double fc;
更改为double fc = 0;
。
您知道fc
将被赋予一个值,因为其中一个if
语句将是true
,但编译器不知道这一点。您只需要为fc
提供一个虚拟值,以便编译器在您尝试打印时可以看到它肯定会有一个值。