以下程序是读取1到100的整数,计算每个数出现的次数。输入以0结束。 结果与预期不符,请问错在哪里?
importjava.util.Scanner;publicclassll{publicstaticvoidmain(String[]args){Scannerinput...
import java.util.Scanner;
public class ll {
public static void main (String[] args){
Scanner input=new Scanner(System.in);
System.out.println("输入1到100间的整数(输入0结束):");
int []num=new int[100];
int i=0;
int a = input.nextInt();
while(a != 0) {
num[i] = a;
i++;
a = input.nextInt();
}
int[]count=countNum(num);
displayCount(count);
}
public static int []countNum(int[]num){
int[]count=new int[100];
for(int i=0;i<num.length;i++){
if(num[i]!=0)
count[num[i]-1]++;
}
return count;
}
public static void displayCount(int[]count){
for(int i=0;i<count.length;i++){
if(count[i]!=0)
System.out.println(i+'\t'+"occurs"+'\t'+count[i]+'\t'+displayTime(count[i]));
}
}
public static String displayTime(int num){
if(num>=2)
return "times";
else
return "time";
}
} 展开
public class ll {
public static void main (String[] args){
Scanner input=new Scanner(System.in);
System.out.println("输入1到100间的整数(输入0结束):");
int []num=new int[100];
int i=0;
int a = input.nextInt();
while(a != 0) {
num[i] = a;
i++;
a = input.nextInt();
}
int[]count=countNum(num);
displayCount(count);
}
public static int []countNum(int[]num){
int[]count=new int[100];
for(int i=0;i<num.length;i++){
if(num[i]!=0)
count[num[i]-1]++;
}
return count;
}
public static void displayCount(int[]count){
for(int i=0;i<count.length;i++){
if(count[i]!=0)
System.out.println(i+'\t'+"occurs"+'\t'+count[i]+'\t'+displayTime(count[i]));
}
}
public static String displayTime(int num){
if(num>=2)
return "times";
else
return "time";
}
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询