Pregunta

I recently installed eclipse on my mac and I was fooling around with it in class. I keep getting misplaced construct errors on my first print line and a bunch on syntax error on my main declaration. I'm not really sure whats up.

 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;

}

This isn't finished but as far as I can see I have values for all my variables and everything is closed the way it should be.

¿Fue útil?

Solución

There are many errors in the code:

  • no class declaration
  • incorrect constructor call for Scanner - it doesn't accept empty arguments
  • nextDouble should have parenthesis ()
  • imput should be input, as you declared input
  • string should be String

Here's the corrected code:

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);
    }
}

Otros consejos

You have several problems:

  1. You're need to add class declaration.
  2. main parameter is String and not string.
  3. imput should be input.

You're missing the class declaration! In Java everything must be inside a class. This is very different from C/C++, Python, and a lot of other languages that support functions (Java only has methods).

E.g., if this is all in a file called MyTest.java then:

 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;
  }
 }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top