英文:
Suitable method for declaring Array class variables?
问题
以下是您要翻译的内容:
我对学习数组非常陌生,想要知道如何使用数组方法通过扫描器输入来声明变量。例如,在常规情况下,假设我想要找到常规扫描器输入的平均值,我会这样做:
int num1 = keyboard.nextInt();
int num2 = keyboard.nextInt();
int Average = (num1 + num2) / 2;
System.out.print(Average);
这是我非常熟悉的最简单的方法,但我希望相同的逻辑/方法适用于数组类。
任务是从用户输入一个整数,表示接下来还将输入多少个整数。然后,用户输入其余的整数。程序输出这些数字的平均值,保留一位小数。
虽然这是一个非常简单的任务,但是我无法像我之前提供的使用常规扫描器输入的示例一样来声明一个Average变量。
我的代码:
else if (option == 2){
int[] numbers = new int[keyboard.nextInt()];
for (int x = 0; x < numbers.length; x++){
numbers[x] = keyboard.nextInt(); //现在需要声明一个平均值变量
}
int Average = //我应该在这里放什么?
for (int x = 0; x < numbers.length; x++){
System.out.println(Average); //需要打印平均值
}
}
英文:
I am very new to learning Arrays and want to know how to declare variables with scanner inputs using array method. For example, in the regular scenario, lets say I want to find the average of regular scanner inputs I would do something like this:
int num1 = keyboard.nextInt();
int num2 = keyboard.nextInt();
int Average = (num1+num2)/2;
System.out.print(Average);
This is the most simple method that I am very familiar with, but I want the same logic/method to be applied with Array class.
The task is to Input an integer from the user representing how many more integers are about to be entered. Then, the user enters the rest of the integers. The program outputs the average of the numbers, rounded to one decimal place.
Also this is a very simple task but I am unable to declare an Average variable based on the example I provided earlier using regular scanner inputs.
My code:
else if (option == 2){
int[] numbers = new int[keyboard.nextInt()];
for (int x = 0; x < numbers.length; x++){
numbers[x] = keyboard.nextInt(); //need to now declare an average variable
}
int Average = //What would I put here?
for (int x = 0; x < numbers.length; x++){
System.out.println(Average); //Average needs to be printed
}
}
答案1
得分: 2
这里是一个简单的解决方案...
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int result = sc.nextInt();
while (true) {
String next = sc.next();
if (next.equals("done") || next.equals("calculate")) {
System.out.println(result);
break; // 退出循环。
} else if (next.equals("+")) {
result += sc.nextInt();
} else if (next.equals("-")) {
result -= sc.nextInt();
} else {
System.out.println("无法识别的输入");
break; // 退出循环。
}
}
}
这需要至少输入一个数字。还要注意,这种类型的解决方案只适用于左结合的运算符。(幸运的是,+
和 -
都是左结合的。)例如,我们不能将指数添加到此算法中,因为结果是按左结合方式计算的。
英文:
Here's a simple solution...
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int result = sc.nextInt();
while (true) {
String next = sc.next();
if (next.equals("done") || next.equals("calculate")) {
System.out.println(result);
break; // Exit Loop.
} else if (next.equals("+")) {
result += sc.nextInt();
} else if (next.equals("-")) {
result -= sc.nextInt();
} else {
System.out.println("Unrecognized Input");
break; // Exit Loop.
}
}
}
This requires at least one number to be input. Also note, this type of solution will only work for left associative operators. (Luckily both +
and -
are) For example, we could not add exponents to this algorithm since the result is calculated in a left associative manor.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论