Java:在达到一定值后重新启动程序
我正在做一个游戏,允许用户猜测随机数。如果用户猜对了,并且他/她决定再次播放,则必须生成一个新号码。我假设,如果我在开始时将布尔值“restart”设置为“true”(之后很快将其设置为false),然后在“restart”再次变为true后将其设置为true,那么程序将重新启动……事实并非如此。你知道我做错了什么吗?谢谢Java:在达到一定值后重新启动程序,java,loops,boolean,Java,Loops,Boolean,我正在做一个游戏,允许用户猜测随机数。如果用户猜对了,并且他/她决定再次播放,则必须生成一个新号码。我假设,如果我在开始时将布尔值“restart”设置为“true”(之后很快将其设置为false),然后在“restart”再次变为true后将其设置为true,那么程序将重新启动……事实并非如此。你知道我做错了什么吗?谢谢 import java.util.*; import javax.swing.*; import java.util.Scanner; public cla
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);
}}
import java.util.*;
导入javax.swing.*;
导入java.util.Scanner;
公开课2
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
字符串userNum;
int userInput,numInt,genNum,amount=0;
int repeatPlay=0;
int x=1;
布尔numTruth,重新启动;
布尔值repeatIsYes=false;
重启=真;
userInput=JOptionPane.showConfirmDialog(null),“您想试试猜魔术吗?”
数字?,“猜谜游戏”,JOptionPane.YES\u NO\u选项);
genNum=(1+(int)(Math.random()*1000));
做
if((numTruth=(userInput==JOptionPane.YES_选项))| |(repeatIsYes=(userInput==
JOptionPane.YES(可选)
重新启动=错误;
{
userNum=JOptionPane.showInputDialog(null,“输入您认为介于1和
1000: ");
numInt=Integer.parseInt(userNum);
如果((numInt>1000)| |(numInt<1)){
showMessageDialog(null,“输入不正确”);
repeatIsYes=true;
}
else if(numInt>genNum){
repeatIsYes=true;
repeatPlay=JOptionPane.showConfirmDialog(null,“你猜得太高了!”
+“\n您想再猜一次吗?”+genNum);
}
else if(numInt
您从未真正使用过重新启动
,也就是说您只设置了它。要使应用程序再次启动,请在循环中的适当位置检查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
}
谢谢你的帮助。从这一点开始,我将搜索资源来澄清我的代码,以便加强格式和结构。