Hello, I have to write a Java program that basically asks the user to guess a nu
ID: 658397 • Letter: H
Question
Hello, I have to write a Java program that basically asks the user to guess a number, and tells them if the correct number is higher, or lower, then afterwards, prints the results. I am having difficulty getting the results to carry through, as they all end up zero. Normally to get this to work I would put
private static in (value here) = (whatever) outside of the main. However, I am not allowed to do this and instead have to print it within the main. Here is my program, would anyone be able to dirrect me in how to figure this out?
8 import java.util.*;
9
10 public class GuessingGame{
11
12 public static void main(String[] args){
13
16
17 Scanner console = new Scanner(System.in);
18 String playAgain = " ";
19
20
21 do {
22
23 //playing the game
24 int numGuesses = playGame(console);
25 int numOfGames = 0;
26 int totGuesses=0;
27
28 //update the stattics
29 totGuesses += numGuesses;
30 numOfGames++;
31
32 //asks user if they want to play again and asks for input
33 System.out.print("Do you want to play again? ");
34
35 playAgain = console.next().toLowerCase();
36
37 }
38
39 //this ensures that any answer entered starting with y will play game again
40 while(playAgain.charAt(0)== 'y');
41
42 //calling the method to display the statistics of the game
43 displayStatistics();
44
45 }
46
59
60 //after game is completed this method prints the statisitcs of the game
61 //based on the number of guesses
62 private static void displayStatistics(){
63
64 int numOfGames = 0; // I know this should not be here but it is the only way to make the program work
65 int totGuesses=0;
66
67 System.out.println();
68 System.out.println("Overall results: ");
69 System.out.println("Total games = " +numOfGames);
70 System.out.println("Total guesses = " +totGuesses);
71 System.out.println("Guesses/game = " +(double)totGuesses/numOfGames);
72
73
74 }
75
76 public static int playGame(Scanner console) {
77
78 int numGuesses = 0;
79
80 Random rand = new Random();
81 int number = rand.nextInt(100);
82 int guess = -1;
83
84 while(true) {
85
86 System.out.print("Your guess? ");
87 guess = console.nextInt();
88
89 //updateing statitics
90 numGuesses++;
91
92 //if user gets number correct
93 if (guess == number) {
94
95 System.out.println("You got it right in " +numGuesses+ " guesses!");
96 break;
97
98 //user guesses wrong and their guess is lower the computer gives hint
99 }else if(guess < number) {
100
101 System.out.println("It's higher.");
102
103 //user guesses wrong and their guess is higher the computer gives hint
104 }else {
105
106 System.out.println("It's lower.");
107
108 }
109 }
110
111 return numGuesses;
112 }
113
114 }
Explanation / Answer
<%@page lang="java" cont_Type="text" Encoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Cont_Type" cont="text; char_set=UTF-8">
<title>Value JSP</title>
</head>
<body>
<%
double n = Math.rand();
if (n > 0.75)
{
%>
<h1>You have luck day!</h1>
<p>(<%= num %>)</p>
<%
}
else
{
%>
<h2>Well, we have to do the work on time... </h1>
<p>(<%= num %>)</p>
<%
}
%>
<a href="<%= req.get_ReqURI() %>"><h3>Do it Again</h3></a>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.