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 :(得分:0)
需要注意的事项:
更正后的代码是:
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 ) );