Java Averaging Program

£可爱£侵袭症+ 提交于 2019-12-13 06:34:08

问题


Write a class called Average that can be used to calculate average of several integers. It should contain the following methods:

 A method that accepts two integer parameters and returns their average.  A method that accepts three integer parameters and returns their average.  A method that accepts two integer parameters that represent a range. Issue an error message and return zero if the second parameter is less than the first one. Otherwise, the method should return the average of the integers in that range (inclusive).

I am totally new to Java and programming, this has me completely lost! Here's what I've tried.

import java.util.Scanner;

public class Average {

    public static void main(String[] args) {

    double numb1, numb2, numb3;
    System.out.println("Enter two numbers you'd like to be averaged.");
    Scanner keyboard = new Scanner(System.in);
    numb1 = keyboard.nextInt();
    numb2 = keyboard.nextInt();

    }

        public double average (int num1, int num2) {

            return (num1 + num2) / 2.0; 
    } 

        public double average (int num1, int num2, int num3) 
        { 
         return (num1 + num2 + num3) / 3.0; 
        } 

}

The program doesn't go past getting the values from the user. Please help!


回答1:


You have to actually call your methods.

Just place

Average avg = new Average();
System.out.println("The average is: " + avg.average(numb1, numb2));

at the end of your main method.

Alternatively you can make the methods static:

public static double average (int num1, int num2) {
    return (num1 + num2) / 2.0; 
} 

More info on constructors and static.




回答2:


It looks like your not actually printing out the results. Try the following.

    System.out.print(average(numb1, numb2));                                            



回答3:


Let's detail what you did there.

public static void main(String[] args) {   
//Create variables numb1, numb2 & numb3
double numb1, numb2, numb3;
System.out.println("Enter two numbers you'd like to be averaged.");
//Read standard input (keyboard)
Scanner keyboard = new Scanner(System.in);
//Retrieve first input as an int
numb1 = keyboard.nextInt();
//Retrieve second input as an int
numb2 = keyboard.nextInt();
}

Then your two next methods compute for two or three given integers their average. The main method is the first method called during your program execution. The jvm will execute everything inside. So it will declare the three doubles, read two values from keyboard and then end.

If you want to compute the average of numb1 & numb2 using your method, you have to create an object Average and call your average method like this

public static void main(String[] args) {   
//Create variables numb1, numb2 & numb3
double numb1, numb2, numb3;
System.out.println("Enter two numbers you'd like to be averaged.");
//Read standard input (keyboard)
Scanner keyboard = new Scanner(System.in);
//Retrieve first input as an int
numb1 = keyboard.nextInt();
//Retrieve second input as an int
numb2 = keyboard.nextInt();
//Declare the average value
double average;
//Create an average instance of the class average
Average averageObject = new Average();
//Call your average method
average = averageObject.average(numb1,numb2);
//Print the result
System.out.println("Average is : " + average);
}

Everything in Java is object (read about Object Oriented Programming). Writing your class "Average" defines how your object is structured. It has attributes (characteristics) and methods (actions). Your Average object has no attributes. However it has two methods (average with two and three numbers) acting on integers. However your class is just the skeleton of your object. You need to create an object from this skeleton using the keyword new as :

Average averageObject = new Average();

Sincerely




回答4:


If you'd like your program to find the average you need to include a call to that method in your main method.

numb1 = keyboard.nextInt();
numb2 = keyboard.nextInt();
System.out.println("The average of " + numb1 + " and " + numb2 + " is " + average(numb1,numb2);
}



回答5:


you need to call the methods that you have written after you accept the input.

...

System.out.println("Enter two numbers you'd like to be averaged.");
Scanner keyboard = new Scanner(System.in);
numb1 = keyboard.nextInt();
numb2 = keyboard.nextInt();
System.out.println(average (int numb1 , int numb2 ))

...

You probably want to provide a menu of options for the user to select to determine which method to call

System.out.println("Select one option");
System.out.println("1. Enter two numbers you'd like to be averaged.");
System.out.println("2. Enter the 3 numbers you want averaged.");
System.out.println("3. Enter the number Range you want averaged.");

and based on that answer you can determine which method to call




回答6:


After the access specifier (public) and before the return type (double) place the Java keyword static. You shouldn't worry about what this means right now.




回答7:


You have to use bitwise operators. average of int a and b can be calculated as avg= (a >> 1) + (b >> 1) + (((a & 1) + (b & 1)) >> 1);




回答8:


The main method will only execute what it is asked to. If you want the average methods to be executed, you will have to create an object, pass the required variable and call the methods from the main method. Write the following lines in the main method after accepting the input.

Average avrg = new Average();
System.out.println("The average is: " + avrg.average(numb1, numb2, numb3));

Write only numb1 and numb2 if you want to average only two numbers.



来源:https://stackoverflow.com/questions/19507108/java-averaging-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!