2012-08-10 86 views
-2

Eclipse中不斷給我下面的錯誤:的方法。如果(布爾)是未定義的類型

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
input cannot be resolved 
The method If(boolean) is undefined for the type bai1DinhLuatCuLong 
Syntax error, insert ";" to complete Statement 
F cannot be resolved to a variable 

我不明白這一點。我需要解決什麼問題?爲什麼我不能使用If()函數?

我已經嘗試通過谷歌和必應,並且我已經檢查了我的Java示例書,但我無法解決這個問題。

import java.util.Scanner; 


public class bai1DinhLuatCuLong 
{ 
    public static void main(String[] args) 
    { 
    System.out.print("Giá trì cần tìm (F // q // r) : "); 
    char cantim = input.nextChar(); 

      // HERE THIS IS WHERE I PUT THE IF STATEMENT !!! 
    If (cantim == 'F') { 

     Scanner input = new Scanner(System.in); 
     System.out.print("Độ lớn của điện tích thứ nhất : "); 
     double q1 = input.nextDouble(); 
     System.out.print("Độ lớn của điện tích thứ hai : "); 
     double q2 = input.nextDouble(); 
     System.out.print("Khoảng cách giữa 2 điện tích : "); 
     double r = input.nextDouble(); 
     System.out.print("Hệ số k sẽ tự động được đặt là 9*10^9 như trong hệ SI"); 
     double F = 9 * Math.pow(10,9) * Math.abs(q1) * Math.abs(q2)/Math.pow(r,2); 
    } 

    System.out.print("Độ lớn lực tương tác giữa 2 điện tích điểm : " + "\n Lưu ý E là *10"); 

    } 
} 
+5

情況很重要。你不能使用'如果',它**必須是'如果' – pb2q 2012-08-10 15:32:43

+0

在你的IDE和突出顯示的代碼中,你可以看到'If'與其他關鍵字不同,與'public'和「靜態」,它與「掃描儀」和「系統」顏色相同。 – 2012-08-10 15:35:55

+0

你可能不想刪除這個問題 - 它可以幫助未來的人。點擊答案分數下面的勾號,接受下面的正確答案。 – Edd 2012-08-10 15:44:56

回答

12

If在java中沒有大寫字母I:應該是if

6

if (cantim == 'F')應該是小i

你需要寫一個方法

public boolean If(boolean test) 
{ 
//Your logic. 
} 

但是,根據您的代碼,看來你正在尋找"if"聲明比有新方法。

0

也許你想要做這樣的事情:

Scanner input = new Scanner(System.in); 
String cantim = input.next(); 

if (cantim.equals("F")) { 
    System.out.println("If F"); 
} else { 
    System.out.println("Out of F"); 
} 
相關問題