在范围内的java显示输入范围内、java

2023-09-11 06:38:28 作者:我以为是你。

嗨什么是错用下面的程序?正如我希望它在一系列的显示用户输入整型值 1-10,11-20,21-30 ... 191-200

 公共类节目
{
    / **
     *这是主要的入口点的应用
     * /
   公共静态无效的主要(字符串的args [])
{
  诠释一个[] =新的INT [100];
  INT I = 0;
  扫描仪在=新的扫描仪(System.in);
  而(ⅰ&小于100)
  {
  的System.out.println(请输入INT);
  A [1] = in.nextInt();
  displayStatistics(A [1]);


  }

}

    公共静态无效displayStatistics(整数[] A [1])
    {
        如果(A [1]≥= 1&安培;&安培;一[1]  -  = 100)
      {
        我++;
        的System.out.println(); ---->需要在范围1-10,11-20,21-30显示... 191-200
      } 其他 {
      的System.out.println(整数不是在1-200范围);
      }
    }
}
 

解决方案

 公共静态无效displayStatistics(INT K)
    {
        如果(K> = 1&安培;&安培; K&其中; = 200)
      {
        INT低,高的;
        如果(K%10 == 0)
        {
            低= K-9;
            高= K;
        }
        其他
        {
            低= K-K%10 + 1;
            高= K-K%10 + 10;
        }
        的System.out.println(范围内的值+低+ - +高);

      } 其他 {
      的System.out.println(整数不是在1-200范围);
      }
    }
 

记住要传递一个整数的函数,而不是完整的阵

JAVA的jdk的下载与安装部署详细图解

Hi what was wrong with the following program? As i want it to display user input integer value in a range of 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");
      }
    }
}

解决方案

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");
      }
    }

Remember that you are passing an integer to the function , not the complete array