在printf行上找不到符号

时间:2014-10-17 00:54:32

标签: java

对此错误进行了一些研究,而且大多数看似简单。我查看是否有一些拼写错误,我没有注意到任何。我试图用printf打印几行。当我去编译这段代码时,它给了我一个"找不到符号错误"。可能是这些方法需要在方法中定义变量吗?错误只在我的printSales方法中完成。这不是成品,所以我可能会缩短一两件。但这是我的代码,任何帮助将不胜感激。

import java.util.Scanner;

public class Sales
{

    public static void main( String[] args)
    {
    Scanner input = new Scanner (System.in);
    System.out.print("Please enter a product number.");
    double product1 = 0;
    double product2 = 0;
    double product3 = 0;
    double product4 = 0;
    double product5 = 0;
    double sale;

    int userInput = input.nextInt();
        while(userInput != 0)
        {   
           if(userInput >= 1 && userInput <= 5)
           {
            switch(userInput)
            {
                case 1:  System.out.println("");
                        product1 ++;
                        sale += 2.98;
                         break;

            System.out.print("How many were sold?");             
            userInput = input.nextInt();

                case 2:  System.out.println("");
                        product2 ++;
                        sale += 4.50;
                         break;

            System.out.print("How many were sold?");
            userInput = input.nextInt();

                case 3: System.out.println("");
                        product3 ++;
                        sale += 9.98;
                         break;

            System.out.print("How many were sold?");
            userInput = input.nextInt();

                case 4: System.out.println("");
                        product4 ++;
                        sale += 4.49;
                         break;

            System.out.print("How many were sold?");
            userInput = input.nextInt();

                case 5:System.out.println("");
                        product5 ++;
                        sale += 6.87;
                         break;
            System.out.print("How many were sold?");
            userInput = input.nextInt();

            }//end switch
            } //end if
        else if (userInput != 0)
            bSystem.out.println("ERROR: Incorrect Product Number, Please Try Again");
            System.out.print("");
            userInput = input.nextInt();
        }//end while 
    } //end main
    public void printSales()
    {
    System.out.println();
    System.out.printf("Product 1: $%.2f\n", product1);
    System.out.printf("Product 2: $%.2f\n", product2);
    System.out.printf("Product 3: $%.2f\n", product3);
    System.out.printf("Product 4: $%.2f\n", product4);
    System.out.printf("Product 5: $%.2f\n", product5);
    } //end printSales
} //end class 

3 个答案:

答案 0 :(得分:2)

这是变量范围的问题。您不能在另一种方法中引用一种方法中的变量。尝试重新定义printSales:

public void printSales(int product1, int product2, int product3, int product4, int product5)
{
    System.out.println();
    System.out.printf("Product 1: $%.2f\n", product1);
    System.out.printf("Product 2: $%.2f\n", product2);
    System.out.printf("Product 3: $%.2f\n", product3);
    System.out.printf("Product 4: $%.2f\n", product4);
    System.out.printf("Product 5: $%.2f\n", product5);
} //end printSales

然后当然你必须改变你打电话给printSales的方式(我看到你现在没有打电话,但也许你将来会打电话):

printSales(product1, product2, product3, product4, product5);

确保product1,product2等都在相同的“范围”中 - 您无法进入另一种方法,或者进入if {}或{}或while {}阻止引用变量。< / p>

答案 1 :(得分:1)

四个问题......

1

sale might not be initialised ...

您将sale变量声明为...

double sale;

作为局部变量,它没有默认值,因此执行sale += 2.98之类的操作会产生未定义的结果。

简单地将变量初始化为默认/起始值...

double sale = 0d;

2

  

包bSystem不存在
bSystem.out.println(&#34;错误:产品编号不正确,请再试一次&#34;);

错字,它应该是System.out.println;)

3

  

错误:找不到符号           System.out.printf(&#34;产品1:$%。2f \ n&#34;,product1);

product1product2product3product4product5printSales中没有任何意义,因为它们只有#{1}}已在main中声明为局部变量。

通常情况下,我建议制作这些实例变量,但由于您在static的{​​{1}}上下文中运行,因此无法正常工作。相反,修改main方法以获取参数...

printSales

4

您应该收到一些关于&#39;无法访问的语句public void printSales(double product1, double product2, double product3, double product4, double product5) { ... } break , this caused by the fact that you switch`语句的警告,但是会尝试执行代码...

out of the

相反,请尝试在case 1: System.out.println(""); product1++; sale += 2.98; break; System.out.print("How many were sold?"); userInput = input.nextInt(); ^--- These can't be executed, because of the break before it... 语句之前移动代码....

break

您可能需要查看the Variables trailDefining Methods了解更多详情

答案 2 :(得分:0)

变量&#34; double product1 / 2/3 ......&#34;是本地变量,即它们的范围和生命周期在主方法中受到限制 - 您无法从printSales()到达它们。

要解决问题并能够修改/调用变量,请将它们改为字段。也就是说,将它们放在的上下文中,而不是方法

public class Sales
{    
//variables moved into the context of the class, i.e. you can reach them from
//all methods of this class. Furhermore, they have to be static in order to be
//accessed from a static method.
private static double product1 = 0;
private static double product2 = 0;
private static double product3 = 0;
private static double product4 = 0;
private static double product5 = 0;
private static double sale;

public static void main( String[] args)
{
    Scanner input = new Scanner (System.in);
    System.out.print("Please enter a product number.");
    //rest of main method but without vairables
}