-4
public class INPUT{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num1,num2;
        num1 = input.nextInt();
        num2 = input.nextInt();
    }
}

My question is how to take 2 inputes without declaring 2 variables.Suppose if i input some int n=2.It will prompt two times two take my input.

ItamarG3
  • 3,846
  • 6
  • 28
  • 42

1 Answers1

0
    System.out.println("How many numbers?");

    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    Integer numbers[] = new Integer[n];
    for (int i = 0; i < n; i++) {
        numbers[i] = scanner.nextInt();
    }

First input is how many you want to have input and then you may store each input in array. In this way, you have a single variable : the array. Here, it is an example. You could also exit the input with a special input, for example : -1 .

davidxxx
  • 104,693
  • 13
  • 159
  • 179