如何修复此错误:无法将结果解析为变量

时间:2017-01-02 07:15:09

标签: java

我输入了这个java程序来计算Eclipse中的简单兴趣。

import java.util.Scanner;

public class PercentageCalculator 
{
public static void main(String[] args)
{
    double x = 0;
    double y = 0;

    Scanner scanner = new
            Scanner(System.in);
    System.out.println("Enter the value of x: ");
    x = scanner.nextDouble();
    System.out.println("Enter the value of y: ");
    y = scanner.nextDouble();
    System.out.println();
    System.out.println("Calculating percentage: (x % of y): ");

    System.out.println(x + " % of" + y + "is " + result);
    System.out.println(); 
}

}

但是当我试图运行它时,它给了我这样的信息:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
result cannot be resolved to a variable

at PercentageCalculator.main(PercentageCalculator.java:19)

这是什么意思,我该如何解决?

2 个答案:

答案 0 :(得分:2)

声明结果变量

double result = 0;

然后计算百分比并将其分配给结果变量。

result = your calculation;

然后打印

System.out.println(x + " % of" + y + "is " + result);

希望这会有所帮助:)

答案 1 :(得分:0)

您尚未声明结果变量

double result=0;