循环直到用户输入长度小于或等于先前的用户输入

时间:2016-09-07 00:55:45

标签: java arrays if-statement

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,因为我还想稍后打印用户输入。

我现在唯一的问题是使编写的代码有效。

2 个答案:

答案 0 :(得分:0)

需要注意的事项:

  1. 您每次都要重置数组,因为声明(内存分配)在&#39;之内。环
  2. 数组大小(i + 1)将在您尝试更多次数时分配大量内存
  3. giveMeAString [i-1]:i = 1 - 这应该是for()的起点。否则,您将获得arrayIndexOutOfBound异常,因为当i = 0时您将访问giveMeAString [-1]。这是不存在的数组位置/元素索引。
  4. 如果你想比较2个连续的字符串长度,你需要确保在循环的第一次迭代中,你至少填充了2个字符串(或者检查你是否在第一次迭代,然后继续循环以添加第二次字符串没有进一步。)
  5. 更正后的代码是:

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

答案 1 :(得分: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 ) );