【问题标题】:exiting a do while loop using a string使用字符串退出 do while 循环
【发布时间】:2012-10-06 22:35:43
【问题描述】:

好的。

这是我的问题。我在做骰子游戏时遇到了一些困难。 我遇到的问题是使用 do while 循环使其退出。 这是我的代码。我将在

中进一步解释我的意思
import java.util.Scanner;

public class Dice
{
 public static void main (String[] args)
 {
    int diceGuess;
    int rollNum, roll1, roll2, roll3;
    String playAgain = "yes";

    Scanner sc = new Scanner(System.in);


     // Create two separate dice objects to create a pair of dice
            Roll die1 = new Roll();
            Roll die2 = new Roll();

    // get from the user the number of dice rolls
            System.out.print ("Please enter a number between 2 and 12 to begin: ");
            diceGuess = sc.nextInt();

    do
    {       
        // loop to show rolls
        for (rollNum = 1; rollNum <=3; rollNum++)
          {
          System.out.println("**********Roll #: " + rollNum + " ************");
            roll1 = die1.roll();
            roll2 = die2.roll();

                //if statement to display you win if you win, and to make the loop break.
                if(diceGuess==roll1+roll2)  
                    {
                        System.out.println("You win!");
                        rollNum = 4;
                    }

                if(rollNum == 3)
                    {
                        System.out.println("You lose.\n");
                        System.out.println("Would you like to play again?");
                        playAgain = sc.nextLine();
                        rollNum ++;
                    }
            }           
        }while(playAgain == "yes");

}

}

这是我的实用程序类。 我希望它询问用户是否要再次播放,然后如果用户输入否,我希望它退出。 我确信这只是我的一个小误解。
非常感谢stackoverflow。

这是另一个类

//Justin Le
//Dice class


import java.util.Random;

public class Roll
{
//variables used in class
private Random randomRoll = new Random();
private int roll;
boolean playAgain = false;


//constructor
public Roll()
{
    roll = 0;
}

//method named roll to return roll number and inside, calls a method to display the roll.
public int roll()
{
    roll = randomRoll.nextInt(6) + 1;
    showRoll(roll);  //accesses showRoll to output in driver class.
    return roll;

}

public boolean test()
{
    playAgain = !playAgain;
    return playAgain;
}


//displays picture of dice roll
private void showRoll(int r)
    {
        switch(r)
            {
            case 1:
                System.out.println("One: \n" +
                                         " \n  "   +
                                         "  *  \n" +
                                         " \n  ");
            break;

            case 2:
                System.out.println("Two: \n" +
                                         "*    \n" +
                                         "     \n" +
                                         "    *\n");
            break;

            case 3:
                System.out.println("Three:\n" +
                                         "*    \n" +
                                         "  *  \n" +
                                     "    *\n");
            break;

            case 4:
                System.out.println("Four:\n" +
                                        "*   *\n" +
                                     "     \n" +
                                     "*   *\n");
            break;

            case 5:
                System.out.println("Five:\n" +
                                        "*   *\n" +
                                         "  *  \n" +
                                         "*   *\n");
            break;

            case 6:
                System.out.println("Six: \n" +
                                        "*   *\n" +
                                         "*   *\n" +
                                         "*   *\n");
            break;
            default:
                System.out.println("error\n");
    }

}

}

【问题讨论】:

  • 必须在对象上使用 .equals(),你可以在原始类型上使用 ==

标签: java loops do-while


【解决方案1】:

在你的时候,使用equals() 方法来比较字符串.. : -

playAgain.equals("yes")

请参阅this excellent post 了解使用equals()== 的比较差异。

在您的第一个代码中:-

if(rollNum == 3)
{
    System.out.println("You lose.\n");
    System.out.println("Would you like to play again?");
    //playAgain = sc.nextLine();

    // Change your nextLine() to next()
    playAgain = sc.next();
    rollNum ++;
}

【讨论】:

    【解决方案2】:

    改变

    playAgain == "yes"
    

    "yes".equals(playAgain)
    

    【讨论】:

    • 我这样做了,但我认为其中一个问题是......当它到达我输入是或否的地步时......它只是不断给我新的行来输入 no no no不不
    【解决方案3】:

    使用 while(plyaAgain.equals("yes"));

    点击此链接了解字符串比较的详细信息: How do I compare strings in Java?

    【讨论】:

      【解决方案4】:

      改变这个:

      while(playAgain == "yes");
      

      到这里:

      while(playAgain.equals("yes"));
      

      【讨论】:

        猜你喜欢
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        • 2015-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-31
        • 1970-01-01
        相关资源
        最近更新 更多