I'm trying to finish a project for one of my courses and I'm almost done, but having issues. For this I was able to get something to work but I'm not really sure how I should go about breaking it into the required classes.
The final project is supposed to have a GuessApp class that runs the simple guessing game using the GuessLogic (which I have as correct) which handles the logic of the game. In other words, the GuessApp class is not to keep track of the correct answer, the number of guesses made, the numbers previously guessed, or whether a guess is legal. On the other hand, the GuessApp class is responsible for all of the I/O. In other words, the GuessLogic class is not to print anything (except possibly for the purposes of debugging).
So my question is essentially how do I break up my code into these 2 classes and we were also supposed to implement a toString method in your GuessLogic class that returns the state of the GuessLogic object (that is, all of its member variables) as a single string. How would I do this?
My code thus far:
package guessapp;
import java.util.HashSet;
import java.util.Scanner;
public class GuessApp {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
HashSet<Integer> hs = new HashSet<>();
int GuessLogic = (int) (Math.random() * 10 + 1);
int guess;
int NumGuess = 1;
do {
System.out.print("Enter a guess: ");
guess = keyboard.nextInt();
if (hs.contains(guess)) {
System.out.println("You have already entered this number");
continue; // this will repeat the loop
}
if (guess < 0 || guess > 10) {
System.out.println("Your guess is out of the specified range. Please try again.");
continue; // this will repeat the loop
}
System.out.println("Your guess is " + guess);
if (guess == GuessLogic) {
System.out.println("You got it right!! Congrats!! Total Number of Guesses: " + NumGuess);
return; // this will stop the loop
}
else if (guess < GuessLogic) {
System.out.println("You are wrong!!! Hint: Guess Higher, Guess number: " + NumGuess);
NumGuess++;
}
else if (guess > GuessLogic) {
System.out.println("You are wrong!!! Hint: Guess Lower, Guess number: " + NumGuess);
NumGuess++;
}
hs.add(guess);
} while (true);
}
}
Well, do it by steps.
Create the GuessLogic class, and move to it all the data fields.
public class GuessLogic {
HashSet<Integer> hs = new HashSet<>();
int GuessLogic = (int) (Math.random() * 10 + 1);
int guess;
int NumGuess = 1;
...
Provide a method to add a guess
public void guess(int guess){
this.guess=guess;
this.NumGuess++;
validate();
}
Implement the validate method. Here you have loads of choices. You could keep an enumeration with the current state, something like
enum State {
START,
DUPLICATE,
OUT_OF_RANGE,
LOWER,
HIGHER,
MATCH
}
And validate would set the state.
Then your App queries the state and print the actual message.
Or, which would be simpler, your logic should calculate the message and just maintain a boolean shouldStop, that the app would query to know if it should prompt again or exit
the toString() method, you could just concatenate all the field values (most objects in the Java API have a meaingful toString(). Hint: a good IDE can generate the toString() automatically from the fields.
Hope this helps, and don't be afraid to try things!!
Related
I am a beginner and as you can see I made a simple Java game.
The user has 5 tries to guess a number between 1 and 20.
If the user wins a congratulations message will show.
If the user didn't succeed a game over message will pop up.
Issue
When the user enters the right answer on the 5th try both congratulations and game over messages will pop up.
Code
package org.meicode.Loops;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Welcome");
System.out.println("Enter your name please ");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println("Hello " + name);
System.out.println("Type 1 to start the game");
int yes = scanner.nextInt();
while (yes != 1) {
System.out.println("Type 1 to start the game");
yes = scanner.nextInt();
}
System.out.println("Guess the number in my mind,It is between 1 and 20 and you got 5 tries");
int timestried = 0;
Random random = new Random();
int x = random.nextInt(20) + 1;
while (timestried < 5) {
timestried++;
Scanner scanner1 = new Scanner(System.in);
int answer = scanner.nextInt();
if (x == answer) {
System.out.println("Well done, you did it");
} else if (x > answer) {
System.out.println("Try again,hint:the value is bigger than what you typed");
} else if (x < answer) {
System.out.println("Try again,hint:the value is smaller than what you typed");
}
}
System.out.println("Game over, the number was " + x);
}
}
How can I fix it?
Here is my attempt. I have added some comments in the code to help you.
Note that I have changed some of the file names to, so you may need to change them back for it to run, or just copy the main code section:
package com.misc;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
public class GameTest {
public static void main(String[] args) {
System.out.println("Welcome");
System.out.println("Enter your name please ");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println("Hello " + name);
System.out.println("Type 1 to start the game");
int yes = scanner.nextInt();
//We initialize the answer variable here to use it later on.
int answer = 0;
while (yes != 1) {
System.out.println("Type 1 to start the game");
yes = scanner.nextInt();
}
System.out.println("Guess the number in my mind,It is between 1 and 20 and you got 5 tries");
int timestried = 0;
Random random = new Random();
int x = random.nextInt(20) + 1;
//Print out the randomly generated number so we can test it. We answer wrong 4 times then put in the right answer to see if the message is fixed.
System.out.println("Testing: the answer is " + x);
while (timestried < 5) {
timestried++;
Scanner scanner1 = new Scanner(System.in);
answer = scanner.nextInt();
if (x == answer) {
System.out.println("Well done, you did it");
} else if (x > answer) {
System.out.println("Try again,hint:the value is bigger than what you typed");
} else if (x < answer) {
System.out.println("Try again,hint:the value is smaller than what you typed");
}
}
//This is the conditional that uses the answer variable we declared earlier above to avoid printing out the Game Over message in a success scenario.
if (x != answer) {
System.out.println("Game over, the number was " + x);
}
}
}
Here is proof that it works. I made the program print out the real answer, answered wrong 4 times and correctly the 5th time.
Simple fix
There are 2 things I would add to your code to achieve the desired behavior:
break or exit the loop on correct answer
set a flag signaling the question was solved to later build the message upon it
Basics: How to break loops and why
You can achieve this by two ways:
break the loop when the user typed the correct answer
add an exit-condition to the loop
return from the whole method prematurely
throw an exception that can either be caught outside or will also exit the method
I will explain (1) and (2) here in this answer (3) in a separate answer.
(1) Breaking the loop
The loop shall continue until:
the maximum number of tries has been reached
the correct answer was given
Use a break; statement to break the loop if correct answer:
if (x == answer) {
System.out.println("Well done, you did it");
break;
}
Note: contrary a continue; will skip further loop-body and jump to the next iteration.
(2) add a flag signaling premature exit (e.g. correct answer)
You can add a flag that is set to true if the user types the correct answer:
boolean userHasAnsweredCorrect = false;
while (timesTried < 5) { // here the flag can be added instead breaking
if (x == answer) {
System.out.println("Well done, you did it");
userHasAnsweredCorrect = true;
break;
}
}
// omitted some lines .. then at the end
if (userHasAnsweredCorrect) {
System.out.println("You beat the game!")
} else {
System.out.println("Game over, the number was " + x);
}
See how you define the flag before the loop, set it inside the loop (together with a break;) and then test on the flag after the loop.
Combined: set flag and add exit-condition
boolean userHasAnsweredCorrect = false;
while (timesTried < 5 && !userHasAnsweredCorrect) { // here the break happens instead
if (x == answer) {
System.out.println("Well done, you did it");
userHasAnsweredCorrect = true;
// break;
}
}
Find 2 more simpler ways of breaking the loop in my other answer, here follows the 3rd way:
Put the whole game into a method like startGame() and exit from that. Either exit after loop with max-tries has finished or inside the loop (prematurely) if answered guess was correct.
(3) Exiting the loop and method using return
That premature method-exit can be achieved by inserting a return; inside the loop.
public void startGame() {
// rest of preparation
// starting the game-loop
for (int i = 1; i <= maxTries; i++) { // for-i is indexed and safer (no infinite-loop)
// read input
// score or evaluate answer against x
if (x == answer) {
System.out.println("Well done, you did it");
return; // exit the method, not reaching "game-over" after the loop
}
// continue the iteration
}
// game-over (if not previously exited because of victory)
}
To have an exit-condition for the for loop, define int maxTries = 5 either as local variable, class field or constant.
I'm looking to repeat a "game" if it is already satisfied in my case where user has to guess the random number. I can't understand where to to get back to the main game unless i have to create another "do - while" loop inside it and retype the game again in the section where it says: System.out.println("you have tried: " + count + " times. Would you like to play again? y/n"). Is there a way to just bring back to the actual guess loop rather than create another one?
Hopefully makes sense.
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class pass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String pass = "password123";
String input;
int guess;
int count;
count = 0;
int num;
do {
System.out.print("Enter your password: ");
input = scanner.next();
} while (!input.equals(pass));
System.out.println("Correct! Now play the guess game! Guess a number between 1 - 10.");
do {
num = ThreadLocalRandom.current().nextInt(1,10);
guess = scanner.nextInt();
count++;
if (guess == num) {
System.out.println(" Well done!");
**System.out.println("you have tried: " + count + " times. Would you like to play again? y/n");**
}
else if (guess < num) {
System.out.println("your number is smaller than the number given");
}
else {
System.out.println("your guess is too high");
}
} while (guess != num);
}
}
The simplest solution would be to move the entire "guess loop" into a separate method. Then in the case when you want it to repeat, just call the method recursively.
If you want to reuse code you can make functions (or methods here, because we are inside a class). They can be used to encapsulate code and call it from anywhere to use it.
You can define a methods like that:
public static void methodName() {
// code go here
}
Then, you can call it from anywhere like that :
pass.methodName(); // It will execute the code inside methodName()
In reality, this is a lot more complex than that, you can give methods values and return others, change the scope of it to make it internal only or reachable by other classes. But I presume that you are a beginner so I keep it simple. I strongly recommend you to make a quick research about Object Oriented Programmation!
For your code, you can put the game's while loop in a method and call it at the beginning and each time the player wants to restart the game. Good luck with your game!
I manage to do this way. It seems working but one thing is letting me down at the very last when I key in "n" or other key than "y". Exception in thread "main" java.util.InputMismatchException. Is there a more softer way to finish it?
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class pass {
public static void randomnum(){
Scanner scanner = new Scanner(System.in);
int guess;
int count;
count = 0;
int num;
do {
num = ThreadLocalRandom.current().nextInt(1,10);
guess = scanner.nextInt();
count++;
if (guess == num) {
System.out.println(" Well done!");
System.out.println("you have tried: " + count + " times.");
String answer;
do{
System.out.println("Do you want to play again? y/n");
answer = scanner.next();
if (answer.equals("y")) {
System.out.println("let's play again");
randomnum();
System.out.println("Correct! Now play the guess game! Guess a number between 1 - 10.");
}
else {
System.out.println("you are logout!");
break;
}
}while (answer.equals("Y"));
randomnum();
}
else if (guess < num) {
System.out.println("your number is smaller than the number given");
}
else {
System.out.println("your guess is too high");
}
} while (guess != num);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String pass = "password123";
String input;
do {
System.out.print("Enter your password: ");
input = scanner.next();
} while (!input.equals(pass));
System.out.println("Correct! Now play the guess game! Guess a number between 1 - 10.");
randomnum();
}
}
I really need help with this. Im using BlueJ and it says 'might not be initialized'. How do i fix it? its correctNumber roughly line 16ish.
import java.util.Scanner;
import java.util.Random;
public class NumberGuessingGame {
public static void main(String[] args) {
Random randomNumber = new Random();
int correctNumber;
int guessTracker;
int guessLimit = 6; //the number of tries
int userInput;
Scanner in = new Scanner(System.in);
int game = 1;
boolean winTracker = false;
while (1 == game)
correctNumber = randomNumber.nextInt(1100); //computer generates a random number, max 100
userInput = 0;
guessTracker = 0;
System.out.println("Hello and welcome to this number guessing game. Please guess the number between 1 and 100 and I will help you by telling you if your guess is too high or low: ");
while (**correctNumber** != userInput && guessTracker < guessLimit){
userInput = in.nextInt();
guessTracker++;
if (userInput == correctNumber){
System.out.println("You have won the game! Your reward is a fact game: Did you know the first working camera was invented in 1816! "); //winner message, with a unlocked fact game
System.out.println("The correct number was " + correctNumber); //the correct number
System.out.println("It took a total of " + guessTracker + " guesses"); //number of guesses it took the user to guess the right number.
}
else if (userInput < correctNumber){
System.out.println("Your number is too low"); //displays that the users guess is too low
System.out.println("Please enter your next guess: "); //// user can now eneter their next guess
}
else if (userInput > correctNumber){
System.out.println("Your number is too high"); //displays that the users guess is too high
System.out.println("Please enter your next guess: "); // user can now eneter their next guess
}
if (correctNumber != userInput){
System.out.println("Sorry you have run out of guesses! The correct number was: " + correctNumber); // displays the correct number
}
}
}
}
You need to initialize correctNumber to a value.
This is not always the case, but think about this:
you call while(1 == game) which then initialized correctNumber to a random number, correctNumber = randomNumber.nextInt(1100) this would initialize correctNumber, but when the java compiler compiles your application it can't be sure that 1 == game is true. Therefore, when the compiler gets to the next loop while (**correctNumber** != userInput && guessTracker < guessLimit) your compiler sees that correctNumber has not been initialized even though it would be by the first loop.
In short, the compiler does not know whether a loop will be entered or not, therefore user3437460 is absoultely correct in saying that you need to initialize local scope variables, in this case int correctNumber = 0 will work perfectly for you.
I really need help with this. Im using BlueJ and it says 'might not be initialized'. How do i fix it?
Local scope variables need to be initialized (assigned an initial value) before use:
int correctNumber = 0
Same applies for your other variables.
I am making an addition game but the only problem I have is that the addition sign keeps showing up before the problem even though there is no number in front of it. What is the easiest way to fix this?
package addgame;
import java.util.Random;
import java.util.Scanner;
public class AddingGame {
private static Scanner console;
public static void main (String[]args) {
System.out.println("We are going to play an adding game, you have 3 tries. Type your answer.");
equation();
}
public static void equation() {
int tries = 5;
int points=0;
Random rand = new Random ();
while (tries>=3) {
int totalnums = rand.nextInt(4)+1;
int sumAns=0;
for (int i=1; i<=totalnums+1;i++) {
int nums= rand.nextInt(10)+1;
System.out.print(" + "+nums );
sumAns+=nums;
}
System.out.print(" = ");
console = new Scanner (System.in);
int ans = console.nextInt();
if(ans!=sumAns) {
tries--;
}
else if(tries>=3) {
points++;
}
if(tries<3) {
System.out.println("Game Over...");
System.out.println("Points="+points+"\nBetter luck next time!");
}
}
}
}
There are a couple issues here beyond what you posted.
First of all, don't use tries = 5 when they only have 3 tries. This is unclear. What if you or someone else has to look over this program at a later date? Would you know what "tries = 5" meant?
If you instead say something like
int triesLeft = 3;
There is far less ambiguity. And by starting on 3, your while statement is also more straightforward.
while (triesLeft > 0) {
Similarly, it is a bit unclear how many numbers you want to add. As you've noticed, it would be problematic in an addition game if the number of numbers you were adding was 0 or 1. Your solution does work. However, if you instead did this... (I've included one possible solution to your question in this block of code as well.)
int numberOfAddends = rand.nextInt(4)+2; //This assumes the maximum number of numbers you want to add is 5 (i.e. 3 + 2), and the minimum number is 2.
int sumAns = rand.nextInt(10)+1; //Now note these two lines.
System.out.print(sumAns); //this will make sure the first number is printed before the + sign
for (int i=1; i < numberOfAddends;i++) {
//the inside of this for loop can stay the same.
}
Notice that it is easier to tell what is going on now. It might be worthwhile considering if there are any ways you could make it even clearer.
Past this point, it's really just small things with your code.
while(triesLeft > 0) {
.
.
.
if(ans!=sumAns) {
tries--;
}
else { //the check you specified was only ever reachable when the condition you were checking is true. What you wrote was equivalent to else if (true).
points++;
}
}
System.out.println("Game Over..."); //You don't need a check for this when it is placed outside the while loop.
System.out.println("Points="+points+"\nBetter luck next time!");
I'm writing a java program that will count up to a number that the user inputs. The user is only allowed to input a number that is between 1-10.
For instance:
if the user entered 6 the output will be:
1 2 3 4 5 6
How do I do this with only using operators and while and if statements?
Here's my code. I've been painfully trying to figure out why my code won't work. Thanks in advance!
import java.util.Scanner;
public class loop_lab {
public static void main(String[] args) {
System.out.println("Hi user, input any number that is between 1-10");{
Scanner input = new Scanner(System.in);
int num1 = input.nextInt();
int num2 = 0;
if (1<=num1 && num1>=10);
num2=0;
while (num2 < num1)
System.out.println(""+(num2 + 1));
num2++;
}
}
}
I think the problems lies with the code-blocks (the stuff between {}). Especially look at how the while-loop behaves. What is supposed to be in the loop and what not? Also, your if-statement is empty. The ; closes the code-block that is handled by the if.
An IDE can help you detect these errors by applying syntax-formatting. The comments in your code looked like they were coming from Eclipse. Try ctrl-shift-f (or look it up in the menu). This automatically formats and indents your code, this makes it easier to detect errors in the structure.
The if has a stray trailing ; As a result, the next line is always run.
I make it a point to include even single line statements involved with conditional statements and loops inside {/}. That helps make the start & end of the code block clear. My earlier comment about the code indentation is also a factor in identifying where code block begin & end.
First, your conditional check should use an or and braces; and assign 0 to num1, as to prevent the loop from running if the user inputs anything outside the 1-10 range:
if (num1 < 1 || num1 > 10){
num1=0;
}
And you can also improve your loop:
while (num2 < num1) {
System.out.println( ""+ num2++ );
}
Also, as said by user689893, check your {} blocks.
in while loop just change
while (num2 < num1){
if(num2==0)
System.out.println((num2 + 1));
else{
num2++;
System.out.println(num2);
}
}
Try this one
import java.util.Scanner;
public class loop_lab {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hi user, input any number that is between 1-10");
Scanner input = new Scanner(System.in);
int num1 = input.nextInt();
int num2 = 1;
if (1<=num1 && num1>=10){
num2=1;
while (num2 <= num1)
{
System.out.println("" + num2);
num2++;
}
}
}
}