2016-11-20 77 views
-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(); 
    } 
} 

我的問題是如何利用2個inputes沒有宣佈2 variables.Suppose,如果我輸入一些詮釋N = 2.It會提示兩次兩個採用我的輸入。採取多種輸入不聲明變量爲輸入

+0

數組?你在輸入數據後做了什麼? –

+0

請在問題主體中發佈代碼,而不是作爲圖像/鏈接 – ItamarG3

+0

不是數組。我想嘗試不同的測試用例,因此我需要知道 – user5922850

回答

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

第一個輸入是您想要輸入的數量,然後您可以將每個輸入存儲在數組中。這樣,你有一個變量:數組。 這裏是一個例子。您也可以使用特殊輸入退出輸入,例如:-1