Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 循环,直到用户输入长度小于或等于以前的用户输入_Java_Arrays_If Statement - Fatal编程技术网

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

Java 循环,直到用户输入长度小于或等于以前的用户输入,java,arrays,if-statement,Java,Arrays,If Statement,我想循环,直到当前用户输入长度小于以前的用户输入长度 经过多次尝试,我已经有一段时间没有解决它了 我做错了什么? 我想使用数组,因为我还想稍后打印用户输入 我现在唯一的问题是让编写的代码正常工作。需要注意的几点: 每次都要重置数组,因为声明内存分配在“for”循环中 当尝试次数较多时,i+1的数组大小将分配大量内存 giveMeAString[i-1]:i=1-这应该是您的开始。否则,您将获得arrayIndexOutOfBound异常,因为当i=0时,您将访问giveMeAString[-1]

我想循环,直到当前用户输入长度小于以前的用户输入长度

经过多次尝试,我已经有一段时间没有解决它了

我做错了什么? 我想使用数组,因为我还想稍后打印用户输入


我现在唯一的问题是让编写的代码正常工作。

需要注意的几点:

每次都要重置数组,因为声明内存分配在“for”循环中 当尝试次数较多时,i+1的数组大小将分配大量内存 giveMeAString[i-1]:i=1-这应该是您的开始。否则,您将获得arrayIndexOutOfBound异常,因为当i=0时,您将访问giveMeAString[-1]。这是不存在的数组位置/元素索引。 如果要比较两个连续的字符串长度,则需要确保在循环的第一次迭代中,至少填充了两个字符串,或者检查是否在第一次迭代中,然后继续循环以添加第二个字符串,而无需进一步操作。 更正代码为:

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

        }

    }   
}

与前面的答案类似,但如果不应限制数组大小,则使用ArrayList实现

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

        }
更新,使用数组实现

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

运行代码的当前结果是什么?对于开始字符串[]giveMeAString=新字符串[i+1];将为循环的每次迭代重新启动。我以为它只会重新初始化数组中的变量量。为什么我收到-2?在研究工作中,我如何只重新初始化数组可以容纳的变量数量,这解决了很多问题。但这仍然不是我想要的。因为数组大小不应该是已知的。在字符串比前一个字符串短之前,它应尽可能多。这意味着我必须在每次输入后更改数组的大小。但是我不知道它是怎么做到的,所以我试着把它放在一个for循环中。即使它不是那么适用。好的。那么你能接受大小为2的数组吗?看起来你总是一次只比较两个元素。所以,您的解决方案可以是,尽管它有更多的数据复制和更多的运行时间:填充数组的两个元素,并像往常一样进行比较。如果您决定进入下一次迭代firstScanner 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 ) );