2016-04-25 3 views
-2

Das einzige Problem, das ich mit meinem Programm habe, ist der Scanner, ich habe es oft verwendet, aber in diesem Programm wird es nicht richtig laufen. Der Fehler liegt in der öffentlichen statischen Doppel-getCandlecost() und den öffentlichen statischen getShippingType() - Methoden. unter dem int shippingType = sc.nextInt(); und die doppelte Kerze costCost = sc.nextDouble(); beide sagen "sc kann nicht gelöst werden" und in meiner Hauptklasse habe ich es erklärt.Scanner kann nicht verwendet werden, wenn deklariert, sc kann nicht aufgelöst werden

import java.util.Scanner; 
    import java.text.DecimalFormat; 

public class Candel { 

    public static void main(String[] args) { 

    Scanner sc = new Scanner(System.in); 

    double candleCost, shippingCost; 
    int shippingType; 

    candleCost = getCandlecost(); 
    shippingType = getShippingType(); 
    shippingCost = getShippingCost(candleCost, shippingType); 
    output(candleCost, shippingCost); 


} 

public static double getCandlecost() 
{ 
    boolean done = false; 
    do{ 
     try 
     { 
      System.out.print("Enter the cost of the candle order "); 
      double candleCost = sc.nextDouble(); 
      done = true; 
      return candleCost; 
     } catch (InputMismatchException e) 
     { 
      System.out.println("Error, Enter a dollar amount greater than 0"); 

     } 
    } while (!done); 
    return 0; 
} 

public static int getShippingType() 
{ 
    System.out.println("Enter the type of shipping: "); 
    System.out.println("1> Priority <overnight>"); 
    System.out.println("2> Express <2 business days>"); 
    System.out.println("3> Standard <3 to 7 business days>"); 
    System.out.println("Enter type number: "); 
    int shippingType = sc.nextInt(); 
    if(shippingType == 1){} 
     else if(shippingType == 2){} 
      else if(shippingType == 3){} 
    return shippingType; 

} 
public static double getShippingCost(double candleCost, int shippingType) 
{ 

     switch(shippingType) 
     { 
     case 1: 
      candleCost = 16.95 + candleCost; 
      break; 
     case 2: 
      candleCost = 13.95 + candleCost; 
      break; 
     case 3: 
      if (candleCost > 100.00){ 
       candleCost = candleCost; 
      } 
      else{ 
       candleCost = 7.95 + candleCost; 
      } 
      break; 
      } 
     return candleCost; 
} 



public static void output(double candleCost, double shippingCost) 
{ 
    DecimalFormat twoDigits = new DecimalFormat("$#,000.00"); 
    System.out.println("The candle cost of " + twoDigits.format(candleCost) + " with shipping costs of " 
      + shippingCost + " equals " + twoDigits.format(candleCost + shippingCost)); 

} 

} 
+2

Ihr Scanner ist lokal zu Haupt(). Auch wenn Sie viele Male in Java programmiert haben, aber den Umfang der Variablen nicht verstehen, ist es Zeit, die Grundlagen zu studieren. –

Antwort

1

Ihr Scanner ist nicht im Lieferumfang enthalten. Da Sie es in der main()-Methode deklariert haben, kann nur diese Methode darauf zugreifen. Ihr Scanner muss ebenfalls statisch sein. Schreiben Sie es stattdessen so:

public class Candel { 
    static Scanner sc = new Scanner(System.in); 

    public static void main(String[] args) { 
     ... 
    } 
    ... 
} 
+3

Sie müssen den Scanner statisch machen, da er statische Methoden verwendet. FYI: Ich habe nicht runtergestimmt. –

+0

Wenn ich das mache, bekomme ich die Fehler an der gleichen Stelle, aber es bietet eine schnelle Lösung, um "sc zu statisch zu ändern" – mvanderk10

+0

@ShireResident Ja, behoben. Vielen Dank. – Gendarme

Verwandte Themen