用户输入字符串到字符串数组

时间:2022-11-27 15:43:37

I'm trying to get the user's input, then store the input into an array. I'm going to get a string input and with this code, I thought it would work, but it's not working. Any help would be appreciated!

我正在尝试获取用户的输入,然后将输入存储到数组中。我将得到一个字符串输入和这个代码,我认为它会工作,但它不起作用。任何帮助,将不胜感激!

import java.util.Scanner;
public class NameSorting {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String[] array = new String[20];

        System.out.println("Please enter 20 names to sort");              
        Scanner s1 = new Scanner(System.in);
        for (int i = 0; i < 0;){

            array[i] = s1.nextLine();


        }


        //Just to test
        System.out.println(array[0]);

    }

}

3 个解决方案

#1


Since you know that you want to have an array of 20 string:

既然你知道你想拥有一个20字符串的数组:

String[] array = new String[20];

Then your for loop should use the length of the array to determine when the loop should stop. Also you loop is missing an incrementer.

那么你的for循环应该使用数组的长度来确定循环何时应该停止。你循环也缺少增量器。

Try the following code to get you going:

请尝试以下代码来帮助您:

public static void main(String[] args) throws Exception {
    Scanner s = new Scanner(System.in);
    String[] array = new String[20];

    System.out.println("Please enter 20 names to sort");

    for (int i = 0; i < array.length; i++) {
        array[i] = s.nextLine();
    }

    //Just to test
    System.out.println(array[0]);
}

#2


Look at your for-loop, it lacks of increment attribute. Example: for(int i = 0; i < 0; i++) If you want to debug each loop I recommend you to print assignment inside for-loop

看看你的for循环,它没有增量属性。示例:for(int i = 0; i <0; i ++)如果要调试每个循环,我建议您在for循环中打印赋值

for (int i = 0; i < 0;)
{
    array[i] = s.nextLine();
    System.out.println(array[i]);  // Debug 
}

#3


for (int i = 0; i < 0;){
        array[i] = s.nextLine();
    }

For the first iteration i will be initialised to '0' and since i should be less then '0' as per your condition it wont even go into the loop.change the loop to

对于第一次迭代,我将初始化为'0',因为我应该小于'0'根据你的条件它甚至不会进入循环。将循环更改为

for(int i=0;i<20;i++){
array[i]=s.nextLine();
}

#1


Since you know that you want to have an array of 20 string:

既然你知道你想拥有一个20字符串的数组:

String[] array = new String[20];

Then your for loop should use the length of the array to determine when the loop should stop. Also you loop is missing an incrementer.

那么你的for循环应该使用数组的长度来确定循环何时应该停止。你循环也缺少增量器。

Try the following code to get you going:

请尝试以下代码来帮助您:

public static void main(String[] args) throws Exception {
    Scanner s = new Scanner(System.in);
    String[] array = new String[20];

    System.out.println("Please enter 20 names to sort");

    for (int i = 0; i < array.length; i++) {
        array[i] = s.nextLine();
    }

    //Just to test
    System.out.println(array[0]);
}

#2


Look at your for-loop, it lacks of increment attribute. Example: for(int i = 0; i < 0; i++) If you want to debug each loop I recommend you to print assignment inside for-loop

看看你的for循环,它没有增量属性。示例:for(int i = 0; i <0; i ++)如果要调试每个循环,我建议您在for循环中打印赋值

for (int i = 0; i < 0;)
{
    array[i] = s.nextLine();
    System.out.println(array[i]);  // Debug 
}

#3


for (int i = 0; i < 0;){
        array[i] = s.nextLine();
    }

For the first iteration i will be initialised to '0' and since i should be less then '0' as per your condition it wont even go into the loop.change the loop to

对于第一次迭代,我将初始化为'0',因为我应该小于'0'根据你的条件它甚至不会进入循环。将循环更改为

for(int i=0;i<20;i++){
array[i]=s.nextLine();
}