gpt4 book ai didi

java - 删除用户输入的信息

转载 作者:行者123 更新时间:2023-12-02 13:24:50 25 4
gpt4 key购买 nike

我正在尝试为一些作业制作一个电影数据库。添加删除功能时遇到困难。由于我的 moivecount 变量,我在尝试重新添加另一部电影时陷入困境。如果我删除 movie3(如果我有 4 部电影),则 moviecount == 3,当我尝试添加另一部电影时,当我需要添加 movie3 时,它会再次要求 movie4。

import java.util.*;
public class Interface {
private void run()
{


Scanner console = new Scanner(System.in);
Movie m1;
Movie m2;
Movie m3;
Movie m4;

String name, director;
int size, duration, option;
int moviecount = 0;

m1 = new Movie();
m2 = new Movie();
m3 = new Movie();
m4 = new Movie();

do {
System.out.println("Import new movie:(0), Delete movie from Database (2), View Movies: (3), Exit (9): ");
option = console.nextInt();
switch(option)
{
case 0:
if(moviecount == 0)
{
System.out.print("First Movie: ");
name = console.next();
m1.setName (name);
System.out.print("Director: ");
director = console.next();
m1.setDirector (director);
System.out.print("Size in MB: ");
size = console.nextInt();
m1.setSize(size);
System.out.print("Duration in minutes: ");
duration = console.nextInt();
m1.setDuration(duration);
moviecount++;
break;


}

else if(moviecount == 1)
{
System.out.print("Second Movie: ");
name = console.next();
m2.setName (name);
System.out.print("Director: ");
director = console.next();
m2.setDirector (director);
System.out.print("Size in MB: ");
size = console.nextInt();
m2.setSize(size);
System.out.print("Duration in minutes: ");
duration = console.nextInt();
m2.setDuration(duration);
moviecount++;
break;
}

else if(moviecount == 2)
{
System.out.print("Third Movie: ");
name = console.next();
m3.setName (name);
System.out.print("Director: ");
director = console.next();
m3.setDirector (director);
System.out.print("Size in MB: ");
size = console.nextInt();
m3.setSize(size);
System.out.print("Duration in minutes: ");
duration = console.nextInt();
m3.setDuration(duration);
moviecount++;
break;
}

else if(moviecount == 3)
{
System.out.print("Fourth Movie: ");
name = console.next();
m4.setName (name);
System.out.print("Director: ");
director = console.next();
m4.setDirector (director);
System.out.print("Size in MB: ");
size = console.nextInt();
m4.setSize(size);
System.out.print("Duration in minutes: ");
duration = console.nextInt();
m4.setDuration(duration);
moviecount++;
break;
}

else
{
System.out.print("Too many movies currently stored \n" );
break;
}

case 2: System.out.print("Which movie to delete? (1), (2), (3), (4) \n");
option = console.nextInt();
switch(option)
{
case 1: m1.setName(null);
m1.setDirector(null);
m1.setSize(0);
m1.setDuration(0);
System.out.print("Movie 1 deleted! ");
moviecount--;
break;

case 2: m2.setName(null);
m2.setDirector(null);
m2.setSize(0);
m2.setDuration(0);
moviecount--;
break;

case 3: m3.setName(null);
m3.setDirector(null);
m3.setSize(0);
m3.setDuration(0);
moviecount--;
break;

case 4: m4.setName(null);
m4.setDirector(null);
m4.setSize(0);
m4.setDuration(0);
moviecount--;
break;

}

case 3: System.out.print("Movies stored in database: \n");
System.out.print(m1.getName()+"\n");
System.out.print(m2.getName()+"\n");
System.out.print(m3.getName()+"\n");
System.out.print(m4.getName()+"\n");










}
}
while(option!=9);



}
public static void main(String[] args){
Interface intFace = new Interface();
intFace.run();
}
}

最佳答案

问题出在你的设计上。您遇到这个问题是因为您实际上并没有将电影存储在任何地方。您只需创建 4 个不同的电影对象并将它们打印出来。如果您更改了打印语句的顺序,它们的顺序将会不同。

您将需要添加某种数据结构 - 任何数组、列表或某种其他类型的集合来存储您的电影。

例如,您可以创建一个列表来存储您的电影数据。

    //create new List called movieList
List movieList = new ArrayList();

//add movies to the list
movieList.add(m1);
movieList.add(m2);
movieList.add(m3);
movieList.add(m4);


//remove movies from the list by position
movieList.remove(0);
movieList.remove(1);
movieList.remove(2);
movieList.remove(3);

您可以使用列表的 add() 方法将电影添加到列表中,并使用 remove() 方法将其删除。请记住,remove 会删除您提供的索引处的元素,并且索引从 0 开始。

因此,例如要删除第一部电影,您可以调用 movieList.remove(0)。第二部电影的索引为 1,依此类推。

关于java - 删除用户输入的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43433371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com