java display input in range [closed]
Asked Answered
C

2

1

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");
      }
    }
}
Contingency answered 8/11, 2012 at 7:23 Comment(2)
You are passing int value to displayStatistics. However the method signature says, the input will be integer array.Leek
The simple answer to the "what's wrong?" question is that your code isn't even attempting to do what you say it needs to do.Paralipomena
U
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");
      }
    }

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

Unconsidered answered 8/11, 2012 at 7:32 Comment(0)
L
0

You must get compiler error from the above code. Change the method

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

Similarly you can add else if for another range check.

Lachman answered 8/11, 2012 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.