0
`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+"");
}
}`

I keep getting the result that the variable fc might not have been initialized can someone please help

1 Answers1

0

Just change double fc; to double fc = 0;.

You know that fc will be assigned a value because one of the if statements will be true, but the compiler does not know this. You just need to give fc a dummy value so that the compiler can see that it will definitely have a value when you try to print it.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116