- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以...我最近正在学习一些 Java,如果您愿意,我希望您能帮助我完成我的第一个程序。这是我的程序代码,它显示牌组中的随机卡片。
我的问题如下:有没有更聪明的方法让这个程序运行?
/*
* File: RandomCard.java
* ----------------
* This program display random card from the deck with it's own rank
* (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Quenn, King) and suit (Clubs,
* Diamonds, Hearts, Spades).
*/
import acm.program.*;
import acm.util.*;
public class RandomCard extends ConsoleProgram {
/* Run the program */
public void run() {
println("This program displays a random card from the deck.");
println("You random card is " + getRandomRank() + " "
+ getRandomSuit() + ".");
}
/* Get random rank for the card. */
private String getRandomRank() {
int rank = rgen.nextInt(1, 13);
switch (rank) {
case 1: return "Ace";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
default: return null;
}
}
/* Create random suit from within Clubs, Diamonds, Hearts and Spades. */
private String getRandomSuit() {
int suit = rgen.nextInt(0, 3);
switch (suit) {
case 0: return "Clubs";
case 1: return "Diamonds";
case 2: return "Hearts";
case 3: return "Spades";
default: return null;
}
}
/* Create an instance variable for the random number generator */
private RandomGenerator rgen = new RandomGenerator();
}
最佳答案
一个小的改进:任何时候你有这样的 switch 语句:
switch (rank) {
case 1: return "Ace";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
default: return null;
}
您可以改为声明一个数组:
String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King" };
然后:
if (rank < 1 || rank > 13) return null;
return ranks[rank - 1]; // arrays are zero-based
关于java - 只是另一个新手 Java 编码器请求批准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5051783/
构建用于批准或拒绝用户的 RESTful 资源的正确方法是什么? 此资源旨在由管理员审查用户。管理员可以批准或拒绝用户请求。请求需要有 user_id和 updated_status那可能是 appr
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 2年前关闭。 Improve this
我计划创建状态通知。例如,每当信息是 "Pending", it turns to color orange. When "Approved" it turns to color green and
假设我设置了一个存储库。我希望有的用户可以推送,有的用户我想先过一遍推送的内容再批准。有条件推送之类的概念吗?待批准推送队列? 最佳答案 您本质上需要一个代码审查工具。 git 有一些,但最好的可能是
我目前正在将我的一个网站从旧系统升级到 HTML5,但我在使用 Google Maps JavaScript API v3 时遇到了一些问题。要从他们的 API 获取数据,您必须有一个 key - 当
我们想将 JIRA 与 GITLAB 集成以获得批准。当 CICD Pipeline 运行时,它会部署到 DEV/QA,当它部署到 PROD 时,我们要检查两件事 经理/代码所有者批准了一项合并请求
我在我的网站中使用 Authy 进行 2 因素身份验证。我的代码发送了一个推送通知,我在手机上的 Authy 应用程序中收到了它。但问题是有时当我尝试接受/拒绝请求时,我收到一条错误消息 There
我正在修改网站以允许用户输入内容。但是,一旦他们创建了该内容,就需要在发布之前获得批准。这很容易通过在该内容的记录中设置一个“中等”位来实现,并且只需要一个表。 现在我还希望允许用户像在 StackO
我正在使用 ubuntu 12.04,我有一个应用程序,当我运行时,退出: ./ardrone_navigation Setting locale to en_GB.UTF-8 Errore di s
我在 gitlab 有两个分支 master 和 release。 在发布分支,我希望它只有提交消息,如:v0.1.0,v0.1.1。 说在我完成 v0.1.0 开发后,我在 master 中有几个功
我们为一家想要在自己的帐户下发布该应用程序的公司开发了一个应用程序。它是在我们的帐户上开发的,并且已从我们自己的帐户附加了配置文件。做这个的最好方式是什么?我是否必须向他们发送 xCode 项目并告诉
我正在构建一个网站,用户可以在其中更改其公开显示的个人资料。但是,我需要所有更改在上线之前都得到管理员的批准。在更改获得批准之前,他们的旧个人资料将显示。在管理员中应该有一个等待批准的配置文件列表。最
我正在构建一个网站,用户可以在其中更改其公开显示的个人资料。但是,我需要所有更改在上线之前都得到管理员的批准。在更改获得批准之前,他们的旧个人资料将显示。在管理员中应该有一个等待批准的配置文件列表。最
在我们的团队中,一名开发人员将应用送交审核。 它通过了,但在某些时候开发者证书不再有效。 我们现在无法在外部或内部测试人员中下载应用。 如果我们发布那个版本会怎样? 我将感谢您的经验和意见。 最佳答案
更新: $(document).ready(function () { $('#C1All').click(functio
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 8 年前。 Improve
我在 Gitlab 中找不到在我看来常见情况的选项,即仅批准 merge 请求,但不触发构建。我需要将流程分为两个操作:审批者审查并批准 merge 请求,但 merge 请求的作者将进行 merge
这个问题在这里已经有了答案: How to access the correct `this` inside a callback (15 个答案) 关闭 3 年前。
我正在尝试使用新的 Azure AD Privileged Identity Management REST API 批准/拒绝角色激活请求。 我已经能够使用以下请求读取所有待处理的角色激活请求: G
我的网站是一个用 AngularJs 编写的单页应用程序。 我正在尝试为某些页面添加 AdSense。提交后,我的网站被拒绝,原因如下:“内容不足”。 我的问题是:考虑到我们使用 escaped_fr
我是一名优秀的程序员,十分优秀!