以下程序出了什么问题?因为我希望它显示范围为1-10, 11-20,21-30 ... 191-200
?
public class Program
{
/**
* This is the main entry point for the application
*/
public static void main(String args[])
{
int a[] = new int[100];
int i = 0;
Scanner in = new Scanner(System.in);
while(i<100)
{
System.out.println("Enter a int");
a[i] = in.nextInt();
displayStatistics(a[i]);
}
}
public static void displayStatistics(integer[] a[i])
{
if(a[i]>=1 && a[i]<=100)
{
i++;
System.out.println(); ----> need to display in range 1-10, 11-20,21-30 ... 191-200
} else {
System.out.println("Integer not in range of 1-200");
}
}
}
答案 0 :(得分:1)
public static void displayStatistics(int k)
{
if(k>=1 && k<=200)
{
int low,high;
if(k%10==0)
{
low=k-9;
high=k;
}
else
{
low=k-k%10+1;
high=k-k%10+10;
}
System.out.println("value in range " +low+" -"+high);
} else {
System.out.println("Integer not in range of 1-200");
}
}
请记住,您将整数传递给函数,而不是完整的数组
答案 1 :(得分:0)
您必须从上面的代码中获得编译器错误。改变方法
public static void displayStatistics(int a) {
if (a >= 1 && a <= 100) {
System.out.println("Input[" + a + "] is within the range 1 to 100");
} else {
System.out.println("Integer not in range of 1-200");
}
}
同样,您可以添加else if
进行另一次范围检查。