如何在选择电影时让用户做出另一种选择?

时间:2014-05-04 06:48:59

标签: java arrays multiple-choice

我正在编写一个程序,用户可以选择购买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 




}

}

4 个答案:

答案 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循环,如下所示:

  • 在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"));