好吧,对于我的计算机科学课程,我必须制作两个单独的数组,用两个单独的方法填充用户输入。我坚持实际上将我的代码输入存储在我的两个数组中。我甚至不知道从哪里开始。我试过寻找例子,但我没有找到任何东西。我对编码也很陌生。
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];
}
}
答案 0 :(得分:1)
请查看我在下面对您的代码所做的更改以及与每个相关的评论。如果你想让它变得更加灵活,可以在方法中使用ArrayList,让用户输入所需的数量,并使用ArrayList的.add()方法添加用户输入的每个条目。你必须提出一些用户在完成时键入的“哨兵”值(空字符串可以工作;然后你必须检查他们输入的字符串的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)
}
}
}
答案 1 :(得分:0)
您需要在运行输入方法之前创建数组,而不是之后。然后,在输入每个字符串时,将字符串放在适当的数组中。