0

我想知道如何在循环中获取变量并将其带到外面。那可能吗?

这就是问题:

编写一个实现两人游戏的程序。计算机是记分员,两个玩家是人类。每轮游戏开始时,计算机从 1.0 到略小于 100.0 之间随机选择一个双精度数。每个玩家估计数字的平方根并输入估计值。估计最接近正确的玩家赢得该轮。玩家轮换每轮谁先上。游戏在指定的回合数后结束。

几轮?4 第一个玩家,登录--> Dweezle 第二个玩家,登录--> Moon Unit

83.29097831183603 的平方根是多少?Dweezle,你的猜测:9.1 Moon Unit,你的猜测:9.2 正确的平方根:9.126389116832353 Dweezle 是 0.026389116832353565 客场 Moon Unit 是 0.07361088316764608 客场 Dweezle 获胜!

87.79346957866132 的平方根是多少?Moon Unit,你的猜测:8.8 Dweezle,你的猜测:8.9 正确的平方根:9.36981694477866 Dweezle 是 0.46981694477866043 客场 Moon Unit 是 0.5698169447786601 客场 Dweezle 获胜!

67.44682032701256 的平方根是多少?Dweezle,你的猜测:8.2 Moon Unit,你的猜测:8.1 正确的平方根:8.212601313044033 Dweezle 是 0.012601313044033446 客场 Moon Unit 是 0.11260131304403309 客场 Dweezle 获胜!

71.64725527288849 的平方根是多少?Moon Unit,你的猜测:8.4 Dweezle,你的猜测:8.45 正确的平方根:8.464470170831042 Dweezle 是 0.01447017083104285 客场 Moon Unit 是 0.06447017083104178 客场 Dweezle 获胜!

---- 最终得分 ---- Dweezle: 4 Moon Unit: 0

这是我到目前为止所得到的。但是,由于某种原因,它跳过了 String firstPlayer = scan.nextLine(); 部分代码并跳转到下一个。让我再次重复我的问题,你如何在循环中取出一个变量并将其取出?非常感谢你们。

public static void main(String[] args) {
    System.out.println("This is the square root game. Only two players allowed.");

    Scanner scan = new Scanner(System.in);
    System.out.println("How many rounds are you two playing?");
    int numofRounds = scan.nextInt();

    System.out.println("First player, what is your name?");
    String firstPlayer = scan.nextLine();
    System.out.println();

    System.out.println("Second player, what is your name?");
    String secondPlayer = scan.nextLine();
    System.out.println();

    while (numofRounds > 0){
        Random rn = new Random();
        int range = 100;
        double randomNum =  rn.nextInt(range) + 1;
        System.out.println("What is the square root of " + randomNum + " ?");

        System.out.println(firstPlayer + ", your guess:");
        double firstPlayerInput = scan.nextDouble();

        System.out.println(secondPlayer + ", your guess:");
        double secondPlayerInput = scan.nextDouble();

        double sqrtRandom = java.lang.Math.sqrt(randomNum);
        System.out.println("The correct square root is: " + sqrtRandom);

        double firstDifference = java.lang.Math.abs(sqrtRandom - firstPlayerInput);
        System.out.println(firstPlayer + " is " + firstDifference + " away.");

        double secondDifference = java.lang.Math.abs(sqrtRandom - secondPlayerInput);
        System.out.println(secondPlayer + " is " + secondDifference + " away.");

        if (firstDifference < secondDifference) {
            int scoreFirst = 0;
            scoreFirst = scoreFirst + 1;                 
            System.out.println(firstPlayer + " wins! His/her score: " + scoreFirst);

        }
        if (secondDifference < firstDifference) {
            int scoreSecond = 0;
            scoreSecond = scoreSecond + 1;                
            System.out.println(secondPlayer + " wins! His/her score: " + scoreSecond);

        }
        else 
            System.out.println("This game is a tie.");

        numofRounds = numofRounds - 1;
    }
    System.out.println("---- Final Score ----");
    System.out.println(firstPlayer + ": " + scoreFirst + "" + secondPlayer + ": " + scoreSecond );

}

}

编辑 -------------------------------------------- 非常感谢所有人答案真的帮了大忙!!!!!!!

4

3 回答 3

0

您需要在循环之前声明变量。这样,它的范围就不仅限于循环内。像这样:

int scoreFirst = 0;
int scoreSecond = 0;
while (numOfRounds > 0) {
    // ...
}
于 2013-11-04T23:43:18.573 回答
0

简单的答案是在循环之前声明它们。

int n = 0;
while( true ){
    n = 1;
}
// n will retain it's value here
于 2013-11-04T23:44:06.073 回答
0

如果您在循环外声明变量,则可以在循环外访问它,例如:

double d = 0;
while(soneCondition) {
    ...
    d = 1;
    ...
}
// the change to the variable is seen here

变量的可访问性由定义它的位置决定。

于 2013-11-04T23:44:21.700 回答