Java平均程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19507108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:50:54  来源:igfitidea点击:

Java Averaging Program

javaaverage

提问by Jennifer Dana

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

编写一个名为 Average 的类,可用于计算多个整数的平均值。它应该包含以下方法:

? 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.

我对 Java 和编程完全陌生,这让我完全迷失了!这是我尝试过的。

import java.util.Scanner;

导入 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!

该程序不会通过从用户那里获取值。请帮忙!

回答by Jeroen Vannevel

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 mainmethod.

在你的main方法结束时。

Alternatively you can make the methods static:

或者,您可以使方法静态:

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

More info on constructorsand static.

有关构造函数static.

回答by Farmer Joe

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

回答by sirFunkenstine

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

看起来您实际上并未打印出结果。请尝试以下操作。

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

回答by Arvind

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

并根据该答案,您可以确定要调用的方法

回答by Airrr

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.

然后你接下来的两个方法计算两个或三个给定整数的平均值。main 方法是程序执行期间调用的第一个方法。jvm 将执行里面的所有内容。所以它将声明三个双打,从键盘读取两个值然后结束。

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

如果您想使用您的方法计算 numb1 和 numb2 的平均值,您必须创建一个对象 Average 并像这样调用您的平均值方法

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 :

Java 中的一切都是对象(阅读面向对象编程)。编写“Average”类定义了对象的结构。它具有属性(特性)和方法(动作)。您的 Average 对象没有属性。然而,它有两种作用于整数的方法(平均有两个和三个数字)。但是,您的类只是对象的骨架。您需要使用关键字 new as 从这个骨架创建一个对象:

Average averageObject = new Average();

Sincerely

真挚地

回答by Whoppa

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.

在访问说明符 (public) 之后和返回类型 (double) 之前放置 Java 关键字 static。你现在不应该担心这意味着什么。

回答by Praveen Gundu

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

您必须使用按位运算符。int a 和 b 的平均值可以计算为 avg=(a >> 1) + (b >> 1) + (((a & 1) + (b & 1)) >> 1);

回答by Shivam Shah

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.

main 方法只会执行它被要求执行的操作。如果要执行平均方法,则必须创建一个对象,传递所需的变量并从 main 方法调用方法。接受输入后,在 main 方法中写入以下几行。

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.

如果您只想对两个数字求平均值,请只写 numb1 和 numb2。