【问题标题】:Taking variables inside loop and putting it out of it?将变量放入循环中并将其排除在外?
【发布时间】:2013-11-04 23:38:32
【问题描述】:

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

这就是问题所在:

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

几轮? 4 第一个玩家,登录--> Dweezle 第二个玩家,登录-->月球单位

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

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

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

71.64725527288849 的平方根是多少? 月球单位,你的猜测: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 );

}

}

编辑 -------------------------------------------------------- 非常感谢所有回答的人,这真的很有帮助!!!!!!

【问题讨论】:

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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2022-11-20
        • 1970-01-01
        • 2020-12-18
        • 1970-01-01
        • 2011-01-25
        • 2015-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多