我在随机数猜测游戏中遇到了while循环

时间:2016-03-17 10:55:08

标签: java if-statement while-loop do-while

希望它循环播放所有内容,直到用户输入正确的数字。当用户输错号码时,应该说"键入不同的号码"。当用户输入正确的号码时,它应该说"恭喜你赢了#34;。但在此之前,它会循环并说出#34;键入不同的数字"经过5次尝试,我希望它说'#34;你没能完成这个任务!你想再试一次吗?"

如果他们在1次尝试时猜测他们会给500美元,第二次尝试400美元等等,直到5次尝试。

import javax.swing.*;
import java.util.Random;

public class Projekt_1 {

    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "WELCOME TO GUESS GAME!" + "\nYou gonna guess a number between 1 and 20 " + "\nYou have  5 tries to guess the number! " + "\nYou gonna get more money on less tries, highest win are 500 dollar on one try! " + "\nHOPE YOU LIKE IT :)");

        Random talet = new Random();
        int secretnumber = talet.nextInt(20) + 1;
        int tries = 0;
        int money = 600;
        String number;

        int guess;
        boolean win = false;

        while (win == false) {
            number = JOptionPane.showInputDialog("Guess a number between 1 and 20");
            guess = Integer.parseInt(number);
            tries++;

            if (guess == secretnumber) {
                win = true;
            } else if (guess > secretnumber) {
                JOptionPane.showInputDialog("Your number is to low :(" + "\nType in a higher number!");
                guess = Integer.parseInt(number);
            } else if (guess < secretnumber) {
                JOptionPane.showInputDialog("Your number is to high :(" + "\nType ina a lower number!");
                guess= Integer.parseInt(number);
            }
        }

        JOptionPane.showMessageDialog(null, "Congrats you won!" + "\nYour number was " + secretnumber + "\nit took you " + tries + "tries");
    }
}   

3 个答案:

答案 0 :(得分:1)

<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Submit</title> <script> var url = 1; function setURL(url){ var win1 = "http://localhost/Audio/src/submit1.html" ; var win2 = "http://localhost/Audio/src/newPage.html"; if (url === 1){ document.getElementById('iframe').src=win2; url=2; } else{ document.getElementById('iframe').src=win1; url=1; } } </script> </head> <body> <iframe src="http://localhost/Audio/src/audio.html" style="width:0;height:0;border:0; border:none;"></iframe> <iframe id="iframe" style="border:0; " src="http://localhost/Audio/src/submit1.html"> </iframe> <input type="button" value="click me 71" onclick="setURL(url)"> </body> </html> 循环中添加其他条件将起作用。如果while是最大值为försök的尝试次数(在您的情况下),则条件应为:

5

要显示消息和分数,您只需在while (win == false && försök<5) //if försök starts from 0 { //code if(win==true) break; försök++; } 循环后检查försök的值:

while

基本上你的代码应该是这样的:

if(försök==5)
{
      // display this message: "you failed this mission! do you want to try again?" 
     score=0;
}
else
{
     //display: "congrats you won"
      score=(5-försök)*100;
}

答案 1 :(得分:0)

你可以在循环时控制中断。当尝试次数超过5次或输入正确的数字时将被打破。
请参阅以下内容以供参考

Scanner s = new Scanner(System.in);
        int numOfTries = 0;
        int input;
        int correctNumn = 15;

        while (true) {

            input = s.nextInt();
            if (input != correctNumn && numOfTries <= 5) {

                numOfTries++;
                if (numOfTries == 5) {
                    System.out.println("Game Over");
                    break;
                }

                continue;
            } else if (input == correctNumn) {
                System.out.println("corect ans");
                break;
            }

        }

答案 2 :(得分:0)

如果你用你的母语发布所有内容真的很难帮助你......但这是我的答案......

您的代码的问题是在洞穴logi中,您从不使用变量尝试(försök),如果用户猜错了数字,那么您再次阅读尝试,但您应该再次启动while的逻辑。 ..

反正....

实施例

public static void main(String[] args) {
        final int maxTries = 5;
        JOptionPane.showMessageDialog(null, "Welcome... " + maxTries + " tries ... 500 krones ");

        final Random rnd = new Random();
        final int hemligtnummer = rnd.nextInt(20) + 1;
        int tryCounter = 0;
        final int pengar = 600;
        String nummer;

        int guess = -1;

        while (guess != hemligtnummer && tryCounter < maxTries) {
            nummer = JOptionPane.showInputDialog("...a number 1 and 20");
            guess = Integer.parseInt(nummer);
            tryCounter++;

            if (guess == hemligtnummer) {
                break;
            } else if (guess > hemligtnummer) {
                JOptionPane.showMessageDialog(null, "Try " + tryCounter + " was too big try a smaller one");
            } else if (guess < hemligtnummer) {
                JOptionPane.showMessageDialog(null, "Try " + tryCounter + "too small try a bigger one");
            }
        }
        JOptionPane.showMessageDialog(null,
                "Grattis du vann!" + "\nteh number was " + hemligtnummer + "\nDet tog dig " + tryCounter + " försök");
        JOptionPane.showMessageDialog(null, "Your price is :" + (pengar - tryCounter * 100) + " Krones");
    }