作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个允许用户猜测随机数的游戏。如果用户猜对了,并且他/她决定再次玩,则必须生成新的数字。我假设,如果我在开始时将 boolean 值“restart”设置为“true”(随后很快将其设为 false),然后在“restart”再次变为 true 后将其设置回 true,则程序将重新开始。 ..它没。你看到我做错了什么了吗?谢谢。
import java.util.*;
import javax.swing.*;
import java.util.Scanner;
public class RandomGuess2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String userNum;
int userInput, numInt, genNum, amount = 0;
int repeatPlay = 0;
int x = 1;
boolean numTruth, restart;
boolean repeatIsYes = false;
restart = true;
userInput = JOptionPane.showConfirmDialog(null, "Would you like to try and guess the magic
number?", "Guessing Game", JOptionPane.YES_NO_OPTION);
genNum = (1 + (int)(Math.random() * 1000));
do
if((numTruth = (userInput == JOptionPane.YES_OPTION)) || (repeatIsYes = (userInput ==
JOptionPane.YES_OPTION)))
restart = false;
{
userNum = JOptionPane.showInputDialog(null,"Enter the number you're thinking between 1 &
1000: ");
numInt = Integer.parseInt(userNum);
if((numInt > 1000) || (numInt < 1)) {
JOptionPane.showMessageDialog(null, "Incorrect entry");
repeatIsYes = true;
}
else if(numInt > genNum) {
repeatIsYes = true;
repeatPlay = JOptionPane.showConfirmDialog(null, "You guessed too high!"
+ "\nWould you like to guess again?" + genNum);
}
else if(numInt < genNum) {
repeatIsYes = true;
repeatPlay = JOptionPane.showConfirmDialog(null, "You guessed too low!"
+ "\nWould you like to guess again?" + genNum);
}
else if(numInt == genNum) {
repeatIsYes = false;
repeatPlay = JOptionPane.showConfirmDialog(null, "How did you do that? You guessed
+ correctly!\nWould you like to guess again?" + genNum);
if(restart = (repeatPlay == JOptionPane.YES_OPTION))
{restart = true;}
}
if(repeatIsYes = (repeatPlay == JOptionPane.YES_OPTION)) {
numTruth = true;
repeatIsYes = true;
}
else {
numTruth = false;
repeatIsYes = false;
JOptionPane.showMessageDialog(null, "Goodbye! \nYou played " + amount + " times.");
break;
}
}
else {
numTruth = false;
repeatIsYes = false;
JOptionPane.showMessageDialog(null, "Goodbye!");
break;
}
while(numTruth = true);
}}
最佳答案
您从未真正使用重新启动
,即您仅设置它。要使您的应用程序再次启动,请在循环中的适当位置检查 restart
的值。
我没有时间彻底阅读您的代码(顺便说一句,由于格式和结构的原因,很难阅读您的代码),但一个简单的解决方案是使用两个循环。
这是一些伪代码:
while( playAnotherGame) { //this could be your "restart" flag
boolean gameIsRunning = true;
while( gameIsRunning ) {
//ask for user input
//check guesses
if( guessedCorrectly ) {
gameIsRunning = false; //game is finished
//display result
//ask for user input: restart or quit
if( quit ) {
playAnotherGame = false;
}
}
else {
//ask for user input: guess again, new game or quit
//handle user input
}
}
}
顺便说一句,这样的结构很容易出错并且难以阅读:
//you set and check repeatIsYes at the same time
if(repeatIsYes = (repeatPlay == JOptionPane.YES_OPTION)) {
numTruth = true;
repeatIsYes = true; //this is already true, see the if-condition
}
关于Java:达到一定值后重新启动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23490114/
我正在尝试开发右边框/Angular 具有特定 Angular (30°) 的表格。我见过一些类似的解决方案,但它们都无法在一定程度上发挥作用。如果我想从 30° 改变到 20°,我不想花太多力气。
我是一名优秀的程序员,十分优秀!