gpt4 book ai didi

java - 我怎样才能让用户在选择电影时做出另一种选择?

转载 作者:行者123 更新时间:2023-11-29 05:23:59 25 4
gpt4 key购买 nike

我正在编写一个程序,用户可以在其中选择购买 7 部电影。用户最多可以选择 7 部电影,然后将电影的价格相加并打印总价。我必须在我的程序中使用数组。我的问题是当用户已经选择了他们的第一部电影时,他们可以选择是否要购买另一部电影。我很困惑我应该如何通过给用户另一种选择来选择不止一部电影来编写我的程序。我需要关于如何解决我的问题的帮助,因为当我运行我的程序时,它不会让我选择另一部电影。这是我的代码:

import java.util.Scanner;
public class MovieHits {

public static void main(String[] args)
{
//Declare Variables
Scanner keyboard = new Scanner(System.in);
int userChoice = 0;
String choice;
int priceTotal = 0;
int [] number = {1, 2, 3, 4, 5, 6, 7};

String [] Movie = new String [7];
int [] movieCost ={ 5, 4, 3, 6, 4, 4, 3};

Movie [0] = "Iron Man";
Movie [1] = "Transformers";
Movie [2] = "Godzilla";
Movie [3] = "Fast and Furious";
Movie [4] = "Captain America";
Movie [5] = "X Men";
Movie [6] = "Rio";

//Welcome the user
System.out.println("Hello, Welcome to TC Movies OnDemand.");

//Display the listed movies so the user can know with movie they want to watch
System.out.println("\nChoose which movie you want to watch: ");
for ( int index = 0; index < 7; index = index + 1 )
{
System.out.println(number[index]+ ": " + Movie[index]);
System.out.println("\t" + "Price: $" + movieCost[index]);
}


//Switch Statement to give user a menu to choose a movie
userChoice = keyboard.nextInt();

switch (userChoice)
{
case 1:
System.out.println("The movie you have chosen is " + Movie[0] + "\nPrice is: " + "$" + movieCost[0]);
break;

case 2:
System.out.println("The movie you have chosen is " + Movie[1] + "\nPrice is: " + "$" + movieCost[1]);
break;

case 3:
System.out.println("The movie you have chosen is " + Movie[2] + "\nPrice is: " + "$" + movieCost[2]);
break;

case 4:
System.out.println("The movie you have chosen is " + Movie[3] + "\nPrice is: " + "$" + movieCost[3]);
break;

case 5:
System.out.println("The movie you have chosen is " + Movie[4] + "\nPrice is: " + "$" + movieCost[4]);
break;

case 6:
System.out.println("The movie you have chosen is " + Movie[5] + "\nPrice is: " + "$" + movieCost[5]);
break;

case 7:
System.out.println("The movie you have chosen is " + Movie[6] + "\nPrice is: " + "$" + movieCost[6]);
break;
default:
System.out.println("I'm Sorry you did not chose a movie.");
break;
}


//Tell the user if they want to get another movie
System.out.println("Do you want to add another movie. Enter Yes or No");
choice = keyboard.next();

do
{
priceTotal = movieCost[userChoice];

}
while (choice.equalsIgnoreCase("Yes"));
{

}
//Tell the user the total price




}

最佳答案

我会像这样清理你的代码:

String choice = "Yes"; //Set to yes by default.
//Welcome the user
System.out.println("Hello, Welcome to TC Movies OnDemand.");

while(choice.equalsIgnoreCase("Yes")){
//Display the listed movies so the user can know with movie they want to watch
System.out.println("\nChoose which movie you want to watch: ");
for ( int index = 0; index < 7; index = index + 1 ){
System.out.println(number[index]+ ": " + Movie[index]);
System.out.println("\t" + "Price: $" + movieCost[index]);
}

userChoice = keyboard.nextInt();

try{
System.out.println("The movie you have chosen is " + Movie[userChoice] + "\nPrice is: " + "$" + movieCost[userChoice]);
}catch(Exception e){
System.out.println("Sorry, you did not choose a movie.");
}

//Tell the user if they want to get another movie
System.out.println("Do you want to add another movie? Enter Yes or No.");
choice = keyboard.next();

}

这会重构您的代码,这意味着您不必为您拥有的每种电影类型都使用很长的 switch 语句。

我们使用 userChoice 作为要查找的实际索引,try{}catch{} block 意味着我们仍然可以“捕获”无效输入,如果用户错误地输入了要观看的电影。

我最初使用的是像你一样的 do-while,但那个结构对我来说永远不起作用,所以我将它换成一个直接的 while 循环,我比较熟悉。

关于java - 我怎样才能让用户在选择电影时做出另一种选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23453446/

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