我试图通过一次输入获得发生最大数量的计数。
我的代码如下: -
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int max = 0, count = 0, value, lastValue = 0;
while (num > 0){
value = num % 10;
num = num / 10;
if (value > lastValue){
max = value;
}
lastValue = value;
}
System.out.println(max);
System.out.println("count is " + count);
}
我如何计算最大数量?
假设我有2556621
count
之类的输入应为2
。
答案 0 :(得分:0)
看起来你的代码应该是
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int max = 0, count = 0, value = 0;
while (num > 0){
value = num % 10;
num = num / 10;
if (value > max){
max = value;
count = 1;
} else if (value == max){
count++;
}
}
System.out.println(max); // 6 for 2556621
System.out.println("count is " + count); // 2 for 2556621
}