考察读入一系列整数
题:编写程序读入一系列整数,当输入 0 时结束,输出这一系列整数的最大值和最大值的个 数 。(如输入 3 5 2 5 5 5 0,程序输出:最大值是 5,5 的个数是 4)
代码:
import java.util.*;
class Demo{
public static void main(String[] args){
int max=0;
int count=1;
Scanner in=new Scanner(System.in); //使用Scanner类定义对象
int tmp=0;
while(true){
tmp=in.nextInt();
if(tmp==0) break;
if(tmp>max){
max=tmp;
count=0;
}
if(tmp==max){
count++;
}
}
System.out.println("最大值是"+max+","+max+"的个数是"+count);
}
}
代码(与上文相同,供方便复制用)
import java.util.*;
class Demo{
public static void main(String[] args){
int max=0;
int count=1;
Scanner in=new Scanner(System.in);
int tmp=0;
while(true){
tmp=in.nextInt();
if(tmp==0) break;
if(tmp>max){
max=tmp;
count=0;
}
if(tmp==max){
count++;
}
}
System.out.println("最大值是"+max+","+max+"的个数是"+count);
}
}