在java中使用方法
我编写了以下代码以满足程序要求,如下所示: 平均三个写一个程序,读取三个整数和 显示三个数字的平均值 输入说明:输入三个整数(非负整数) 在控制台上 输出注释(提示和标签):程序提示输入三个 带以下字符串的整数:“输入第一个整数。”,“输入 第二个整数。“,”输入第三个整数。“。然后程序打印 NUMBER1、NUMBER2和NUMBER3的平均值=平均值,其中NUMBER1是输入的第一个整数值,NUMBER2和NUMBER3 随后的整数,AVG是计算出的平均值 名称说明:应调用应用程序类 平均数3: 我的源代码:在java中使用方法,java,object,methods,Java,Object,Methods,我编写了以下代码以满足程序要求,如下所示: 平均三个写一个程序,读取三个整数和 显示三个数字的平均值 输入说明:输入三个整数(非负整数) 在控制台上 输出注释(提示和标签):程序提示输入三个 带以下字符串的整数:“输入第一个整数。”,“输入 第二个整数。“,”输入第三个整数。“。然后程序打印 NUMBER1、NUMBER2和NUMBER3的平均值=平均值,其中NUMBER1是输入的第一个整数值,NUMBER2和NUMBER3 随后的整数,AVG是计算出的平均值 名称说明:应调用应用程序类 平均数
import java.util.Scanner;
public class Average3 {
/**
* @param args
*/
public static void main(String[] args) {
int AVG, NUMBER1, NUMBER2, NUMBER3;
System.out.println("Enter the first integer.");
Scanner keyboard = new Scanner(System.in);
NUMBER1 = keyboard.nextInt();
System.out.println("Enter the second integer.");
NUMBER2 = keyboard.nextInt();
System.out.println("Enter the third integer.");
NUMBER3 = keyboard.nextInt();
AVG = (NUMBER1 + NUMBER2 + NUMBER3) / 3;
System.out.println("The average of NUMBER1, NUMBER2, and NUMBER3 = " + AVG);
}
}
我的程序编译得很好,但我知道我可以用一个关联的方法实现和调用一个对象,但我很难从哪里开始我从概念上理解这些方法和对象,但远不及编写代码。有人有什么建议吗 我不知道我是否会为这么简单的任务使用对象。但是,既然你问了,我会解释如果需要对象我会怎么做 我会创建一个对象(类),它有一个
ArrayList
,这样你就可以计算出3个以上数字的平均值
此对象将有2个公共方法void addNumber(int number)
和double/int getAverage()
addNumber只需向arraylist添加一个数字,getAverage将遍历整个列表,将所有数字相加,然后除以其大小(不要忘记-1)
在main中,创建该类的新对象,然后每次扫描扫描仪时,使用addNumber方法将输入的编号插入数组列表
我想我会怎么做,因为我被指示使用对象
祝你好运 这是一个关于如何使用非常简单的类制作程序的简短示例 你可以很容易地改变它,并询问你想计算多少个数字的平均值
/* using default package */
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* The Class Average3.
*/
public class Average3 {
/** The number list. */
private List<Integer> numberList;
/** The sum. */
private float sum;
/**
* Instantiates a new my first class.
*/
public Average3() {
this.numberList = new ArrayList<Integer>();
this.sum = 0;
}
/**
* Adds the integer.
*
* @param integer the integer
*/
public void addInteger(int integer) {
this.numberList.add(integer);
this.sum += integer;
}
/**
* Gets the average.
*
* @return the average
*/
public float getAverage() {
return (this.sum/this.numberList.size());
}
/**
* Prints the Average.
*/
public void printAverage() {
System.out.print("The average of ");
for(int j = 0; j < this.numberList.size()-1; j++) {
Integer i = this.numberList.get(j);
System.out.print(i.toString() + ", ");
}
System.out.print("and " + numberList.get(numberList.size()-1));
System.out.println(" = " + this.getAverage());
}
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Average3 myClass = new Average3();
System.out.println("Enter the first integer.");
Scanner keyboard = new Scanner(System.in);
myClass.addInteger(keyboard.nextInt());
System.out.println("Enter the second integer.");
myClass.addInteger(keyboard.nextInt());
System.out.println("Enter the third integer.");
myClass.addInteger(keyboard.nextInt());
myClass.printAverage();
}
}
/*使用默认软件包*/
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
/**
*全班平均3人。
*/
公共类平均数3{
/**号码表*/
私人名单号码表;
/**总数*/
私人浮动金额;
/**
*实例化一个新的my first类。
*/
公众平均数3(){
this.numberList=新的ArrayList();
此值为0.sum=0;
}
/**
*添加整数。
*
*@param integer整数
*/
公共void addInteger(整数){
this.numberList.add(整数);
此值为.sum+=整数;
}
/**
*获取平均值。
*
*@返回平均值
*/
公共浮动平均值(){
返回(this.sum/this.numberList.size());
}
/**
*打印平均值。
*/
公众平均{
系统输出打印(“的平均值”);
对于(int j=0;j
作为一个非常基本的示例,在一个.java文件中,您可以创建一个“知道如何”获取输入并创建平均值的类,例如:
// takes input and stores in a list which it
// can do mathematical operations on
class InputMath
{
// ivar to store input
private List<int> list;
// contructor
public AverageInput() {}
// gets user input and pushes to `list`
public void getInput(String question) {}
// works out the average of `list`
public int average() {}
// works out the total of `list`
public int total() {}
...
}
//接受输入并将其存储在列表中
//能在计算机上做数学运算
类输入数学
{
//用于存储输入的ivar
私人名单;
//承包商
public AverageInput(){}
//获取用户输入并推送到“列表”`
public void getInput(字符串问题){}
//计算出“列表”的平均值`
public int average(){}
//算出名单上的总数`
public int total(){}
...
}
然后你可以在你想要的地方使用这个类,比如在你的“main”函数中
import InputMath;
class Main
{
public static void main(String args)
{
InputMath im = new InputMath();
for(int i=0; i<3; i++)
{
im.getInput("Enter number " + (i+1));
}
System.out.println(im.average());
}
}
导入InputMath;
班长
{
公共静态void main(字符串参数)
{
InputMath im=新的InputMath();
对于(int i=0;i首先,我将创建一个类InputData
,该类将存储来自用户的输入:
class InputData {
public int number1;
public int number2;
public int number3;
}
这本身就是一种有用的通用技术:将多个相关值收集到一个数据结构中。然后,您可以重写main
方法来使用该类而不是三个单独的int
变量,或者您可以更进一步,向InputData
类添加一些行为要添加的ehavior将计算平均值:
class InputData {
public int number1;
public int number2;
public int number3;
public int average() {
return (number1 + number2 + number3) / 3;
}
}
这样,您就可以按如下方式重写main
:
public class Average3 {
static class InputData {
public int number1;
public int number2;
public int number3;
public int average() {
return (number1 + number2 + number3) / 3;
}
}
/**
* @param args
*/
public static void main(String[] args) {
InputData input = new InputData();
System.out.println("Enter the first integer.");
Scanner keyboard = new Scanner(System.in);
input.number1 = keyboard.nextInt();
System.out.println("Enter the second integer.");
input.number2 = keyboard.nextInt();
System.out.println("Enter the third integer.");
input.number3 = keyboard.nextInt();
System.out.println("The average of NUMBER1, NUMBER2, and NUMBER3 = "
+ input.average());
}
}
请注意,我制作了InputData
a ofAverage3
。它也可以是同一文件中的一个单独的顶级类(只要它不是public
),也可以是单独文件中的一个类
对此的一个改进是在InputData
类中使用数组而不是单独的int
字段
public class Average3 {
static class InputData {
public int[] numbers;
InputData(int size) {
numbers = new int[size];
}
public int average() {
int sum = 0;
for (int number : numbers) {
sum += number;
}
return sum / numbers.length;
}
}
/**
* @param args
*/
public static void main(String[] args) {
String[] prompts = { "first", "second", "third" };
InputData input = new InputData(3);
Scanner keyboard = new Scanner(System.in);
for (int i = 0; i < prompts.length; ++i) {
System.out.println("Enter the " + prompts[i] + " integer.");
input.numbers[i] = keyboard.nextInt();
}
System.out.println("The average of NUMBER1, NUMBER2, and NUMBER3 = "
+ input.average());
}
}
公共类平均值3{
静态类输入数据{
公共int[]编号;
输入数据(整数大小){
数字=新整数[大小];
}
公共整数平均值(){
整数和=0;
for(int)