gpt4 book ai didi

java - 使用java中的方法

转载 作者:行者123 更新时间:2023-12-02 07:41:01 25 4
gpt4 key购买 nike

我编写了以下代码来满足程序要求:

Average of Three Write a program that reads three whole numbers and displays the average of the three numbers.

Input Notes: Three whole numbers (non-negative integers) are entered at the console.

Output Notes (Prompts and Labels): The program prompts for the three integers with these strings: "Enter the first integer.", "Enter the second integer.", "Enter the third integer.". The program then prints         The average of NUMBER1,  NUMBER2,  and NUMBER3  = AVG   where NUMBER1  is the first integer value entered and NUMBER2  and NUMBER3  the subsequent integers, and AVG  is the computed average value.

SPECIFICATION OF NAMES: Your application class should be called Average3:

我的源代码:

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

}

}

我的程序编译得很好,但我知道我可以实现并调用具有关联方法的对象,但我很难从哪里开始我从概念上理解方法和对象,但不知道编写代码。有人有什么建议吗?

最佳答案

首先,我将创建一个类 InputData这将存储用户的输入:

class InputData {
public int number1;
public int number2;
public int number3;
}

这本身就是一种有用的通用技术:将多个相关值收集到单个数据结构中。然后您可以重写您的 main方法使用此类而不是三个单独的int变量,或者您可以更进一步,向 InputData 添加一些行为类(class)。显然要添加的第一个行为是计算平均值:

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一个static inner classAverage3 。它也可能是同一文件中的单独的顶级类(只要它不是 public )或单独文件中的类。

对此的改进是使用数组而不是单独的 int InputData 中的字段类(class)。您的程序可能如下所示:

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

这里我向 InputData 添加了一个构造函数初始化数组并给它一个参数来设置数组的大小。

然后,您可以引入其他改进,例如使输入值的数量动态化(使用 ArrayList<Integer> 而不是 int 数组)。但这超出了作业的具体要求。有些人(实际上很多人)倾向于自动进行这种概括。然而,Extreme Programming的拥护者会指出,通常最好保持简单:根据今天的需求而不是明天、下周或下个月的需求进行设计和编码。

换句话说,您不知道下一个任务会带来什么,因此无论您如何概括当前任务,都可能会浪费工作。

关于java - 使用java中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11604246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com