将变量置于循环内并将其从中取出?

时间:2013-11-04 23:38:33

标签: java variables loops for-loop while-loop

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

这是问题所在:

编写一个实现双人游戏的程序。计算机是得分守护者,两名运动员是人类。每轮游戏都从计算机开始随机选择从1.0到略低于100.0的双精度数。每个玩家估计数字的平方根并输入估计值。估计最接近正确的玩家赢得该回合。在每轮比赛中排名靠前的球员。播放在指定次数后结束。

多少回合? 4 第一位玩家,登录 - > Dweezle Secnd Player,登录 - >月亮单位

83.29097831183603的平方根是多少? Dweezle,你的猜测:9.1 Moon Unit,你的猜测:9.2 正确的平方根:9.126389116832353 Dweezle距离是0.026389116832353565 月亮单位是0.07361088316764608 Dweezle赢了!

87.79346957866132的平方根是多少? Moon Unit,你的猜测:8.8 Dweezle,你的猜测:8.9 正确的平方根:9.36981694477866 Dweezle距离是0.46981694477866043 月亮单位是0.5698169447786601 Dweezle赢了!

67.44682032701256的平方根是多少? Dweezle,你的猜测:8.2 Moon Unit,你的猜测:8.1 正确的平方根:8.212601313044033 Dweezle距离0.012601313044033446 月亮单位是0.11260131304403309 Dweezle赢了!

71.64725527288849的平方根是多少? Moon Unit,你的猜测:8.4 Dweezle,你的猜测:8.45 正确的平方根:8.464470170831042 Dweezle距离0.01447017083104285 月亮单位是0.06447017083104178 Dweezle赢了!

----最终得分---- Dweezle:4月亮单位: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 );

}

}

EDIT -------------------------------------------- 非常感谢所有的答案,他真的很乐意帮助!!!!!

3 个答案:

答案 0 :(得分:0)

您需要在循环之前声明变量。这样它的scope不限于循环内。像这样:

int scoreFirst = 0;
int scoreSecond = 0;
while (numOfRounds > 0) {
    // ...
}

答案 1 :(得分:0)

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

int n = 0;
while( true ){
    n = 1;
}
// n will retain it's value here

答案 2 :(得分:0)

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

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

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