2016-09-07 65 views
-2
import java.util.Scanner; 

public class userInputTest { 

    public static void main(String[] args) { 

     Scanner sc = new Scanner(System.in); 

     for(int i = 0; i>-1; i=i+1){ 

      String[] giveMeAString = new String[i+1]; 
      String x = sc.next(); 

      giveMeAString[i]=x; 

      if(i>=0){ 
       if (giveMeAString[i].length() < giveMeAString[i-1].length()){ 
        System.out.println("The string is shorter than previous string!"); 
        break;} 
      } 

     } 

    } 
} 

我想循環直到當前用戶輸入長度小於先前用戶輸入長度。循環直到用戶輸入長度小於或等於先前用戶輸入

許多嘗試後,我還沒有解決它。

我做錯了什麼? 我想使用Array,因爲我也想稍後打印出用戶輸入。

我現在唯一的問題是使書面代碼工作。

+0

運行代碼的當前結果是什麼? – mapeters

+0

開始'String [] giveMeAString = new String [i + 1];'將爲循環的每次迭代重新啓動 –

+0

我認爲它只會重新初始化該數組內的變量的數量。 – adevapp

回答

0

幾件事情需要注意:

  1. 你重置您的每一次陣列,作爲聲明(內存分配)是在「for」循環
  2. 的第(i + 1)將分配
  3. 數組大小當你嘗試更多次數時,很多內存
  4. giveMeAString [i-1]:i = 1 - 這應該是你的for()的起點。否則,你會得到arrayIndexOutOfBound異常,因爲當i = 0時你將訪問giveMeAString [-1]。這是不存在的數組位置/ element-index。
  5. 如果你想比較2個連續的字符串長度,你需要確保在循環的第一次迭代時,你至少有2個字符串填充(或檢查你是否在第一次迭代,然後繼續循環添加第二個字符串沒有進一步)。

更正代碼:

Scanner sc = new Scanner(System.in); 

     //arrays are alaways static memory allocation constructs. First input should always be the array size. If this is not known 
     //ahead of time, use List, ArrayList etc. 
     int a; 
     System.out.println("Enter Array size in number: "); 
     a = sc.nextInt(); 
     String[] giveMeAString = new String[a]; 
     String x; 

     //updaing the loop initialization and condition 
     for(int i = 1; i>0; i=i+1){ 

      //should NOT be creating array every time. Strings are immutable and hence costly w.r.t. memory/responseTime. 
      //Besides, i didn't understand the logic of creating array of String of size (i+1). 
      //instead, consider using resizable list (ArrayList etc). Below line is resetting your previously input values in giveMeAString[] 
      //String[] giveMeAString = new String[i+1]; 


      if (i == 1) { 
       //adding first input to array's first index. Without this, we would get NPE. One time execution. 
       x = sc.next(); 
       giveMeAString[i - 1] = x; 
      } 

      //adding next input to array's next index 
      x = sc.next(); 
      giveMeAString[i]= x; 

      if(i>=0){ 
       if (giveMeAString[i].length() < giveMeAString[i-1].length()){ 
        System.out.println("The string is shorter than previous string!"); 
        break; 
       } 
      } 

     } 
+0

這解決了很多。但它仍然不是我想要的。因爲數組大小不應該是已知的。它應該儘可能多(直到字符串比前一個字符串短)。這意味着我必須在每次輸入後更改數組的大小。但我不知道它是如何完成的,因此我嘗試將它放在for循環中。即使它不適用。 – adevapp

+0

好的..你可以採取大小2陣列?看起來你總是隻比較兩個元素。所以,你的解決方案可能是(雖然它有更多的數據複製和更多的運行時間):填充數組中的兩個元素並像往常一樣進行比較。如果您決定進入下一次迭代(第一次),則將第二次複製到數組的第一個索引中,並將新接受的值置於「第二次」並再次進行比較。 – DevdattaK

0

一如以往的答案,但使用ArrayList實現中,如果數組的大小不應該被限制。

Scanner scn = new Scanner(System.in); 
    List<String> strList = new ArrayList<>(); 

    /* 
    * First clase to allow logic receive at least 1 input without performing the check. 
    * Next clause to check if current input length is greater than previous input length, continue receiving input 
    */ 
    while (strList.size() < 2 || strList.get(strList.size() - 1 ).length() 
      >= strList.get(strList.size() - 2).length()) 
    { 
     System.out.println(" Enter new String: "); 
     strList.add(scn.next()); 
    } 

    System.out.println("Previous String is shorter."); 
    scn.close(); 

    System.out.println(Arrays.toString(strList.toArray())); 

更新,使用數組實現。

Scanner scn = new Scanner(System.in); 
String [] strArray = new String[0]; 

/* 
* First clause to allow logic receive at least 1 input without 
* performing the check. Next clause to check if current input length is 
* greater than previous input length, continue receiving input 
*/ 
while (strArray.length < 2 
    || strArray[strArray.length - 1].length() >= strArray[strArray.length -2].length()) 
{ 
    String[] tempArr = new String[strArray.length + 1]; 
    for(int i = 0; i < strArray.length; i++){ 
     tempArr[i] = strArray[i]; 
    } 
    strArray = tempArr; 
    System.out.println(" Enter new String: "); 
    strArray[strArray.length - 1] = scn.next(); 
} 

System.out.println("Previous String is shorter."); 
scn.close(); 

System.out.println(Arrays.toString(strArray)); 
+0

如果你在尋求一個數組實現,你可以編寫你自己的實現,因爲我非常肯定這在大多數基礎編程課程中都有涉及。或者,如果需要的話,將其作爲另一個問題提出 –

+0

嗨,thnx回答。但有沒有可能的方式來解決它通過使用String [] giveMeAString = new String [] ;.我想盡可能保持簡單。我沒有了解arrayList。所以我不認爲它的neccensary這個家庭演習。我基本上想要運行一個for循環並寫一個String,並將其添加到一個字符串數組中,以便稍後在system.out.println中調用它。我必須最少寫2個字符串,以便我可以檢查哪一個更短。最後寫的一個不會比前一個短。因爲那時它應該打破; – adevapp

+0

做一個谷歌搜索,大量發表那裏[回覆](http://stackoverflow.com/questions/12524318/how-to-increase-the-size-of-an-array-in-java)到那題。 –