-4

我最近在我的 mac 上安装了 eclipse,我在课堂上玩弄它。我在第一行打印时不断出现错误的构造错误,在我的主要声明中出现一堆语法错误。我不太确定这是怎么回事。

 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;

}

这还没有完成,但据我所知,我的所有变量都有值,并且一切都按应有的方式关闭。

4

3 回答 3

2

代码中有很多错误:

  • 没有类声明
  • 不正确的构造函数调用Scanner- 它不接受空参数
  • nextDouble 应该有括号 ()
  • imput应该是input,正如你所声明的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);
    }
}
于 2013-04-22T15:29:25.670 回答
2

你有几个问题:

  1. 您需要添加类声明。
  2. 主要参数是String而不是字符串。
  3. imput应该是input
于 2013-04-22T15:30:11.353 回答
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;
  }
 }
于 2013-04-22T15:28:24.760 回答