Java 我如何让用户在选择电影时做出其他选择?
我正在编写一个程序,用户可以选择购买7部电影。用户可以选择多达7部的电影,然后将电影的价格相加,然后打印总价格。我必须在程序中使用数组。我的问题是,当用户已经选择了他们的第一部电影时,如果他们想购买另一部电影,他们会有一个选择。我不知道该如何通过给用户另一个选择来选择多部电影来编写程序。我需要关于如何解决问题的帮助,因为当我运行我的程序时,它不会让我选择另一部电影。这是我的代码:Java 我如何让用户在选择电影时做出其他选择?,java,arrays,multiple-choice,Java,Arrays,Multiple Choice,我正在编写一个程序,用户可以选择购买7部电影。用户可以选择多达7部的电影,然后将电影的价格相加,然后打印总价格。我必须在程序中使用数组。我的问题是,当用户已经选择了他们的第一部电影时,如果他们想购买另一部电影,他们会有一个选择。我不知道该如何通过给用户另一个选择来选择多部电影来编写程序。我需要关于如何解决问题的帮助,因为当我运行我的程序时,它不会让我选择另一部电影。这是我的代码: import java.util.Scanner; public class MovieHits { public
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
}
import java.util.Scanner;
公映类电影{
公共静态void main(字符串[]args)
{
//声明变量
扫描仪键盘=新扫描仪(System.in);
int userChoice=0;
字符串选择;
int priceTotal=0;
int[]数={1,2,3,4,5,6,7};
字符串[]电影=新字符串[7];
int[]movieCost={5,4,3,6,4,4,3};
电影[0]=“钢铁侠”;
电影[1]=“变形金刚”;
电影[2]=“哥斯拉”;
电影[3]=“快速和愤怒”;
电影[4]=“美国队长”;
电影[5]=“X战警”;
电影[6]=“里约”;
//欢迎用户
System.out.println(“您好,欢迎来到TC Movies OnDemand”);
//显示列出的电影,以便用户了解他们想要观看的电影
System.out.println(“\n选择要观看的电影:”;
对于(int index=0;index<7;index=index+1)
{
System.out.println(编号[索引]+”:“+电影[索引]);
System.out.println(“\t”+”价格:$”+movieCost[index]);
}
//Switch语句为用户提供选择电影的菜单
userChoice=keyboard.nextInt();
开关(用户选择)
{
案例1:
System.out.println(“您选择的电影是“+movie[0]+”\n价格是“+”$”+movieCost[0]);
打破
案例2:
System.out.println(“您选择的电影是“+movie[1]+”\n价格是“+”$”+movieCost[1]);
打破
案例3:
System.out.println(“您选择的电影是“+movie[2]+”\n价格是“+”$”+movieCost[2]);
打破
案例4:
System.out.println(“您选择的电影是“+movie[3]+”\n价格是“+”$”+movieCost[3]);
打破
案例5:
System.out.println(“您选择的电影是“+movie[4]+”\n价格是“+”$”+movieCost[4]);
打破
案例6:
System.out.println(“您选择的电影是“+movie[5]+”\n价格是“+”$”+movieCost[5]);
打破
案例7:
System.out.println(“您选择的电影是“+movie[6]+”\n价格是“+”$”+movieCost[6]);
打破
违约:
System.out.println(“很抱歉你没有选择电影。”);
打破
}
//告诉用户是否想要另一部电影
System.out.println(“是否要添加其他电影,请输入是或否”);
选择=键盘。下一步();
做
{
priceTotal=movieCost[userChoice];
}
while(choice.equalsIgnoreCase(“是”);
{
}
//告诉用户总价
}
}您应该按如下方式使用do while循环:
- 在do块中,输入希望执行的代码
- 这应该是介于(包括)“选择电影”和检索用户的重复选择之间的所有内容
- 这意味着所有这些代码都保证只出现一次,如果满足while条件,它们将再次出现
- while条件的代码块位于其上方,因此以分号终止
- 我会像这样清理您的代码:
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();
}
String choice=“是”//默认设置为“是”。
//欢迎用户
System.out.println(“您好,欢迎来到TC Movies OnDemand”);
while(choice.equalsIgnoreCase(“是”)){
//显示列出的电影,以便用户了解他们想要观看的电影
System.out.println(“\n选择要观看的电影:”;
对于(int index=0;index<7;index=index+1){
System.out.println(编号[索引]+”:“+电影[索引]);
System.out.println(“\t”+”价格:$”+movieCost[index]);
}
userChoice=keyboard.nextInt();
试一试{
System.out.println(“您选择的电影是“+movie[userChoice]+”\n价格是“+”$”+movieCost[userChoice]);
}捕获(例外e){
System.out.println(“对不起,您没有选择电影。”);
}
//告诉用户是否想要另一部电影
System.out.println(“是否要添加其他电影?输入是或否”);
选择=键盘。下一步();
}
这将重构您的代码,这意味着您不必对每种电影类型都使用长的switch
语句
我们使用了userChoice
作为要查找的实际索引,而try{}catch{}
块意味着如果用户错误地输入要观看的电影,我们仍然可以“捕获”无效输入
我最初使用的是一个像你一样的
do-while
,但是这种结构对我来说从来都不起作用,所以我把它换成了一个我更熟悉的直接while
循环。do-while循环在错误的位置放置do{/code>beforeuserChoice=keyboard.nextInt();
。谢谢兄弟,我将尝试修复我的程序,看看我得到了什么。谢谢你的建议。现在我遇到了另一个问题,当我输入“是”以选择另一部电影时,它会重新打印我第一次得到的相同测试choice@KickButtowski怎么会这样呢?当我运行它时,它只会询问一次,程序就会终止:)啊!这就是我使用数据处理器得到的结果我在现实生活中从未使用过的结构。编辑…现在。它是k,但你仍然有相同的问题。:)你应该将捕获块中的信息更改为更有用的内容,例如电影数量不超过七部或类似的内容。只需指出你的精彩答案。
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"));
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"));