2013-04-22 37 views
-4

我最近在我的Mac上安裝了eclipse,我在課堂上與它混在一起。我在我的第一行打印行上不斷髮生錯誤的構造錯誤,並在我的主要聲明中出現了一些語法錯誤。我真的不知道怎麼回事。Java錯位結構

import static java.lang.System.out; 
import java.util.Scanner; 


public static void main (string args[]) 
    { 

double a, b, c, d, e, f; 

Scanner input = new Scanner(); 
out.println(" Please enter the first number: "); 
a = imput.nextDouble; 
out.println("Please enter the second number: "); 
b = imput.nextDouble; 
out.println ("Please enter the third number : "); 
c = imput.nextDouble; 
out.println ("Please enter in fourth number : "); 
d = imput.nextDouble; 
out.println(" Please enter in fifth number : "); 
e = imput.nextDouble; 



double sum = a + b + c + d + e; 

}

這還沒完,但據我可以看到我有值,我的所有變量,一切都被關閉的事情應該是這樣。

+0

您還沒有一個類下保持它。 – 2013-04-22 15:29:13

+0

您的代碼中存在很多問題,在我的回答中,您會看到完整的運行代碼,修復程序和解釋錯誤。 – Jops 2013-04-22 15:38:18

+0

你應該真正密切關注你在課堂上學習的內容和/或使用書籍或教程。 Java不是一種可以通過「無所事事」正確學習的語言 – madth3 2013-04-22 23:58:25

回答

2

有許多錯誤代碼:

  • 沒有類聲明
  • 構造函數不正確呼籲Scanner - 它不接受空參數
  • nextDouble應該有括號( )
  • imputinput,因爲你宣佈input
  • 字符串應該是字符串

下面是更正後的代碼:

import static java.lang.System.out; 
import java.util.Scanner; 

class MyClass { 
    public static void main(String args[]) { 

     double a, b, c, d, e, f; 

     Scanner input = new Scanner(System.in); 
     out.println(" Please enter the first number: "); 
     a = input.nextDouble(); 
     out.println("Please enter the second number: "); 
     b = input.nextDouble(); 
     out.println("Please enter the third number : "); 
     c = input.nextDouble(); 
     out.println("Please enter in fourth number : "); 
     d = input.nextDouble(); 
     out.println(" Please enter in fifth number : "); 
     e = input.nextDouble(); 

     double sum = a + b + c + d + e; 
     out.println("Sum is : " + sum); 
    } 
} 
1

你錯過了類的聲明!在Java 中,一切必須在類中。這是從C/C++,Python和很多支持功能其他語言(Java只有方法)非常不同。

例如,如果這是在所有文件名爲MyTest.java則:

import static java.lang.System.out; 
import java.util.Scanner; 

public class MyTest { 

    public static void main (string args[]) 
    { 

    double a, b, c, d, e, f; 

    Scanner input = new Scanner(); 
    out.println(" Please enter the first number: "); 
    a = imput.nextDouble; 
    out.println("Please enter the second number: "); 
    b = imput.nextDouble; 
    out.println ("Please enter the third number : "); 
    c = imput.nextDouble; 
    out.println ("Please enter in fourth number : "); 
    d = imput.nextDouble; 
    out.println(" Please enter in fifth number : "); 
    e = imput.nextDouble; 



    double sum = a + b + c + d + e; 
    } 
} 
+1

代碼中還存在其他一些小問題,但類定義修復了您提到的特定錯誤。有關更多問題,請參閱@ baraky的答案。 – DaoWen 2013-04-22 15:32:37

2

您有幾個問題:

  1. 你需要添加類聲明。
  2. 主要參數是String,而不是字符串。
  3. imput應該input
+0

哇,我覺得很蠢。謝謝 – Blank1268 2013-04-22 15:36:19