- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的程序是关于用户从列出的 7 部电影中选择一部电影或许多人想要观看的电影以及显示的价格。该程序将为用户提供 7 部电影可供选择,如果他们想选择另一部电影,价格将加到总数中。到目前为止,我有一系列电影和价格,但我不确定应该如何为电影做出用户选择并添加总价格。我应该使用 switch 语句还是循环我很困惑。这是我到目前为止所拥有的:
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 [] 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(Movie[index]);
System.out.println("\t" + "Price: $" + movieCost[index]);
}
//Switch Statement to give user a menu to choose a movie
switch (userChoice)
{
case 1:
System.out.println("The movie you have chosen.");
break;
最佳答案
您应该使用循环来打印电影选择。读取用户输入后,您可以使用 switch case 来确定选择了哪部电影。您的示例代码实际上并不读取用户输入,实例化的 Scanner 对象从未被使用。在开关盒之前,您应该有例如
userChoice = keyboard.nextInt();
但是,有一种更面向对象的“Java 方式”可以使用 Map 而不是 String 数组来完成此操作,并且无需使用 switch-case:
public class MovieHits {
public static class Movie {
private int cost;
private String name;
public Movie(String name, int cost) {
this.cost = cost;
this.name = name;
}
public int getCost() {
return cost;
}
public String getName() {
return name;
}
}
public static void main(String[] args) {
//Declare Variables
Scanner keyboard = new Scanner(System.in);
int userChoice;
Map<Integer, Movie> movies = new HashMap<Integer, Movie>();
movies.put(1, new Movie("Iron Man", 5));
movies.put(2, new Movie("Transformers", 4));
movies.put(3, new Movie("Godzilla", 3));
// ...
//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: ");
Set<Integer> keys = movies.keySet();
for (Integer key : keys) {
Movie movie = movies.get(key);
System.out.println(key + ": " + movie.getName());
System.out.println("\t" + "Price: $" + movie.getCost());
}
userChoice = keyboard.nextInt();
System.out.println("You have chosen " + movies.get(userChoice).getName());
内部类通常违背最佳实践,但在本例中我使用它来保持简单。
如果用户可以选择多个电影,则在 while 循环中读取 userChoice 并用特定数字或用户输入空行来中断它。在循环内存储选定的电影,例如在列表中,计算用户在循环内或选择所有想要的电影后观看的总价格。
关于java - 如何让用户选择任何电影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23308253/
问题是什么? 我在 Pythonanywhere.com 上运行 This error can be due to the fact that ImageMagick is not installed
我目前正在设计一个电影数据库。我目前有一个包含电影标题、长度等的电影表。然后我有一个类型转换。我在这些之间有一个与 filmid 和 actorid 之间的联结表。这将包含在影片中扮演角色的所有 Ac
所以,我有一个包含 3 个表和相关列的数据库: 标签:TID、标签名称 电影:MID、movie_title tags_to_movies:电影 ID、标签 ID 如何选择至少具有我想要的标签的电影。
我的程序在 GNUPlot 中生成了 100 个图。我想将它们连接成一部短片,其中每个情节约为 0.1 秒。我怎样才能最有效地做到这一点? 我能想到的效率最低的方法是拍摄每个情节的屏幕截图,以某种方式
我正在使用 ffmpeg 剪切视频文件: ffmpeg - -ss 00:00:00 -t 00:00:30 不包括 *.avi 格式的不同文件格式是可以的。对于 avi 电影,ffmpeg 显示视
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
是否有任何方法可以模拟视频应用程序,以便我们仍然保持对设备(iPad/iPhone)的控制,但通过电缆将视频发送到电视?我研究了屏幕镜像,但它对于视频来说太慢了,而且无论如何,Apple 不再允许屏幕
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我正在开发 Windows 服务,我的 Windows 服务使用“ffmpeg”,同时我正在开发用于测试代码的桌面应用程序。 我想在我的视频中导入水印。我可以用我的桌面应用程序做到这一点。但是当我运行
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在编写一个Mac应用程序,可以将不同来源的视频下载到本地硬盘。到目前为止效果很好,但我想允许用户在下载这些视频时播放这些视频。浏览 QTKit 的文档,我找不到任何方法可以让我将电影数据分段提供给
我已经尝试通过另一个用户的建议,使用嵌入 Flash 的 swfobject 方法。然而,我一定没有那么聪明。 下面是我想出的怪物,但没有电影播放,当我控制+单击(Mac)时,我什至无法将它区分为 F
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。它不符合 Stack Overflow guidelines 。它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题
我注意到 GIF 并不是真正兼容所有浏览器,所以我一直在使用电影。直到现在一直在寻找。我有一个带有用 css 编写的背景图片的网站。我觉得这行不通,但我尝试像链接图像一样链接电影,但它甚至没有出现。下
有人知道是否有可能(在 PHP 中)从服务器上的 quicktime 电影中获取帧/缩略图吗? 类似于通常的 GD 缩略图生成,但用于 .mov 文件。 注意:我使用的是 dreamhost,所以我只
我需要根据一系列生成的图像制作一部电影。 (我正在根据物理建模程序的输出创建图像。) 我在 QtKitCreateMovie 中找到了 Apple 的示例,并将其用作起点。我没有从应用程序包中加载 j
我想创建具有 2 个 View 的非常简单的应用程序:-> 列出一些标签-> 带电影的单 View 每个标签都连接到一个 flash 电影。这部电影将在我自己的网络服务器上。我想在我的应用程序中与一些
将 JSON 传入和传出 Flash 电影的最佳方式是什么? 目前在 javascript 和 Flash 之间进行通信: 我正在使用“flashvars”在最初加载时将数据传递给 Flash 电影
我正在开发一个 chromecast 发送器应用程序,我希望在其中启动 Netflix 并播放请求的电影。但是,我只能设法启动 Netflix 应用程序,但它不会加载视频,因为我不确定需要随请求发送哪
因此,当我尝试访问我的应用程序上的电影页面时出现以下错误: ActionController::ParameterMissing (param is missing or the value is e
我是一名优秀的程序员,十分优秀!