我试图从循环中找到最大数量,它是出现的次数。这是我到目前为止编写的代码。
public class MaxNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int max = 0;
int number = 0;
int count_max = 0;
while (true) {
number = input.nextInt();
if (number == 0) {
break;
}
number = max;
if (max > number) {
max=number;
count_max++;
}
}
System.out.println("Max Number: " + number);
System.out.println("Occurences: " + count_max);
}
我的输出中只有零,我在做任何逻辑错误?
最佳答案
这是我认为要达到的目标的我的版本,大多数情况下都可以正常工作,请注意,如果您以描述的字符串开头失败,则必须开始输入数字,还请注意数字,然后大于long
得到忽略或“修剪”
import java.util.Scanner;
public class Test {
public static final void main(String[] args) {
long a = 0, b = 0, c = 0, d = 0;
System.out.println("Type some numbers!");
Scanner sc = new Scanner(System.in);
String in = sc.nextLine();
Scanner ln = new Scanner(in);
while (ln.hasNextLong()) {
a = ln.nextLong();
if (a > b) {
b = a;
++d;
}
++c;
}
System.out.println("\n info:");
System.out.println(" highest:" + b);
System.out.println(" iterations:" + c);
System.out.println(" overrides:" + d);
}
}