- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以向我解释为什么我似乎无法获得多个随机数来匹配我的猜测吗?这里的目标是产生一个随机数,然后要求用户输入五个数字并查看有多少匹配。我使用了一个临时的 JOptionPane(不在下面的代码中)来显示随机数是什么,但它只显示一个随机值。问题可能出在数组中,还是仅仅是因为我生成了一个随机数?
public class Test1 {
public static void main(String[] args) {
final int MIN_NUM = 1;
final int MAX_NUM = 5;
int count = 0;
int randomNum = getRandomNum(MIN_NUM, MAX_NUM);
int guess = getUserGuess();
boolean determineMatch = isGuessCorrect(randomNum, guess);
if (determineMatch == true) {
count++;
}
displayResult(count);
}
public static int getRandomNum(int MIN_NUM, int MAX_NUM) {
return (int)(Math.random() * MAX_NUM) + MIN_NUM;
}
public static int getUserGuess() {
final int MIN_GUESS = 1;
final int MAX_GUESS = 5;
int x = 0;
int[] guess = new int[MAX_GUESS];
for (int i = 0; i < guess.length; i++) {
do {
try {
guess[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 1 and 5:"));
}
catch (NumberFormatException e) {
guess[i] = 0;
}
if (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS) {
JOptionPane.showMessageDialog(null, "ERROR! Enter a number between 1 and 5");
}
} while (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS);
}
return guess[x];
}
public static boolean isGuessCorrect(int randomNum, int guess) {
boolean determineMatch = true;
if (randomNum == guess) {
determineMatch = true;
}
else {
determineMatch = false;
}
return determineMatch;
}
public static void displayResult(int count) {
if (count >= 0 || count <= 1) {
JOptionPane.showMessageDialog(null,
"Out of 5 tries, you guessed " + count + " correct.\nYou don’t have any supernatural powers. Sorry!");
}
else if (count >= 2 || count <= 3) {
JOptionPane.showMessageDialog(null,
"Out of 5 tries, you guessed " + count + " correct.\nYou might be good. Try again another time.");
}
else if (count >= 4 || count <= 5) {
JOptionPane.showMessageDialog(null,
"Out of 5 tries, you guessed " + count + " correct.\nYou’re hired! When can you start?");
}
}
}
最佳答案
你的类(class)有多个问题,首先你只生成一个随机数,其次你只匹配第一个答案。以下是修改后的版本
package com.test;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class Test1 {
public static void main(String[] args) {
final int MIN_NUM = 1;
final int MAX_NUM = 5;
int count = 0;
int[] guesses = getUserGuess();
List<Integer> randomNums = getRandomNum(MIN_NUM, MAX_NUM);
for (int i = 0; i < guesses.length; i++) {
boolean determineMatch = isGuessCorrect(randomNums, guesses[i]);
if (determineMatch == true) {
count++;
}
}
displayResult(count);
}
public static List<Integer> getRandomNum(int MIN_NUM, int MAX_NUM) {
List<Integer> randomNums = new ArrayList<Integer>();
for (int i = MIN_NUM; i <= MAX_NUM; i++) {
randomNums.add((int) (Math.random() * MAX_NUM) + MIN_NUM);
}
return randomNums;
}
public static int[] getUserGuess() {
final int MIN_GUESS = 1;
final int MAX_GUESS = 5;
int[] guess = new int[MAX_GUESS];
for (int i = 0; i < guess.length; i++) {
do {
try {
guess[i] = Integer
.parseInt(JOptionPane
.showInputDialog("Enter a number between 1 and 5:"));
}
catch (NumberFormatException e) {
guess[i] = 0;
}
if (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS) {
JOptionPane.showMessageDialog(null,
"ERROR! Enter a number between 1 and 5");
}
} while (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS);
}
return guess;
}
public static boolean isGuessCorrect(List<Integer> randomNums, int guess) {
boolean determineMatch = true;
if (randomNums.contains(guess)) {
determineMatch = true;
} else {
determineMatch = false;
}
return determineMatch;
}
public static void displayResult(int count) {
if (count >= 0 || count <= 1) {
JOptionPane
.showMessageDialog(
null,
"Out of 5 tries, you guessed "
+ count
+ " correct.\nYou don’t have any supernatural powers. Sorry!");
} else if (count >= 2 || count <= 3) {
JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
+ count
+ " correct.\nYou might be good. Try again another time.");
} else if (count >= 4 || count <= 5) {
JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
+ count + " correct.\nYou’re hired! When can you start?");
}
}
}
修改后的版本如下:
package com.test;
import javax.swing.JOptionPane;
public class Test1 {
public static void main(String[] args) {
final int MIN_NUM = 1;
final int MAX_NUM = 5;
int count = 0;
int[] guesses = getUserGuess();
int randomNum = getRandomNum(MIN_NUM, MAX_NUM);
for (int i = 0; i < guesses.length; i++) {
boolean determineMatch = isGuessCorrect(randomNum, guesses[i]);
if (determineMatch == true) {
count++;
}
}
displayResult(count);
}
public static int getRandomNum(int MIN_NUM, int MAX_NUM) {
return (int) (Math.random() * MAX_NUM) + MIN_NUM;
}
public static int[] getUserGuess() {
final int MIN_GUESS = 1;
final int MAX_GUESS = 5;
int[] guess = new int[MAX_GUESS];
for (int i = 0; i < guess.length; i++) {
do {
try {
guess[i] = Integer
.parseInt(JOptionPane
.showInputDialog("Enter a number between 1 and 5:"));
}
catch (NumberFormatException e) {
guess[i] = 0;
}
if (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS) {
JOptionPane.showMessageDialog(null,
"ERROR! Enter a number between 1 and 5");
}
} while (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS);
}
return guess;
}
public static boolean isGuessCorrect(int randomNum, int guess) {
boolean determineMatch = true;
if (randomNum == guess) {
determineMatch = true;
} else {
determineMatch = false;
}
return determineMatch;
}
public static void displayResult(int count) {
if (count >= 0 || count <= 1) {
JOptionPane
.showMessageDialog(
null,
"Out of 5 tries, you guessed "
+ count
+ " correct.\nYou don’t have any supernatural powers. Sorry!");
} else if (count >= 2 || count <= 3) {
JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
+ count
+ " correct.\nYou might be good. Try again another time.");
} else if (count >= 4 || count <= 5) {
JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
+ count + " correct.\nYou’re hired! When can you start?");
}
}
}
关于java - 无法获得超过一个随机数来匹配我的五个猜测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31090227/
我使用这个 cmd 应用程序 https://github.com/tokland/youtube-upload 上传 50 个视频后,我收到此错误: [RequestError] Server re
尝试将 docker 容器提交到镜像时出现错误: [root@dockerregistry /]# docker commit da4180ab1536 dockerregistry:5000/myi
我只是想知道这样做会更好吗: if((fd = open(filename, O_RDWR)) == -1) { fprintf(stderr, "open [ %s ]\n", strerror(e
我在我的开发机器(单个笔记本)中使用 Elasticsearch 1.4.4。一切都设置为默认值,因为我从未更改过任何设置。 当我启动它时,我通常会收到以下消息: [2015-10-27 09:38:
我收到错误 Lock wait timeout exceeded;尝试重新启动事务。这是什么原因,如何解决?仅供引用:MySQL 配置文件中的 innodb_lock_wait_timeout = 1
我对 Slack 中的 block 功能有疑问。有人设法构建 3 列而不是 2 列吗? 我凭直觉尝试了以下代码,但它不起作用: { "blocks": [ {
div 中的内容超过由 css 决定的固定大小。 #fixeddiv { position: fixed; margin: auto; max-height: 300px
我想将 EditText 字段限制为 150 个字符,我可以很容易地做到这一点。但是,当用户试图超过该限制时,我还需要通过键入(即第 151 个字符)或粘贴超过 150 个字符来提醒用户。 解决这个问
我目前正在使用此代码并排记录两个窗口: ffmpeg -f gdigrab -framerate 30 -i title="" -f gdigrab -framerate 30 -i title=""
我在几个包含长字符串的单元格上使用嵌套的 SUBSTITUE 函数,并定期更新 SUBSTITUE fx,这导致我将其复制并粘贴到所有需要它的单元格中。问题是,我的 SUBSTITUTE 列表会随着时
我创建了一个标题 div,如下所示:
Here is the demo link 您怎么看,页面中只有 8 个广告可见,但我有 14 个广告。我已阅读有关广告限制的信息 here但不明白是不是我的情况?有人可以给我确切的答案,为什么我看不
我的非常简单的算法 - C 中的快速排序有问题。它非常有效(随机化大约 0.1 秒并检查列表是否已排序)但是当我想要对超过 500k 的元素进行排序时它会崩溃。不幸的是,我需要对它们进行更多排序,因为
我成功解决了一个关于 Hackerrank 的问题,它通过了所有测试用例,但我得到了一个错误,超过了时间限制。我猜如果我优化我的代码它会工作,但我想不出任何方法来使我的代码更有效率。 问题是: 对大小
你会如何用 包围下面的文字3 个反引号 ```使用 tpope 的 Vim Surround . 我所能做的就是 1 个反引号 使用 S`在视觉模式下: 最佳答案 这不是你问的,但这可以在没有环绕的情
我目前有一个模拟账户。我正在尝试为我的雇主使用 SwifType 制作 POC。我们有一个非常大的数据库,每 1 小时索引一次,并创建一个 JSON 文件。我认为与 Elastic 的集成将非常容易,
我为一个大约有 100 到 120 名成员的小型组织维护网站。 每个月,我们都会发送一封电子邮件,通知我们的成员我们将在即将举行的 session 中讨论的主题。 我正在尝试使用我们的网站为我们提供一
这个问题已经有答案了: How to automatically input an array formula as string with more than 255 characters in l
我有一个在 JBoss 6.1 中运行的 JSF 应用程序,它使用内部Tomcat Servlet 容器。 我已经通过apache commons文件上传实现了上传。我想防止上传过大的文件并设置了属性
当我尝试在 PyMySQL 上执行查询时,出现以下错误: pymysql.err.InternalError: (1205, 'Lock wait timeout exceeded; try rest
我是一名优秀的程序员,十分优秀!