Oop 如何循环代码以再次播放猜数?
代码是好的,但我试图使代码要求球员再次发挥,这是如果球员想再次发挥,然后代码继续运行。但是,它没有发生Oop 如何循环代码以再次播放猜数?,oop,Oop,代码是好的,但我试图使代码要求球员再次发挥,这是如果球员想再次发挥,然后代码继续运行。但是,它没有发生 package exercise; import javax.swing.JOptionPane; import java.util.Random; public class guessing_game { public static void main(String[] args) { Random random = new Random(); int randomNu
package exercise;
import javax.swing.JOptionPane;
import java.util.Random;
public class guessing_game {
public static void main(String[] args) {
Random random = new Random();
int randomNumber = random.nextInt(101);
boolean userCorrect = false;
String userInputString;
int userGuessedNumber;
int count = 0;
String play= "n", UserCorrect="y" ;
do {
count=count+1;
userInputString =JOptionPane.showInputDialog("Guess the number:");
userGuessedNumber =Integer.parseInt( userInputString );
if( userGuessedNumber > randomNumber) {
JOptionPane.showMessageDialog( null, "Too high, try again");
}
else if( userGuessedNumber < randomNumber) {
JOptionPane.showMessageDialog( null, "Too low, try again");
}
else {
JOptionPane.showMessageDialog( null, "Yes, you guessed the number, the guessed number is: " + randomNumber);
JOptionPane.showMessageDialog( null, "The number your guessed is " + count);
userCorrect =true;
play = JOptionPane.showInputDialog("Play again? (y/n) : ");
}
}
while( userCorrect!=true || play == UserCorrect);
}
package演习;
导入javax.swing.JOptionPane;
导入java.util.Random;
公开课猜谜游戏{
公共静态void main(字符串[]args){
随机=新随机();
int randomNumber=random.nextInt(101);
布尔userCorrect=false;
字符串userInputString;
int userGuessedNumber;
整数计数=0;
String play=“n”,UserCorrect=“y”;
做{
计数=计数+1;
userInputString=JOptionPane.showInputDialog(“猜数字:”);
userGuessedNumber=Integer.parseInt(userInputString);
如果(userGuessedNumber>randomNumber){
showMessageDialog(null,“太高,请重试”);
}
else if(userGuessedNumber
}您正在尝试比较两个
=
字符串。在大多数情况下,这将始终返回false
如果要比较两个字符串,请使用.equals(String)
函数
对于您来说,将
play==UserCorrect
更改为play.equals(UserCorrect)
当玩家再次玩游戏时如何重置随机数?您可以在赢的情况下执行此操作。