简体   繁体   English

声明时无法使用扫描仪,无法解析 sc

[英]Scanner cannot be used when declared, sc cannot be resolved

The only problem I'm having with my program is the scanner, I've used it many times but in this program it won't run right.我的程序唯一遇到的问题是扫描仪,我已经用过很多次了,但是在这个程序中它不能正常运行。 The error is in the public static double getCandlecost() and the public static int getShippingType() methods.错误出在 public static double getCandlecost() 和 public static int getShippingType() 方法中。 under the int shippingType = sc.nextInt();在 int shippingType = sc.nextInt(); 下and the double candleCost = sc.nextDouble();和双烛成本 = sc.nextDouble(); both say "sc cannot be resolved" and in my main class I did declare it.两者都说“sc 无法解决”,并且在我的主课中我确实声明了它。

  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));

}

}

Your scanner is out of scope.您的扫描仪超出范围。 Since you have declared it in the main() method, only that method can access it.由于您已在main()方法中声明它,因此只有该方法可以访问它。 Your scanner needs to be static as well.您的扫描仪也需要是静态的。 Write it like this instead:改为这样写:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM