2017-01-02 117 views
-1

我輸入了此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) 

這是什麼意思?如何解決呢?

+0

你需要聲明它,你不使用它,並使用谷歌第一 –

+0

是'結果不能被解析爲一個變量' –

+0

你忘了定義,計算並將**結果**賦值給*'result' *。 –

回答

0

您還沒有宣佈結果變量

double result=0; 
+0

請不要通過回滾我們的改進來污損您的帖子。 – FelixSFD

2

聲明你結果變量

double result = 0; 

然後計算百分比,並將其分配給你的結果變量。

result = your calculation; 

然後打印

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

希望這將有助於:)

相關問題