通过用户输入从数组中删除特定元素



我对数组有点陌生,我想知道如何让用户输入他们想从可用电影列表中删除的电影,然后如何返回一个新列表,使每个电影都上升一个位置。我看了很多视频,但我想不通(它会要求我输入用户名,然后向我显示菜单选项。但一旦我按3,它不会询问我要删除哪部电影,也不会从阵列中删除电影

package practice;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner sc = new Scanner(System.in);

List<String> allowedUsernames = Arrays.asList("John", "Sam", "Lucy", "Arthur", "Trisha");
System.out.println("Welcome to the Java movie rental store! To start, please enter your username!");
String name = "";

do {
if (!name.equals("")) {
System.out.println("Please enter a valid username.");
}
name = input.nextLine();
} while (!allowedUsernames.contains(name));

System.out.println("Hello " +name+ "! Please select from the following menu options!");

getchoices();
}

public static void getchoices() {  
Scanner scan = new Scanner(System.in);

String[] optionslist = {
"1) See full list of movies",
"2) Add a movie",
"3) Delete a movie",
"4) Modify a movie",
"5) Exit"
};

for (int i = 0; i < optionslist.length; i++) {
System.out.println(optionslist[i]);
}

int number = scan.nextInt();
System.out.println("You selected: " + optionslist[number-1].substring(3, optionslist[number - 1].length()));
movieList();
}

public static void printNewMovielist() {
// TODO Auto-generated method stub
String[] newList = {
"Shrek",
"Beauty and the Beast",
"Wall-E",
"Cinderella",
"Alice in Wonderland"
};

System.out.println("Movies Available: ");
newMovieList(newList);

newList = deleteMovies(newList); 
System.out.println("The updates list of movies available at the rental store: ");
newMovieList(newList); 
deleteMovies(newList); 
}
public static void movieList() {   
ArrayList<String> Movies = new ArrayList<String>();

System.out.println("Movies Available:");

Movies.add("Shrek");
Movies.add("Beauty and the Beast");
Movies.add("Wall-E");
Movies.add("Cinderella");
Movies.add("Alice in Wonderland");

for (int i = 0; i <Movies.size(); i++) {
System.out.println(Movies.get(i));
}
}
public static String[] deleteMovies (String [] newList) { 
String[] deleteMovie = new String [newList.length - 1]; 

for (int i = 0; i < newList.length; i++) { 
deleteMovie[i] = newList[i]; 
}

Scanner sc = new Scanner (System.in); 
System.out.println("Which movie would you like to delete?"); 

return deleteMovie; 
}
public static void newMovieList(String [] newList) { 
for (int i = 0; i < newList.length; i++) { 
System.out.println((i - 1) + " )" + newList[i]);   
}
}  
public static String[] deleteMovies(String[] newList) {
String[] deleteMovie = new String[newList.length - 1];
for (int i = 0; i < newList.length; i++) {
deleteMovie[i] = newList[i];
}
Scanner sc = new Scanner(System.in);
System.out.println("Which movie would you like to delete?");
return deleteMovie;
}

您可以在上面看到未使用sc。您创建了扫描仪,但没有要求用户提供nextLine()!将此与您使用input扫描仪的方式进行对比。因此,您的程序将退出,因为它已完成(它不等待用户的nextLine()(。

编辑:正如K450在评论中指出的,您在代码中甚至没有达到以上这一点。你只需打印出他们选择的选项,然后列出所有电影,仅此而已。如果您的程序发现用户试图删除电影(输入该选项(,则应调用此方法。

致";删除";通过移动所有元素,您可以创建一个长度为n - 1的数组,与您所做的类似,但请注意,原始数组中删除元素后的所有元素的索引将比新数组中应复制元素的位置大一。

例如,如果您想删除数组中的Sam

["John", "Sam", "Mary"]

那么你会想要`

newArray[0] = oldArray[0];
newArray[1] = oldArray[2];

有一些方法可以在不使用for循环的情况下做到这一点,但我建议至少将其作为一种练习。不过,对于您的用例来说,更好的选择可能是ArrayList,因为您可以轻松地删除索引中的元素。

相关内容

  • 没有找到相关文章

最新更新