How to make a variable accessable to other methods - java

I am trying to build "simple" guessing game :D The idea is that the while loop will check everytime if the anwer was right and if not then user should try to guess one more time. But How can i make it so that the method random number generated by method suvaline would be accessable to both check and gameplay methods.
package AlsoGame;
import java.util.Random;
import java.util.Scanner;
public class AlsoGame {
public static void main(String[] args) {
System.out.println("Please try to guess a number in range from 1 to 10");
AlsoGame mainObject = new AlsoGame ();
int katse = mainObject.suvaline();
System.out.println(katse);
mainObject.check(katse);
mainObject.gameplay(katse);
}
public int InsertNum(){
System.out.println("Bitte, bitte enter a number");
Scanner input = new Scanner(System.in);
int inputNumber = input.nextInt();
return inputNumber;
}
public static int suvaline(){
Random number = new Random();
int x = 1 + number.nextInt(10);
return x;
}
public void gameplay(int katse){
boolean check = false;
int inputNumber = InsertNum();
while(check == false){
check = check(inputNumber);
if (check == true){
System.out.println("You guessed correctly, you win");
return;
}
else{
if (inputNumber > 10 || inputNumber <= 0 ){
System.out.println("Cant you read, you fool, I said guess a number in range from 1 to 10");
if (inputNumber > katse){
System.out.println("you guessed to high");
if (inputNumber < katse){
System.out.println("You guessed to low");
}
}
}
}
}
}
public boolean check(int randomNum){
System.out.println();
final int thresholdVal = randomNum ;
System.out.println(thresholdVal);
if (randomNum == thresholdVal){
return true;
}else{
return false;
}
}
}
I edit it and now the else statement in gameplay method is not executed :D
package AlsoGame;
import java.util.Random;
import java.util.Scanner;
public class AlsoGame {
public final int bla = suvaline();
public static void main(String[] args) {
System.out.println("Please try to guess a number in range from 1 to 10");
AlsoGame mainObject = new AlsoGame ();
mainObject.gameplay();
}
public int InsertNum(){
System.out.println("Bitte, bitte enter a number");
Scanner input = new Scanner(System.in);
int inputNumber = input.nextInt();
return inputNumber;
}
public int suvaline(){
Random number = new Random();
int x = 1 + number.nextInt(10);
return x;
}
public void gameplay(){
int katse = bla;
System.out.println(katse);
boolean check = false;
while(check == false){
int inputNumber = InsertNum();
check = check(inputNumber);
if (check == true){
System.out.println("You guessed correctly, you win");
}
else{
if (inputNumber > 10 || inputNumber <= 0 ){
System.out.println("Cant you read, you fool, I said guess a numbEr in range from 1 to 10");
if (inputNumber > katse){
System.out.println("you guessed to high");
if (inputNumber < katse){
System.out.println("You guessed to low");
}
}
}
}
}
}
public boolean check(int randomNum){
int thresholdval = bla;
if (randomNum == thresholdval){
return true;
}else{
return false;
}
}
}

I don't see why you asked the question, since you're already accessing the variable as a parameter. But to make it accessible in all methods you can do this:
public class AlsoGame {
private int katse = suvaline();
public boolean check(){
int thresholdval = bla;
if (this.katse == thresholdval){
return true;
}else{
return false;
}
}
public void some_other_method(){
//do something with this.katse
}
}

Related

How to add another level in Guess game using java that generates every attempt a new random number

Hello everyone I'm still new in java but I was trying to do this guess game that has 2 levels first one in a normal guess game the second level is that the user has 5 attempts to guess the number but each time he/she fails it generates a new random number but I can't link the 2 levels together when I pass the first level it gets me to the second but with the conditions of the first and doesn't generate a random number each attempt I hope you guys have the knowledge to let me know what kind of mistakes I made
package guessing_game;
import java.util.Random;
public class Game {
Random random = new Random();
private boolean gameOver;
private int secretNumber;
private int numberOfGuess;
public Game(int range) {
gameOver = false;
secretNumber = random.nextInt(10);
numberOfGuess = 0;
}
public boolean isGameOver() {
return gameOver;
}
public boolean guess(int n) {
if (gameOver) {
System.out.println("The game is over. "
+ "You can not guess again.");
} else if (n == secretNumber) {
System.out.println("You guessed right!"
+"let's go to next level.");
gameOver = false;
} else {
if (n < secretNumber) {
System.out.println("Your guess is too low.");
} else {
System.out.println("Your guess is too high.");
}
numberOfGuess++;
if (numberOfGuess == 5) {
System.out.println("You have used up all of your guesses.");
gameOver = true;
}
}
return gameOver;
}
public void guess2(int n){
if (gameOver) {
System.out.println("The game is over. "
+ "You can not guess again.");
} else if (n == secretNumber) {
System.out.println("You guessed right!");
gameOver = true;
} else {
if (n < secretNumber) {
System.out.println("Your guess is not right.");
secretNumber=random.nextInt(10);
} else {
System.out.println("Your guess is not right.");
secretNumber=random.nextInt(10);
}
numberOfGuess++;
if (numberOfGuess == 5) {
System.out.println("You have used up all of your guesses.");
gameOver = true;
}
}
}
}
package guessing_game;
import java.util.Scanner;
public class Guessing_game {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Game theGame = new Game(10);
while(theGame.isGameOver() == false){
System.out.println("Guess a number between 0 and 10:");
int n = input.nextInt();
theGame.guess(n);
if(theGame.guess(n)==false)
theGame.guess2(n);
}
}
}
Break the problem down into the steps you described.
You have two levels for guessing random a number.
First level: infinite guesses of a fixed random number
Second level: max 5 guesses of a random number that changes with each guess
Something like
public class Game {
public boolean play() {
Random random = new Random();
int level = 1;
int secretNumber = random.nextInt(10); // random number for all of level 1
// infinite guesses for level 1
do {
int guess = ... // get user input from keyboard
if (guess == secretNumber) {
level++;
}
} while (level == 1);
// level 2, only five guesses
for (int i = 0; i < 5; i++) {
// level 2, random number changes at each failed guess
secretNumber = random.nextInt(10);
int guess = ... // get user input from keyboard
if (guess == secretNumber) {
return true; // they won the game
}
}
return false; // they lost
}
}

Program keeps printing "keep playing" instead of reading the code

Every time I run my program, if the point limit was not met it is supposed to ask the user to keep playing, and if they choose yes then it is supposed to go back to the loop and run the code again but it is not doing that. when I enter "yes" it just prints the amount of points i currently have rather than going back to the loop.
import java.util.*;
public class Blackjack {
private int points;
private int limit;
private Scanner scan;
public Blackjack() {
scan = new Scanner(System.in);
}
/*Purpose: To print the current points and the limit.
Input: Points and Limit
Output: Points and Limit
*/
public void displayPoints() {
System.out.println("Your points:" + points + "/" + limit);
}
/*Purpose: To ask the user for the game limit.
Input: Game limit
Output: Entered limit
*/
public void startGame() {
System.out.println("Enter point limit:");
limit = scan.nextInt();
displayPoints();
}
/*Purpose: To get the roll value from rolling a dice
Input: Nothing
Output: Random dice number
*/
public int getRoll() {
Random r = new Random();
int roll = r.nextInt(6) + 1;
System.out.println("You rolled:" + roll);
return roll;
}
/*Purpose: To see if the player wants to keep playing the game,
Input: yes or no
Output: User's response.
*/
public boolean askUser(boolean firstTime) {
if (firstTime == true)
System.out.println("Start Playing?");
else {
System.out.println("Keep playing?");
}
scan.next();
return firstTime;
}
/* Purpose: to display the result of points in the game.
Input: No input.
Output: amount of points in the game.
*/
public void displayResult() {
if (points == limit)
System.out.println("BlackJack!");
else if (points > limit)
System.out.println("Bust!");
else if (points < limit)
System.out.println("Stand at " + points + " points!");
}
/*Purpose: to play all methods
Input: none.
Output: Game results.
*/
public void play() {
boolean gameOver = false;
startGame();
askUser(true);
String response = "";
int roll = getRoll();
while (response.equals("yes") && gameOver == false)
getRoll();
points = points + roll;
displayPoints();
if (points >= limit)
gameOver = true;
else {
askUser(false);
}
displayResult();
}
public static void main(String[] args) {
Blackjack practice = new Blackjack();
practice.play();
}
}
You didn't get response when user type.
I think you can change your askUser method like below code.
public String askUser(boolean firstTime) {
if (firstTime == true)
System.out.println("Start Playing?");
else {
System.out.println("Keep playing?");
}
String response = scan.next();
return response;
}
then change play method like :
public void play() {
boolean gameOver = false;
startGame();
String response = askUser(true);
;
;
}
I have made small changes in your code. Try this:
package stack;
import java.util.*;
public class Blackjack{
private int points;
private int limit;
private Scanner scan;
private boolean firstime = true; //new
boolean gameOver; //new
private String answer;//new
public Blackjack(){
scan = new Scanner(System.in);
}
/*Purpose: To print the current points and the limit.
Input: Points and Limit
Output: Points and Limit
*/
public void displayPoints(){
System.out.println("Your points:" + points+"/"+limit);
}
/*Purpose: To ask the user for the game limit.
Input: Game limit
Output: Entered limit
*/
public void startGame(){//Changes
if(firstime == true) {
System.out.println("Start Playing?");
answer = scan.next();
}
switch(answer) {
case "yes":
System.out.println("Enter point limit:");
limit = scan.nextInt();
int roll = getRoll();
points = points + roll;
displayResult();
break;
case "no":
goodBye();
}
}
//New method
public void goodBye() {
System.out.println("Goodbye!");
scan.close();
}
/*Purpose: To get the roll value from rolling a dice
Input: Nothing
Output: Random dice number
*/
public int getRoll(){
Random r = new Random();
int roll = r.nextInt(6)+1;
System.out.println("You rolled:" + roll);
return roll;
}
/* Purpose: to display the result of points in the game.
Input: No input.
Output: amount of points in the game.
*/
public void displayResult(){//Changes
if(points == limit) {
gameOver = true;
System.out.println("BlackJack!");
displayPoints();
System.out.println("Keep playing?");
answer = scan.next();
}
else if (points > limit) {
gameOver = true;
System.out.println("Bust!");
displayPoints();
System.out.println("Keep playing?");
answer = scan.next();
}
else if (points < limit) {
gameOver = false;
System.out.println("Stand at " + points + " points!");
}
}
/*Purpose: to play all methods
Input: none.
Output: Game results.
*/
public void play(){//Changes
startGame();
ENDWHILE:while(gameOver == true || gameOver == false) {
if(answer.equals("yes")) {
firstime = false;
while(gameOver == true) {
points = 0;
startGame();
}
while(gameOver == false) {
startGame();
}
}else {
break ENDWHILE;
}
}
goodBye();
}
public static void main(String [] args){
Blackjack practice = new Blackjack();
practice.play();
}
}
this one is working.. you have made 2 little mistakes i have commented inside the code. need to wrap while loop content inside {}. and user input needs to be return as string in askUser function. the following code is working as you wanted.
package javaapplication1;
import java.util.*;
public class JavaApplication1 {
private int points;
private int limit;
private Scanner scan;
public JavaApplication1(){
scan = new Scanner(System.in);
}
public void displayPoints(){
System.out.println("Your points:" + points+"/"+limit);
}
/*Purpose: To ask the user for the game limit.
Input: Game limit
Output: Entered limit
*/
public void startGame(){
System.out.println("Enter point limit:");
limit = scan.nextInt();
displayPoints();
}
public int getRoll(){
Random r = new Random();
int roll = r.nextInt(6)+1;
System.out.println("You rolled:" + roll);
return roll;
}
public String askUser(boolean firstTime){
if(firstTime == true)
System.out.println("Start Playing?");
else {
System.out.println("Keep playing?");
}
return scan.next();
}
public void displayResult(){
if(points == limit)
System.out.println("BlackJack!");
else if (points > limit)
System.out.println("Bust!");
else if (points < limit)
System.out.println("Stand at " + points + " points!");
}
public void play(){
boolean gameOver = false;
startGame();
String resUser = askUser(true); // add a variable to catch the input //exp-"yes"
String response = "";
int roll = getRoll();
while(resUser.equals("yes")&& gameOver == false){ // you forget the { and use //the variable to check if it is yess or not
getRoll();
points = points + roll;
displayPoints();
if(points >= limit)
gameOver =true;
else{
resUser = askUser(false);//catching the new user input
}
}// you forget the }. if you are not wrapping the loop with {}, it will only //execute one line after the loop
displayResult();
}
public static void main(String [] args){
JavaApplication1 practice = new JavaApplication1();
practice.play();
}
}

Print return value in Java

I'm currently making a high-low guessing game in Java. I believe I have two issues hindering the game from functioning properly.
Firstly, I have a while-statement inside the playGame method that doesn't change as it is now, and I have trouble understanding how I can make it change if the correct guess is made. As I understand it I can't change the value of boolean bool = true; from the giveResponse method, it has to be done from the same method playGame? If there a better way of doing this than using a while-statement?
Secondly, when the correct guess is made and the playGame method return the value of parameter guessCount to the main method it should then be printed out. The instruction I have for this game says it should be printed in the main method after the return have been made.
import java.io.Reader;
import java.util.Scanner;
public class HiLo2 {
public static void main(String[] args) {
System.out.println("Decide a level of difficulty\r"
+ "1. 1-10 \r"
+ "2. 1-100 \r"
+ "3. 1-1000");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
if (n==1) {
playGame(10);
}
else if (n==2) {
playGame(100);
}
else if (n==3) {
playGame(1000);
}
else {
System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
}
}
public static int playGame(int maxNumber) {
int answer = (int)(Math.random() * maxNumber) +1;
int guessCount = 0;
boolean bool = true;
Scanner reader = new Scanner(System.in);
System.out.println("Guess a number between 1 and " + maxNumber);
while (bool) {
int guess = reader.nextInt();
guessCount = guessCount + 1;
giveResponse(answer, guess);
}
return guessCount;
}
public static void giveResponse(int answer, int guess) {
if (guess == answer) {
System.out.println("Your guess is correct!\r");
}
else if (guess > answer) {
System.out.println("Your guess is too high, guess again:\r");
}
else if (guess < answer) {
System.out.println("Your guess is too low, guess again:\r");
}
}
}
I hope the questions are clear and specific enough. Any pointers in the right direction is appreciated.
You could have the giveResponse method return a boolean
static boolean giveResponse(...)
and put return false; inside the if (guess == answer){ case, and return true at the end of the method. Instead of calling giveResponse as a void method, use bool = giveResponse(answer,guess).
And for the second question, add a int numberOfGuesses in the main method before the if statement, and call playGame with numberOfGuesses = playGame(...), then System.out.println("You guessed " + numberOfGuesses + " times."); after the if-statements.
I think this is what you're trying to do.
public class HiLo2 {
public static void main(String[] args) {
System.out.println("Decide a level of difficulty\r"
+ "1. 1-10 \r"
+ "2. 1-100 \r"
+ "3. 1-1000");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
if (n==1) {
System.out.println("Guess count: "+playGame(10));
}
else if (n==2) {
System.out.println("Guess count: "+playGame(100));
}
else if (n==3) {
System.out.println("Guess count: "+playGame(1000));
}
else {
System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
}
}
public static int playGame(int maxNumber) {
int answer = (int)(Math.random() * maxNumber) +1;
int guessCount = 0;
boolean bool = false;
Scanner reader = new Scanner(System.in);
System.out.println("Guess a number between 1 and " + maxNumber);
while (! bool) {
int guess = reader.nextInt();
guessCount = guessCount + 1;
bool = giveResponse(answer, guess);
}
return guessCount;
}
public static boolean giveResponse(int answer, int guess) {
if (guess == answer) {
System.out.println("Your guess is correct!\r");
return true;
}
else if (guess > answer) {
System.out.println("Your guess is too high, guess again:\r");
return false;
}
else if (guess < answer) {
System.out.println("Your guess is too low, guess again:\r");
return false;
}
}
}
Here is your complete solution
import java.io.Reader;
import java.util.Scanner;
public class HereSOl {
public static void main(String[] args) {
System.out.println("Decide a level of difficulty\r" + "1. 1-10 \r" + "2. 1-100 \r" + "3. 1-1000");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
if (n == 1) {
System.out.println("Guesstcount : " + playGame(10));
} else if (n == 2) {
System.out.println("Guesstcount : " + playGame(100));
} else if (n == 3) {
System.out.println("Guesstcount : " + playGame(1000));
} else {
System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
}
}
public static int playGame(int maxNumber) {
int answer = (int) (Math.random() * maxNumber) + 1;
int guessCount = 0;
boolean bool = true;
Scanner reader = new Scanner(System.in);
System.out.println("Guess a number between 1 and " + maxNumber);
while (bool) {
int guess = reader.nextInt();
guessCount = guessCount + 1;
boolean ans =giveResponse(answer, guess);
if (ans == true) {
break;
}
}
return guessCount;
}
public static boolean giveResponse(int answer, int guess) {
if (guess == answer) {
System.out.println("Your guess is correct!\r");
return true;
} else if (guess > answer) {
System.out.println("Your guess is too high, guess again:\r");
return false;
} else if (guess < answer) {
System.out.println("Your guess is too low, guess again:\r");
return false;
}
return false;
}
}
I think you should use enum, Here is my Solution.
import java.util.Scanner;
public class HiLo2 {
public static void main(String[] args) {
System.out.println("Decide a level of difficulty\r" + "1. 1-10 \r" + "2. 1-100 \r" + "3. 1-1000");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
if (n == 1) {
playGame(10);
} else if (n == 2) {
playGame(100);
} else if (n == 3) {
playGame(1000);
} else {
System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
}
}
public static int playGame(int maxNumber) {
int answer = (int) (Math.random() * maxNumber) + 1;
int guessCount = 0;
boolean bool = true;
Scanner reader = new Scanner(System.in);
System.out.println("Guess a number between 1 and " + maxNumber);
while (bool) {
int guess = reader.nextInt();
guessCount = guessCount + 1;
Answer ans = giveResponse(answer, guess);
System.out.println(ans.getMessage());
if (ans == Answer.CORRECT) {
bool = false;
System.out.println("You took " + guessCount + " guess to get right number.");
}
}
return guessCount;
}
public static Answer giveResponse(int answer, int guess) {
if (guess == answer) {
return Answer.CORRECT;
} else if (guess > answer) {
return Answer.HIGH;
} else {
return Answer.LOW;
}
}
private enum Answer {
CORRECT("Your guess is correct!\r"), HIGH("Your guess is too high, guess again:\r"), LOW("Your guess is too low, guess again:\r");
private String message;
public String getMessage() {
return this.message;
}
Answer(String message) {
this.message = message;
}
}
}

Java classes and calling variables in other classes

I'm still fairly new in java programming and I've gotten some aspects down but the classes within Java are by far giving me the most trouble. What I'm trying to do is make a random number game where the player has to pick a number 1 through 10 and if it's wrong then try again and have the program record how many times they guessed (but not add to the number of guess when a number has been picked previously or if the number that was picked is outside the specified range) I have already worked out the logic code and was trying to make a class specifically for just the logic and a class that is specifically just for the I/O interface. But I'm having one heck of a time. Any input or tips will be very appreciated and I will provide the code that I already have below:
This is the Logic class where I want it to handle all the logic
package guessapp;
import java.util.HashSet;
import java.util.Scanner;
public class GuessLogic {
public static int Logic() {
HashSet<Integer> hs = new HashSet<>();
int GuessLogic = (int) (Math.random() * 10 + 1);
Scanner keyboard = new Scanner(System.in);
int A;
int guess;
int NumGuess = 1;
do {
guess = keyboard.nextInt();
if (hs.contains(guess)) {
A = 1;
return A;
}
if (guess < 0 || guess > 10) {
A = 2;
return A;
}
if (guess == GuessLogic) {
A = 3;
return A; // this will stop the loop
} else if (guess < GuessLogic) {
NumGuess++;
A = 4;
return A;
} else if (guess > GuessLogic) {
NumGuess++;
A = 5;
return A;
}
hs.add(guess);
} while (true);
}
public static int getGuess() {
int guess;
Scanner keyboard = new Scanner(System.in);
guess = keyboard.nextInt();
return guess;
}
}
And this is the class I want to handle all I/O interface
import java.util.HashSet;
import java.util.Scanner;
public class GuessApp {
public static void main(String[] args) {
int r, w, y;
r = GuessLogic.Logic();
w = GuessLogic.getGuess();
int NumGuess;
NumGuess = 2;
System.out.print("Enter a guess: ");
if (r == 1) {
System.out.println("You have already entered this number");
}
if (r == 2) {
System.out.println("Your guess is out of the specified range. Please try again.");
}
System.out.println("Your guess is " + w);
if (r == 3) {
System.out.println("You got it right!! Congrats!! Total Number of Guesses: " + NumGuess);
} else if (r == 4) {
System.out.println("You are wrong!!! Hint: Guess Higher, Guess number: " + NumGuess);
} else if (r == 5) {
System.out.println("You are wrong!!! Hint: Guess Lower, Guess number: " + NumGuess);
}
}
}
Below is the modified codes. There are some general ideas:
GuessLogic should be used as an instance rather than a static class. Because you need GuessLogic to save the operations and the target number.
The while loop should be coded in main. Because GuessLogic is responsible for logic only.
The elements is Set is unique, so there is no need to count how many different number by yourself.
GuessApp:
public class GuessApp {
public static void main(String[] args) {
int r, w, y;
GuessLogic guessLogic = new GuessLogic();
while(true){
System.out.print("Enter a guess: ");
w = guessLogic.getGuess();
r = guessLogic.Logic();
if (r == 1) {
System.out.println("You have already entered this number");
continue;
}
if (r == 2) {
System.out.println("Your guess is out of the specified range. Please try again.");
continue;
}
System.out.println("Your guess is " + w);
if (r == 3) {
System.out.println("You got it right!! Congrats!! Total Number of Guesses: " + guessLogic.getNumber());
break;
} else if (r == 4) {
System.out.println("You are wrong!!! Hint: Guess Higher, Guess number: " + guessLogic.getNumber());
} else if (r == 5) {
System.out.println("You are wrong!!! Hint: Guess Lower, Guess number: " + guessLogic.getNumber());
}
}
}
}
GuessLogic:
public class GuessLogic {
HashSet<Integer> hs = new HashSet<>();
int number = (int) (Math.random() * 10 + 1);
public int getNumber(){
return hs.size();
}
public int Logic(int guess) {
if (hs.contains(guess)) {
return 1;
}
if (guess < 0 || guess > 10) {
return 2;
}
if (guess == number) {
return 3; // this will stop the loop
} else if (guess < number) {
// just add to the set. The set will guarantee that there is no repetitive item.
hs.add(guess);
return 4;
} else if (guess > number) {
hs.add(guess);
return 5;
}
return -1;
}
public int getGuess() {
int guess;
Scanner keyboard = new Scanner(System.in);
guess = keyboard.nextInt();
return guess;
}
}

Running loop on Java method

So I have this assignment for class about doing something that implemented methods. I made this really simple program to give you a lucky number of the day. But for some reason, I couldn't get it to run properly more than twice.
The version I'm posting doesn't contain the loop, but I tried about 10 different ways and it just wouldn't work. Either it would keep spitting out numbers endlessly, or it would print out the welcome lines again instead of just the "would you like another number y/n" line. If someone could just help me figured out how I should have organized it so that the loop only displays this line:
Would you like to receive another number? y/n
and then if the user decides yes, the intro method runs again and that line displays again until uses presses "n"
here's the code:
import java.util.Scanner;
import java.math.BigInteger;
import java.util.Random;
public class MinOppgave2 {
public static void main(String[] args) {
menu();
}
public static void menu(){
//intro text
System.out.println("Welcome to lucky number of the day!");
System.out.println("What kind of number would you like today?");
intro();
Scanner input = new Scanner(System.in);
System.out.println("Would you like to receive another number? y/n");
String txtinput = input.nextLine();
if (txtinput.equalsIgnoreCase("y")){
intro();
}
else if (txtinput.equalsIgnoreCase("n")){
System.out.println("That's all for now, have a nice day!");
}
System.out.println("That's all for now, have a nice day!");
}
public static void intro(){
// user choice
Scanner input = new Scanner(System.in);
System.out.println("Please choose between: even odd or prime");
String text1 = input.nextLine();
//if/else user choice arguments
if (text1.equalsIgnoreCase("even"))
Evennum();
else if (text1.equalsIgnoreCase("odd"))
Oddnum();
else if (text1.equalsIgnoreCase("prime"))
Prime();
else
menu();
}
public static void Evennum(){
// random number generator
int num = 0;
Random rand = new Random();
num = rand.nextInt(1000) + 1;
while (!isEven(num)) {
num = rand.nextInt(1000) + 1;
}
System.out.println(num);
}
public static void Oddnum(){
// random number generator
int num = 0;
Random rand = new Random();
num = rand.nextInt(1000) + 1;
while (!isOdd(num)) {
num = rand.nextInt(1000) + 1;
}
System.out.println(num);
}
public static void Prime(){
// random number generator
int num = 0;
Random rand = new Random();
num = rand.nextInt(1000) + 1;
while (!isPrime(num)) {
num = rand.nextInt(1000) + 1;
}
System.out.println(num);
}
// prime checker
private static boolean isPrime(int numin){
if (numin <= 3 || numin % 2 == 0)
return numin == 2 || numin == 3;
int divisor = 3;
while ((divisor <= Math.sqrt(numin)) && (numin % divisor != 0))
divisor += 2;
//true/false prime answer
return numin % divisor != 0;
}
private static boolean isEven(int numin){
//math argument for even number
return (numin % 2) == 0;
}
private static boolean isOdd(int numin){
//math argument for even number
return (numin % 2) == 1;
}
}
Wrong recursion on the wrong place...
Try this:
public static void intro() {
System.out.println("Welcome to lucky number of the day!");
System.out.println("What kind of number would you like today?");
}
public static String takeInput() {
Scanner input = new Scanner(System.in);
return input.nextLine();
}
public static boolean pickNumber() {
System.out.println("Would you like to receive another number? y/n");
if (!takeInput().equalsIgnoreCase("y")) {
System.out.println("That's all for now, have a nice day!");
return false;
}
System.out.println("Please choose between: even odd or prime");
String chosen = takeInput();
//if/else user choice arguments
if (chosen.equalsIgnoreCase("even"))
Evennum();
else
if (chosen.equalsIgnoreCase("odd"))
Oddnum();
else
if (chosen.equalsIgnoreCase("prime"))
Prime();
return true;
}
public static void main(String[] args) {
intro();
while (pickNumber())
;
}

Categories