user5563815
user5563815

Reputation:

Double variable input

i'm currently learning java. I'm trying to make an example application that when we enter 4 number, the average of those number printed.

Here is my attempt:

package ave4numbers;

     import java.util.Scanner;

     public class Ave4Numbers {

          double a,b,c,d;
          double e = (a+b+c+d)/4;
          Scanner sc = new Scanner(System.in);

          public static void main(String[] args) {

               System.out.println("Enter your numbers ");
               a = sc.nextDouble();
               b = sc.nextDouble();
               c = sc.nextDouble();
               d = sc.nextDouble();

               System.out.println(e);

          }

     }

But that doesn't work. what's wrong? Thanks.

Upvotes: 1

Views: 736

Answers (4)

gonzo
gonzo

Reputation: 2121

Besides the error in trying to do the math before you have your numbers, you also need to declare your variables inside your main method or make your variables static.

    public static void main(String[] args) {
        double a,b,c,d;  //These don't have to be static, if they are inside your method. 
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your numbers ");
        a = sc.nextDouble();
        b = sc.nextDouble();
        c = sc.nextDouble();
        d = sc.nextDouble();
        double e = (a+b+c+d)/4;  //Do math after we save the numbers
        System.out.println(e);
    }

or...

    static double a,b,c,d;  //If you leave these outside your method, it has to be static
    static Scanner sc = new Scanner(System.in); //This as well

    public static void main(String[] args) {

        System.out.println("Enter your numbers ");
        a = sc.nextDouble();
        b = sc.nextDouble();
        c = sc.nextDouble();
        d = sc.nextDouble();
        double e = (a+b+c+d)/4; //Do math after we save the numbers
        System.out.println(e);

    }

Static Example

public class Solution
{
    public static void main(String[] args) {
        //You can call static methods from another static method without an instance of the class. 
        //So you can do this if it is static. Notice I did not create an instance of Example. 
        Example.staticMethod();
        System.out.println(Example.staticVariable);
        //However these will NOT compile
        //Example.nonStaticMethod
        //System.out.println(Example.nonStaticVariable);

        //If you want access to the nonStaticMethod you need an instance. 
        Example myExample = new Example();
        myExample.nonStaticMethod();  //This WILL compile. 
        System.out.println(myExample.nonStaticVariable); //Will compile
    }
}

class Example{
    static String staticVariable = "";
    public String nonStaticVariable = "";
    public static void staticMethod(){
    }
    public void nonStaticMethod(){
    }
}

Upvotes: 0

Raf
Raf

Reputation: 7649

You need to initialize those variables or directly take them from input and then do the addition plus average in the print statement.

System.out.println((a+b+c+d)/4); 

Modifying the code you should have something like below:

public static void main(String[] args) {

            Scanner sc = new Scanner(System.in);

              System.out.println("Enter your numbers ");
              double a = sc.nextDouble();
              double b = sc.nextDouble();
              double c = sc.nextDouble();
              double d = sc.nextDouble();

              System.out.println((a+b+c+d)/4);

        }

Upvotes: 1

ControlAltDel
ControlAltDel

Reputation: 35011

All this happens when the class is loaded. e will be 0, since a,b,c, and d are by default initialized to 0.

      double a,b,c,d;
      double e = (a+b+c+d)/4;
      Scanner sc = new Scanner(System.in);

Then below you have not changed the value of e in your main method, so it will still be 0

           System.out.println("Enter your numbers ");
           a = sc.nextDouble();
           b = sc.nextDouble();
           c = sc.nextDouble();
           d = sc.nextDouble();

           System.out.println(e);

To fix, do

           System.out.println("Enter your numbers ");
           a = sc.nextDouble();
           b = sc.nextDouble();
           c = sc.nextDouble();
           d = sc.nextDouble();

           //set the value of e
           e = (a+b+c+d)/4;

           System.out.println(e);

Upvotes: 1

svarog
svarog

Reputation: 9839

you tried to print the value of e before calculating accepting the values of a, b, c and d, do this instead

package ave4numbers;

 import java.util.Scanner;

 public class Ave4Numbers {

      double a,b,c,d;
      Scanner sc = new Scanner(System.in);

      public static void main(String[] args) {

           System.out.println("Enter your numbers ");
           a = sc.nextDouble();
           b = sc.nextDouble();
           c = sc.nextDouble();
           d = sc.nextDouble();

           System.out.println((a+b+c+d)/4);
      }
 }

Upvotes: 0

Related Questions