2017-08-31 43 views
2

我的程序是ArrayList存儲輸入的號碼,當用戶輸入號碼-1時,程序將停止結束打印所有輸入號碼的升序。我的程序運行,但讓號碼進入,它不會打印出數字。它似乎是如果聲明不起作用。如何在if語句中調用ArrayList以正常工作?存儲輸入號碼的arraylist

import java.util.ArrayList; 
import java.util.Scanner; 
import java.util.Collections; 

public class testArrayList {   

    public static void main(String[] args) { 

     Scanner inputs = new Scanner(System.in); 
     ArrayList <Integer> nums = new ArrayList<Integer>(); 

     System.out.println("Enter a number(-1 to end): "); 

     while(inputs.hasNextInt()) { 
      nums.add(inputs.nextInt()); 
     } 

     // it looks like it runs until here. 

      for (Integer n : nums) { 
       if (n == -1) { 
        Collections.sort (nums); 
        System.out.println("Here is the list of numbers : "); 
        System.out.println(n); 
       } 
      }   
    } 
} 
+1

您是否嘗試過通過代碼帶有[調試器](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-步驟我診斷的 - 問題)? – litelite

+0

爲什麼不使用int作爲原始數據,而不是整數? – Razvan

+0

因爲不能將'int'放入'ArrayList'中。 – csmckelvey

回答

0

System.in會阻塞並等待新的輸入,只要你繼續輸入號碼,hasNextInt()永遠是true。相反,你應該在初始輸入(while)循環檢查-1

while(inputs.hasNextInt()) { 
    int num = inputs.nextInt(); 
    if (num == -1) { 
     break; 
    } 
    nums.add(num); 
} 

nums.sort(); 
System.out.println("Here is the list of numbers : "); 
System.out.println(nums); 
+0

我們說的一樣。儘管如此,你的代碼在輸出上有所不同。您的輸出使用導出爲{1,2,3,7,10]的輸出列表的toString方法,而原始代碼在一行中輸出每個數字。 – Lothar

0

你的循環將號碼列表不檢查值-1停止循環。 -1是一個整數,所以它繼續。

修復後,您將遇到下一個問題。你迭代列表中的元素並在用戶輸入1時進行排序。我沒有檢查過,但是這很可能會導致異常,因爲如果迭代器在迭代時更改基礎列表。根據你的描述,你應該在循環之前調用sort,然後在循環中執行System.out.println。

0

我的程序運行,但讓號碼進入,它不打印 號碼。它好像如果語句不working.Trying 進行一次一步來:

在你的代碼,它會做到只爲n從列表nums的值等於-1。

相反,你應該儘量把你的代碼爲:

// Accept user input 
Integer input = inputs.nextInt(); 

// check if it is not -1 
while(input != -1) { 
    nums.add(input); 
    input = inputs.nextInt() 
} 

// sort the arraylist 
Collections.sort (nums); 

// print the elements 
System.out.println("Here is the list of numbers : ");   
for (Integer n : nums) { 
    System.out.println(n); 
} 
+0

非常感謝!它有很大幫助! – Michelle

+0

@Michelle酷。如果確實如此,請標記答案,以便將來可以幫助他人。 – nullpointer

0

的,如果永遠達不到的語句,您可以通過添加之前只是,如果因爲掃描儀忽略空格這是從來沒有印一個println檢查。它會繼續嘗試輸入信息直到你輸入一個字母。爲了得到所需的效果,我可能只是做n = inputs.nextInt();那麼如果n是-1,則使用break。此外,您的整個for循環,如果條件是不必要的,只需對列表進行排序並打印它 - 使用.toString,因爲否則它將只是一個內存地址 - 因爲你在任何情況下都這樣做(雖然你可能想要刪除-1) 。

0

我知道有幾個答案,但這裏是我的版本:

import java.util.ArrayList; 
import java.util.Scanner; 
import java.util.Collections; 

    public class testArrayList 
    {   

    public static void main(String[] args) 
    { 

     Scanner inputs = new Scanner(System.in); 
     ArrayList <Integer> nums = new ArrayList<Integer>(); 

     System.out.println("Enter a number(-1 to end): "); 

     while(inputs.hasNextInt()) 
     { 
      //check if the next value is -1 
      int nextVal = inputs.nextInt(); 
      if(nextVal == -1) 
      { 
       break; //then exit the loop 
      } 
      else 
      { 
       nums.add(inputs.nextInt()); //else add to the list 
      } 

     } 

     //since we exited the loop, we can sort the list, and print header 
     Collections.sort (nums); 
     System.out.println("Here is the list of numbers : "); 
     for (int i = 0 ; i < nums.size(); i++) 
     {        
      System.out.println(nums.get(i)); // print every sorted int from 
              // the list 
     } 
    }   
} 

打印時,也讀爲-1退出代碼時,有你的代碼的邏輯存在問題。現在它在讀取循環中,然後打印整個排序列表。

這裏測試: https://www.jdoodle.com/online-java-compiler

0

我重組了代碼,並使它更簡單,因爲你試圖輸入-1值時,退出地圖。

public class TestArrayList { 

    public static void main(String[] args) { 

      Scanner scanner = new Scanner(System.in); 
      ArrayList <Integer> nums = new ArrayList<Integer>(); 

      while(true) { 
       System.out.println("Enter a number(-1 to end): "); 
       int value = scanner.nextInt(); 

       /*read until your input is -1*/ 
       if (value != -1){ 
        nums.add(value); 
       }else { 
        Collections.sort (nums); 
        System.out.println("Here is the list of numbers : "); 
        for (int i : nums){ 
         System.out.println(i); 
        } 
        break; 
       } 
      } 
      scanner.close(); 
     } 
} 
0

掃描儀輸入=新掃描儀(系統。在); ArrayList nums = new ArrayList();

System.out.println("Enter a number(-1 to end): "); 

    while (inputs.hasNextInt()) { 
     //Accepts the inputs 
     int input = inputs.nextInt(); 
     //Checks condition 
     if (input == -1) { 
      break;// breaks out of the loop is fails 
     } 
     nums.add(input);// else keep adding 
    } 
    System.out.println("Here is the list of numbers : "); 
    Collections.sort(nums); 
    for (Integer n : nums) { 
     System.out.println(n); 
    }