有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将用户输入(字符串)存储到JAVA数组中

好的,对于我的计算机科学课程,我必须制作两个独立的数组,用两个不同方法的用户输入填充它们。我一直坚持将代码中的输入存储在两个数组中。我甚至不知道从哪里开始。我试着寻找例子,但什么也没找到。我对编码也相当陌生

  import java.util.Scanner;
 /**
  * @author SH
  *
  */
  public class SearchSort {

Scanner console = new Scanner(System.in);

public void inputFavoriteMovies(){
    System.out.println("Enter 6 of your favorite movies");
    int x = 0;
    while(x<6){
    String movies = console.nextLine();
    x++;
    }
}
public void inputFavoriteMusic(){
    System.out.println("Enter 5 of your Favorite Songs");
    int y = 0;
    while(y<5){
        String music = console.nextLine();
        y++;
    }
}

public static void main(String[] args) {        
    new SearchSort().inputFavoriteMovies();
    new SearchSort().inputFavoriteMusic();

    String[] myFavoriteMovies = new String[6];


    String[] myFavoriteMsuic = new String[5];
 }

}

共 (2) 个答案

  1. # 1 楼答案

    您需要在运行输入方法之前创建数组,而不是之后。然后,在输入每个字符串时,将该字符串放入适当的数组中

  2. # 2 楼答案

    看看我在下面对你的代码所做的更改,以及我对每一项的评论。如果你想让这变得更加灵活,可以在方法中使用ArrayList,让用户输入想要的数量,然后使用。ArrayList的add()方法添加用户键入的每个条目。你必须想出一些用户在完成时键入的“sentinel”值(空字符串可以工作;然后你必须检查他们输入的字符串的inputValue.Length(),并与0进行比较,一旦长度==0,就退出while循环)。然后,可以使用ArrayList的toArray()并将其转换为数组,然后返回调用代码。这将给你一个非常动态的安排和相当多的灵活性。您可能还没有了解ArrayList,但很可能很快就会了解

    希望这对你有所帮助,祝你好运

       import java.util.Scanner;
         /**
          * @author SH
          *
          */
          public class SearchSort {
    
        /*
    
        Since your code is essentially linear in nature (and not truly object-oriented), it's probably best to make it all static. You'll note I added the static modifier to the 2 methods you created along with the console declaration.
    
        */
    
        static Scanner console = new Scanner(System.in);
    
        public static void inputFavoriteMovies(String[] storage){
    /*
        For both of these methods, it makes sense to pass an array into them and then use the passed array for storage. Since arrays are essentially pass by reference, when you make changes to the array in this method, the array that was passed in will be modified when you return from this code.
    
    Using the length of the array for both the prompt and the loop control helps to reduce the "index out of bounds" errors that may otherwise occur. It would actually be a little better to code this as a for loop:
    
    for (int x=0; x < storage.length; x++ ) {
    <insert all the code>
    } // end for
    
    */
            System.out.println("Enter " + storage.length + " of your favorite movies");
            int x = 0;
            while(x < storage.length){
            storage[x] = console.nextLine();
            x++;
            }
        }
    
    /*
    comments here are essentially identical to those above.
    */
    
        public static void inputFavoriteMusic(String[] storage){
            System.out.println("Enter " + storage.length + " of your Favorite Songs");
            int y = 0;
            while(y < storage.length){
                storage[y] = console.nextLine();
                y++;
            }
        }
    
        public static void main(String[] args) {        
    
    /*
    Here in main, you basically declare an array as large as you want. Since you are now using methods that look at the length of the array, the code is slightly more abstract and gives you a tad more flexibility in design since it's not hard-coded
    */
    
            String[] myFavoriteMovies = new String[6];
    
    // After declaring the array, pass it to the input method you created earlier. When that 
    // method returns, the array will contain the values the user entered.
    
            inputFavoriteMovies(myFavoriteMovies);
    
    // same as above - declare an array, then pass it to your input method.
            String[] myFavoriteMusic = new String[5];
            inputFavoriteMusic(myFavoriteMusic);
    
    /* now, if you want to print the results, you'd do a pair of for loops that iterate
       over each array and output the results. For a bit more elegance, you could use
       this form of the for loop:
    
       for (String userInput : myFavoriteMovies) {
    System.out.println(userInput);
    }
    
    for (String userInput : myFavoriteMusic) {
    System.out.println(userInput)
        }
    
      }
    }