2013-11-04 57 views
0

我想知道如何在一個循環內獲取一個變量並將其帶到外部。那可能嗎?把變量放在循環裏面並把它放出來?

這就是問題所在:

編寫實現一兩個玩家的遊戲程序。計算機是守門員,兩名球員是人類。遊戲的每一輪都是從電腦隨機選取一個從1.0到略低於100.0的雙精度數字開始。每個參與者估計數字的平方根並輸入估計值。估計最接近正確的玩家贏得一輪。在每輪比賽中首先出場的球員交替出場。遊戲在指定次數後結束。

多少回合? 4 首次登入 - > Dweezle Secnd玩家,登入 - >月球單位

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

這是我到目前爲止得到。但是,由於某種原因,它會跳過字符串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); 

} 

}

編輯------------------------------------- ------- 非常感謝所有答案的人,他真的幫助了很多!!!!!

+0

請參閱我的回答[此問題](http://stackoverflow.com/questions/19778280/if-statement-skipping-right-to-else-after-being-called-once),第一項我的列表。 – ajb

+0

對於「但是,由於某種原因,它跳過字符串firstPlayer = scan.nextLine();」請參閱http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx –

回答

0

如果聲明外循環的變量,它是循環外部訪問,例如:

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

變量的可訪問性是由定義它的位置決定的。

0

您需要在循環之前聲明該變量。這樣它的scope不限於循環內。像這樣:

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

簡單的答案是在循環之前聲明它們。

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