Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 - Fatal编程技术网

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

将用户输入(字符串)存储到JAVA数组中,java,arrays,Java,Arrays,好的,对于我的计算机科学课程,我必须制作两个独立的数组,用两个不同方法的用户输入填充它们。我一直坚持将代码中的输入存储在两个数组中。我甚至不知道从哪里开始。我试着寻找例子,但什么也没找到。我对编码也相当陌生 import java.util.Scanner; /** * @author SH * */ public class SearchSort { Scanner console = new Scanner(System.in); public void input

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

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

}
import java.util.Scanner;
/**
*@author-SH
*
*/
公共类搜索排序{
扫描仪控制台=新扫描仪(System.in);
public void inputFavoriteMovies(){
System.out.println(“输入你最喜欢的6部电影”);
int x=0;

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

请查看我在下面对代码所做的更改以及与每个字符串相关的注释。如果您想使这一操作更加灵活,请在方法中使用ArrayList然后让用户输入所需的任意数量,并使用ArrayList的.add()方法将每个条目添加到用户类型中。您必须想出一些用户在完成输入时键入的“sentinel”值(空字符串可以工作;然后必须检查inputValue.Length()然后,您可以使用toArray()并将其转换为数组,然后返回调用代码。这将为您提供非常动态的安排和更大的灵活性。您可能尚未了解ArrayList,但很可能很快就会了解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)
    }

  }
}
import java.util.Scanner;
/**
*@author-SH
*
*/
公共类搜索排序{
/*
由于您的代码本质上是线性的(而不是真正面向对象的),因此最好将其全部设置为静态。您会注意到,我将静态修饰符添加到了您创建的2个方法以及控制台声明中。
*/
静态扫描仪控制台=新扫描仪(System.in);
公共静态void inputFavoriteMovies(字符串[]存储){
/*
对于这两种方法,将数组传递给它们,然后使用传递的数组进行存储是有意义的。由于数组本质上是按引用传递的,因此当您在此方法中更改数组时,从该代码返回时,将修改传入的数组。
为提示符和循环控件使用数组的长度有助于减少可能发生的“索引越界”错误。将其编码为for循环实际上会更好一些:
用于(int x=0;x
如果您没有找到任何您研究不够的内容。您当前正在解析字符串,然后什么也不做。您可能希望使字符串[]数组类变量以使它们对您的方法可见。进一步阅读:作用域。此外,我认为人们不再使用
console
。可能
Scanner
BufferedReader
您不想尝试对所有内容进行编码,然后一次运行所有内容。这就像建造一辆汽车:您无法完成全部工作,也无法运行试着运行它,因为如果它不运行,你就不知道什么在运行,什么不在运行。从程序的每个单独部分开始,一直运行到它完全按照你想要的方式运行,并将工作部分合并到最终产品@ᴋᴇʏsᴇʀ他没有使用控制台,这只是变量的名称。他使用的是
Scanner
类。有点混乱,呵呵