1

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




}

}

4

4 回答 4

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我更熟悉的直接循环。

于 2014-05-04T06:59:42.003 回答
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"));
于 2014-05-04T07:00:15.773 回答
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"));

    }
于 2014-05-04T06:56:34.697 回答
0

您应该按如下方式使用 do-while 循环:

  • 在 do 块中,输入您希望发生的代码
    • 这应该是(包括)“选择电影”和检索用户重复选择之间的所有内容。
    • 这意味着所有代码都保证发生一次,如果满足 while 条件,将再次发生。
  • while 条件的代码块在它上面,因此以分号结束。
于 2014-05-04T06:58:57.657 回答