I was wondering how to get a variable inside a loop and bring it outside. Is that possible?
This is the problem:
Write a program that implements a two player game. The computer is the score keeper and the two players are humans. Each round of the game starts with the computer randomly picking a double precision number from 1.0 to slightly less than 100.0. Each player estimates the square root of the number and enters the estimate. The player who's estimate is closest to correct wins the round. Players alternate who goes first in each round. Play ends after a specified number of rounds.
How many rounds? 4 First Player, sign in--> Dweezle Secnd Player, sign in--> Moon Unit
What is the square root of 83.29097831183603 ? Dweezle, your guess: 9.1 Moon Unit, your guess: 9.2 The correct square root: 9.126389116832353 Dweezle is 0.026389116832353565 away Moon Unit is 0.07361088316764608 away Dweezle wins!
What is the square root of 87.79346957866132 ? Moon Unit, your guess: 8.8 Dweezle, your guess: 8.9 The correct square root: 9.36981694477866 Dweezle is 0.46981694477866043 away Moon Unit is 0.5698169447786601 away Dweezle wins!
What is the square root of 67.44682032701256 ? Dweezle, your guess: 8.2 Moon Unit, your guess: 8.1 The correct square root: 8.212601313044033 Dweezle is 0.012601313044033446 away Moon Unit is 0.11260131304403309 away Dweezle wins!
What is the square root of 71.64725527288849 ? Moon Unit, your guess: 8.4 Dweezle, your guess: 8.45 The correct square root: 8.464470170831042 Dweezle is 0.01447017083104285 away Moon Unit is 0.06447017083104178 away Dweezle wins!
---- Final Score ---- Dweezle: 4 Moon Unit: 0
This is what I've gotten so far. However, for some reason it skips the String firstPlayer = scan.nextLine(); part of the code and jumps to the next one. Let me repeat my question again, how do you take a variable inside of a loop and bring it out of it? Thanks a lot guys.
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 -------------------------------------------- THANKS A LOT FOR ALL THE ANSWERS GUYS IT REALLY HELPED ALOT!!!!!