我有一个作业,我必须编写一段代码,让用户决定要写入的 int 值的数量,然后决定这些值应该是什么。用户必须至少有 2 个输入。然后程序将比较输入的值,然后打印出两个最高值。到目前为止,我设法打印出最高的值,但我不确定我的做法有什么问题,因为如果我选择打印出 2 个数字并且首先输入最高的数字,则输出将变为 0。我也不确定如何跟踪第二高的数字。希望得到一些帮助。
import java.util.Scanner;
public class ToStoersteTall{
public static void main(String[] args){
System.out.println("How many numbers? (minimum 2)?:");
Scanner reader = new Scanner(System.in);
if (reader.hasNextInt()) {
int numbers = reader.nextInt();
if (numbers >= 2) {
System.out.println("Enter value #1");
if (reader.hasNextInt()) {
int num1 = reader.nextInt();
System.out.println("Enter value #2");
if (reader.hasNextInt()) {
int num2 = reader.nextInt();
int biggest = 0;
for (int i = 3; i <= tall; i++) {
System.out.println("Enter value #" + i);
int num3 = reader.nextInt();
biggest = num1;
if(biggest < num3){
biggest = num3;
}
}
System.out.println(biggest);
} else {
System.out.println("Please enter an integer");
}
} else {
System.out.println("Please enter an integer");
}
} else {
System.out.println("Please enter an integer equal or higher than 2.");
}
} else {
System.out.print("Vennligst oppgi et heltall større eller lik 2.");
}
}
}
我有一个作业,我必须编写一段代码,让用户决定要写入的 int 值的数量,然后决定这些值应该是什么。用户必须至少有 2 个输入。然后程序将比较输入的值,然后打印出两个最高值。到目前为止,我设法打印出最高的值,但我不确定我的做法有什么问题,因为如果我选择打印出 2 个数字并且首先输入最高的数字,则输出将变为 0。我也不确定如何跟踪第二高的数字。希望得到一些帮助。
有几件事:
关闭扫描仪(以及一般与 IO 相关的资源)的好习惯
减少 if 语句阻止膨胀以提高可读性
您指定了 2 个有保证的数字,因此请尝试在循环之前解析这些数字
可以删除 system.exit 调用或替换 system.exit 并将大量代码移回到更大的 if-else 块中,如 OP 中的原始状态(但为了可读性,我又提到了)
添加了对输入的第一个和第二个数字的检查,以确保 high1 是最高值,high2 是第二高值。
-
在循环和检查值时保持顺序(注意:不使用数组),如果数字是新高,则替换 high1 并将 high1 的值向下移动到 high2,或者如果数字是第二(新)高,则替换 high2。如果值相等,则排除此逻辑,您可能需要根据自己的约束进行指定
import java.io.IOException;
import java.util.Scanner;
public class ToStoersteTall {
public static void main(String[] args) throws IOException {
System.out.println("How many numbers? (minimum 2)?:");
Scanner reader = new Scanner(System.in);
int n = 0;
if (reader.hasNextInt()) {
n = reader.nextInt();
} else {
System.out.println("Vennligst oppgi et heltall større eller lik 2.");
System.exit(-1); // quits execution
}
if (n < 2) {
System.out.println("Please enter an integer equal or higher than 2.");
System.exit(-2);
}
// Since guaranteed 2 numbers, parse and assign now
int high1 = 0, high2 = 0;
System.out.println("Enter value # 1");
if (reader.hasNextInt())
high1 = reader.nextInt();
System.out.println("Enter value # 2");
if (reader.hasNextInt())
high2 = reader.nextInt();
// check to see if a switch to keep correct highest order, swap values if so
if (high1 < high2) {
int t = high2;
high2 = high1;
high1 = t;
}
// loop won't execute if only 2 numbers input, but will if 3 or more specified at start
for (int i = 2; i < n; ++i) {
System.out.println("Enter value #" + (i + 1));
if (reader.hasNextInt()) {
int t = reader.nextInt();
if (t > high1) {
high2 = high1; // throw away high2 value and replace with high1
high1 = t; // replace high1 value with new highest value
} else if (t > high2) {
high2 = t;
}
} else {
System.out.println("Please enter an interger");
}
}
reader.close();
System.out.println("The two highest numbers are: " + high1 + ", " + high2);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)