Java while循环在一次迭代后停止

时间:2012-10-22 20:41:09

标签: java loops while-loop

我正在尝试创建一个岩石,纸张,剪刀,蜥蜴,spock程序,它会重复,直到用户选择退出。但是,我无法弄清楚如何使程序循环。它在一场比赛后继续存在。我认为这可能与我的while语句的设置方式有关,但我不完全确定如何解决它。 感谢。

import java.util.Scanner;
import java.util.Random;

public class project2_rpsls {

public static void main(String[] args) 
{
    int round=0;
    String uMove = null, cMove = null/*, loop*/;
    Scanner scan=new Scanner(System.in);
    /*System.out.println("Would you like to play?");
    loop=scan.next();
    loop=loop.toUpperCase();
    while (loop.equals("Y"))*/

        while (round>=0) 
        {
            round++;
        }
        //get the moves
        {
        uMove = getuMove();
        cMove = getcMove();
        }
        //Determining the winner
        {
            String winner;
                if (cMove.equals(uMove))
                        {
                        winner="It was a tie!";
                        }
                else if (uMove.equals("ROCK") && cMove.equals("LIZARD") ||
                        uMove.equals ("ROCK") && cMove.equals("SCISSORS"))
                        {
                        winner="You Win!";
                        }
                else if (uMove.equals("PAPER") && cMove.equals("ROCK") ||
                        uMove.equals ("PAPER") && cMove.equals("SPOCK"))
                        {
                        winner="You Win!";
                        }
                else if (uMove.equals("SCISSORS") && cMove.equals("PAPER") ||
                         uMove.equals ("SCISSORS") && cMove.equals("LIZARD"))
                        {
                        winner="You Win!";
                        }
                else if (uMove.equals("LIZARD") && cMove.equals("SPOCK") ||
                         uMove.equals ("LIZARD") && cMove.equals("PAPER"))
                        {
                        winner="You Win!";
                        }
                else if (uMove.equals("SPOCK") && cMove.equals("SCISSORS") ||
                         uMove.equals ("SPOCK") && cMove.equals("ROCK"))
                        {
                        winner="You Win!";
                        }
                else 
                        {
                        winner="You Lose!";
                        }
            System.out.println("The computer picked " + cMove + " -- " + winner);
            }

            scan.close();
}


//Get user's move
private static String getuMove() 

{
Scanner scan=new Scanner(System.in);
    String uMove="test";
    while (!uMove.equals("ROCK") &&
            !uMove.equals ("PAPER")&&
            !uMove.equals("SCISSORS")&&
            !uMove.equals("LIZARD")&&
            !uMove.equals("SPOCK"))
    {
        System.out.println("Please enter your move: ");
        uMove=scan.next();
        uMove=uMove.toUpperCase();  
    }
    scan.close();
    return uMove;

    }

//Get computer's move
private static String getcMove() {
String cMove;
Random cMoveInt = new Random();
int rnum;
rnum=cMoveInt.nextInt(5);

    {
        if (rnum == 1) {
            cMove = "ROCK";
        }
        else if (rnum == 2) {
            cMove = "PAPER";
        }
        else if (rnum == 3){
            cMove = "SCISSORS";
        }
        else if (rnum == 4){
            cMove = "LIZARD";
        }
        else {
            cMove = "SPOCK";
        }

    }

        return cMove;
}

}

感谢指点。在我将while循环更改为

之后
        while (round>=0) 
        {
            round++;

        //get the moves
        uMove = getuMove();
        cMove = getcMove();

        //Determining the winner
        {
            String winner;
             ...etc.

我收到以下错误。

   Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at project2_rpsls.getuMove(project2_rpsls.java:82)
at project2_rpsls.main(project2_rpsls.java:22)

从我的研究来看,似乎我的扫描仪没有找到任何东西可读,但我不知道如何,因为它应该是在阅读用户输入。

1 个答案:

答案 0 :(得分:7)

while (round >= 0)
{
    round++;
}

... 没有但是增加round变量,直到它回绕到零(大约40亿次循环之后)。然后其余的代码运行。将剩下的代码移动到循环中。

例如:

while (round >= 0)
{
    round++;

    uMove = getuMove();
    cMove = getcMove();

    String winner;

    // ...etc.
}

另请注意,以下大括号是多余的:

   {
     uMove = getuMove();
     cMove = getcMove();
   }

这可以写成:

   uMove = getuMove();
   cMove = getcMove();