我正在编写一个程序,用户可以选择购买7部电影。用户可以选择最多7个电影,而不是将电影的价格加起来,而不是打印总价。我必须在我的程序中使用数组。我的问题是当用户已经选择他们的第一部电影时,如果他们想要购买另一部电影,他们可以选择。我对如何编写程序感到困惑,让用户有另一种选择来选择1部以上的电影。我需要帮助解决我的问题,因为当我运行我的程序时,我不会让我选择另一部电影。这是我的代码:
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
}
}
答案 0 :(得分:3)
我会像这样清理你的代码:
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{}
块意味着如果用户错误地输入要观看的电影,我们仍然可以“捕获”无效输入。
我最初使用的是do-while
,但这个结构对我来说永远不会有效,所以我把它换成了一个我更熟悉的直接while
循环。
答案 1 :(得分:0)
do {
//Switch Statement to give user a menu to choose a movie
System.out.println("Choose your movie number:");
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();
priceTotal = movieCost[userChoice];
} while (choice.equalsIgnoreCase("Yes"));
}
答案 2 :(得分:0)
您应该使用do-while循环,如下所示:
答案 3 :(得分:0)
You used do-while loop in wrong place in wrong way.Change Your code to this :
do{
try{
userChoice = keyboard.nextInt();
} catch(Execption e){
System.out.Println("Error in input!");
}
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
try{
System.out.println("Do you want to add another movie. Enter Yes or No");
choice = keyboard.nextLine();
} catch(Execption e){
System.out.Println("Error in input!");
}
priceTotal = movieCost[userChoice];
} while (choice.equalsIgnoreCase("Yes"));