Controlling Answer With While Loop in Java - java

I coded a fishing game simulator. I want to control the answer: If answer is not equal to "Y","y","N" or "n", Ask again : Would Like to fish? until until the user presses the correct key. I do not know how to code this algorithm and which line should I put that algorithm. My code is here:
// --------------------//
import java.util.Random;
import java.util.Scanner;
public class fishinggame {
public static void main(String[] args) {
Random dice = new Random();
Scanner scanner = new Scanner(System.in);
int randomGuess;
String choice = "Y";
int score = 0;
int points = 0;
int counter = 0;
System.out.println("Let's go fishing!!!");
System.out.println();
System.out.println("Would you like to fish?");
System.out.println("Type Y or y for yes");
System.out.println(" Type N or n for quit");
while (choice.equalsIgnoreCase("Y")) {
randomGuess = dice.nextInt(6) + 1;
if (randomGuess == 1) {
System.out.println("You caught an old shoe.");
points += 1; // If Random Guess is 1 player takes that message and wins 1 point
}
if (randomGuess == 2) {
System.out.println("You caught a huge fish!");
points += 100; // If Random Guess is 2,player takes that message and wins 100 points
}
if (randomGuess == 3) {
System.out.println("You caught a leaf ");
points += 2; // If Random Guess is 3,player takes that message and wins 2 points
}
if (randomGuess == 4) {
System.out.println("You caught a small fish ");
points += 50; // If Random Guess is 2,player takes that massage and wins 50 points
}
if (randomGuess == 5) {
System.out.println("You caught a rock ");
points += 3; // If Random Guess is 2,player takes that massage and wins 3 points
}
if (randomGuess == 6) {
System.out.println("You caught a garbage ");
points += 0; // If Random Guess is 2,player takes that massage and can not win any point
}
System.out.println("Want to try again? Y/N");
choice = scanner.nextLine();
counter++; // This structure counts repeat of game
}
if ((choice.equalsIgnoreCase("N"))) {
System.out.println("Game Over");
System.out.println();
System.out.println("Your final score is: " + (points));
System.out.println("Your game repeat is:" + counter);
double avarege = (double) points / counter; // This lane is for counting the average of points
System.out.printf("Your Average Point Is: %6.2f\n" , avarege);
if (avarege >= 20) {
System.out.println("GREAT JOB");
} else if (avarege >= 10) {
System.out.println("That is some fine fishing");
} else {
System.out.println("Try again in future!");
}
System.out.println("Thanks for playing!");
}
}
}

Solution :
import java.util.Random;
import java.util.Scanner;
public class fishinggame {
public static void main(String[] args) {
Random dice = new Random();
Scanner scanner = new Scanner(System.in);
int randomGuess;
String choice = "Y";
int score = 0;
int points = 0;
int counter = 0;
System.out.println("Let's go fishing!!!");
System.out.println();
System.out.println("Would you like to fish?");
System.out.println("Type Y or y for yes");
System.out.println(" Type N or n for quit");
choice = scanner.next();
while (!choice.equalsIgnoreCase("Y") && !choice.equalsIgnoreCase("n")) {
System.out.println("Would you like to fish?");
System.out.println("Type Y or y for yes");
System.out.println(" Type N or n for quit");
choice = scanner.next();
}
while (choice.equalsIgnoreCase("Y")) {
randomGuess = dice.nextInt(6) + 1;
if (randomGuess == 1) {
System.out.println("You caught an old shoe.");
points += 1; // If Random Guess is 1 player takes that message and wins 1 point
}
if (randomGuess == 2) {
System.out.println("You caught a huge fish!");
points += 100; // If Random Guess is 2,player takes that message and wins 100 points
}
if (randomGuess == 3) {
System.out.println("You caught a leaf ");
points += 2; // If Random Guess is 3,player takes that message and wins 2 points
}
if (randomGuess == 4) {
System.out.println("You caught a small fish ");
points += 50; // If Random Guess is 2,player takes that massage and wins 50 points
}
if (randomGuess == 5) {
System.out.println("You caught a rock ");
points += 3; // If Random Guess is 2,player takes that massage and wins 3 points
}
if (randomGuess == 6) {
System.out.println("You caught a garbage ");
points += 0; // If Random Guess is 2,player takes that massage and can not win any point
}
System.out.println("Want to try again? Y/N");
choice = scanner.next();
counter++; // This structure counts repeat of game
}
if ((choice.equalsIgnoreCase("N"))) {
System.out.println("Game Over");
System.out.println();
System.out.println("Your final score is: " + (points));
System.out.println("Your game repeat is:" + counter);
double avarege = (double) points / counter; // This lane is for counting the average of points
System.out.printf("Your Average Point Is: %6.2f\n" , avarege);
if (avarege >= 20) {
System.out.println("GREAT JOB");
} else if (avarege >= 10) {
System.out.println("That is some fine fishing");
} else {
System.out.println("Try again in future!");
}
System.out.println("Thanks for playing!");
}
}
}
The result :
Let's go fishing!!!
Would you like to fish?
Type Y or y for yes
Type N or n for quit
y
You caught a leaf
Want to try again? Y/N
y
You caught a rock
Want to try again? Y/N
y
You caught a small fish
Want to try again? Y/N
y
You caught a small fish
Want to try again? Y/N
n
Game Over
Your final score is: 105
Your game repeat is:4
Your Average Point Is: 26,25
GREAT JOB
Thanks for playing!

Related

Problem producing a new random number at the end of HiLo guessing game

I'm creating a HiLo guessing game in Java. Everything I have so far works as intended except at the end when I prompt a user to play again, the random number remains the same from the previous game. How do I make it so the code produces a new random number when the user chooses to play a new game?
int answer = (int)(Math.random() * 100 + 1);
int guess = 0;
int guessCount = 0;
boolean playGame = true;
String restart;
Scanner scan = new Scanner(System.in);
while(playGame == true)
{
while (playGame == true)
{
System.out.println("Enter a number between 1 and 100: ");
guess = scan.nextInt();
guessCount ++;
System.out.println(answer);
if (guess < 1 || guess > 100)
{
System.out.println("You have entered an invalid number.");
guessCount --;
} else if (guess == answer)
{
System.out.println("Correct! Great guess! It took you " + guessCount + " tries!");
break;
} else if (guess > answer)
{
System.out.println("You've guessed too high! Guess again: ");
} else if (guess < answer)
{
System.out.println("You've guessed too low! Guess again: ");
}
}
System.out.println("Would you like to play again? Y/N");
restart = scan.next();
if (restart.equalsIgnoreCase("Y"))
{
playGame = true;
} else if(restart.equalsIgnoreCase("N"))
{
System.out.println("Thank you for playing!");
break;
}
}
The value in the variable 'answer' remains same since variable is a reference to what you have stored / Initialized or Assigned. It does not manipulate in itself. You have to rewrite the code for e.g. answer = (int)(Math.random()*100+1) at the point before game will be restarted or after it.
You're initializing the answer before the loop, it never changes. You have to assign answer a new value when the user chooses to play a new round. This is not the code I'd write, but here it is:
if (restart.equalsIgnoreCase("Y"))
{
answer = (int)(Math.random() * 100 + 1);
}

Can a program repeat when a user ends the program while it is running?

This is a Hi-Lo number guessing game code, and the program ends either when the user gets the correct number or enters -1 while playing the game.
Here, I was wondering if it is possible to make the program run again even after the user enters -1 and the game ends, for example, in a situation where the user feels like restarting the game without finishing the first game.
import java.util.Random;
import java.util.Scanner;
public class HighLowGame {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
Random generator = new Random();
int number =generator.nextInt(100) + 1;
int count = 0;
boolean game = true;
System.out.println("Please guess the number.(Enter -1 to quit): ");
while(game){
int guess = scanner.nextInt();
**//if user enters -1, the game ends
if(guess == -1){
break;
}**
//guessed number is out of range
if(guess>100 || guess<0){
System.out.println("The number should be between 0 and 100.");
}
//guessed number is smaller than the random number
if(guess < number && 0 <= guess && guess <= 100 ){
count++;
System.out.println("That is too low. Please try again.(Enter -1 to quit):");
}
// guessed number is bigger than the random number
else if(guess > number && 0 <= guess && guess <= 100){
count++;
System.out.println("That is too high. Please try again.(Enter -1 to quit):");
}
//guessed number is the same as the random number
else if(guess==number) {
count++;
System.out.println("Congratulations! You got the correct number.");
System.out.println("Your attempt was " + count + " tries.");
count = 0;
System.out.println("Would you like to play the game again?(yes/no): ");
String another = scanner.next();
if (another.equalsIgnoreCase("no")) {
break;
}
// if the user wants to play the game one more time, it starts again
else {
number = generator.nextInt(100) + 1;
System.out.println("Please guess the number(Enter -1 to quit): ");
}
}
}
}
}
You can just put the game into a method, and when the method ends, you just ask them.
Main:
public static void main(String[] args){
boolean flag = true;
while(flag){
play();//play the game
Scanner input = new Scanner(System.in);//read the respound
System.out.println("want to play again?");
if(input.nextLine().equal("no"){//Want to play? BTW: I'm assuming you only enter "no" or "yes"
flag = false;//Don't want to play
}
}
}
And the method:
public static void play(){
//create scanner, variables and a random number
Scanner scanner = new Scanner (System.in);
Random generator = new Random();
int number =generator.nextInt(100) + 1;
int count = 0;
boolean game = true;
//prompt user to enter a number
System.out.println("Please guess the number.(Enter -1 to quit): ");
while(game){
//user enters a number
int guess = scanner.nextInt();
//if user enters -1, the game ends
if(guess == -1){
break;
}
//guessed number is out of range
if(guess>100 || guess<0){
System.out.println("The number should be between 0 and 100.");
}
//guessed number is smaller than the random number
if(guess < number && 0 <= guess && guess <= 100 ){
count++;
System.out.println("That is too low. Please try again.(Enter -1 to quit):");
}
// guessed number is bigger than the random number
else if(guess > number && 0 <= guess && guess <= 100){
count++;
System.out.println("That is too high. Please try again.(Enter -1 to quit):");
}
//guessed number is the same as the random number
else if(guess==number) {
count++;
//displays the message and the attempt count
System.out.println("Congratulations! You got the correct number.");
System.out.println("Your attempt was " + count + " tries.");
}
}
}
BTW: you said when the user enters -1, you end the game, then why in the world do you want to ask the user again ?_?

How do I start a different method over after another Method has finished in java?

I'm making a gambling game. The player enters a bet amount. After that a die is rolled three times, then based on the value of the die rolls, 1 of 4 things happen. Then you get to start again with the new Pot amount. I cant figure out how to start it again. I tried putting the method name i want to start at the end of the last method in the sequence and it kinda of works. It starts but when i put in a bet amount it doesnt do anything.
Here is my code
import java.util.Scanner;
public class Game {
private double Bet;
private double Pot = 50;
// private double TotalPot;
private int[] die = new int[3];
public Game() {
Pot = 50;
Bet = 0;
}
public void introText() {
System.out.println(
"Welcome to game Bet an amount.\nIf all of the three die together is greater than 12, \nyou get to keep your bet, if you roll doubles you win \ndouble your bet, if you roll triples you win triple \nyour bet. If your roll meets non of this criteria you lose your bet.A bet of 0 \nends the game.");
}
public void inputBet() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your bet: ");
Bet = keyboard.nextDouble();
if (Bet > Pot) {
System.out.println(
"Error, Bet out of range. Please enter a bet amount that is lower or the same as the current Pot. ");
inputBet();
} else if (Bet == 0) {
System.out.println("Thank you for playing. You end the game with a pot of " + Pot);
System.exit(1);
} else if (Bet < 0) {
System.out.println("Error,");
inputBet();
}
}
public void removeBet() {
Pot = Pot - Bet;
}
public void rollDie() {
Die bob = new Die();
int total = 0;
for (int i = 0; i < 3; i++) {
die[i] = bob.rollDie();
System.out.println("Your die number is listed below");
bob.displayDie(die[i]);
total = total + die[i];
}
bob.displayDie(total);
}
public void displayingDie() {
System.out.println("Pot amount before dice rolls $" + Pot);
}
public void dieComparison1() {
if ((die[1] == die[2]) && (die[0] == die[2])) {
Pot = (Bet * 3) + Pot;
System.out.println(+Pot + "if theyre all equal");
} else if ((die[0] == die[1]) || (die[0] == die[2]) || (die[1] == die[2])) {
Pot = (Bet * 2) + Pot;
System.out.println(" Congradulations! You win double your bet.");
} else if (die[0] + die[1] + die[2] > 12) {
Pot = Pot + Bet;
System.out.println("You win. But only your bet amount back");
} else if (die[0] + die[1] + die[2] < 12) {
// Pot = Pot - Bet;
System.out.println("Sorry You lose you bet amount");
}
}
public void print() {
System.out.println("The current Pot amount is: " + Pot);
inputBet();
}
}
You have a conception problem. You should not control your execution flow nor do io actions from your Game class. This is mainly a problem of separation of concerns. Each method of this class should only do actions relative to gambling. For exemple inputBet must only change bet and do controls, in fact it must only be a setter and be renamed setBet and throws IllegalArgumentException for exemple.
You can then do something like this in your main :
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
Game game = new Game();
while(true){
Double bet = keyboard.nextDouble();
if(bet == 0){
//main loop break case
break;
}
try{
game.setBet(bet);
}catch(IllegalArguementException excpt){
system.out.println("error " + excpt.getMessage());
// skip next steps and do new loop iteration
continue;
}
// rest of execution flow
game.rollDice();
game.displayDice();
game.compareDice();
game.print();
}
System.exit(0);
}
and the setBet method could be :
public void setBet(Double bet){
// Range controls
if (bet > Pot || bet < 0) {
Throw new IllegalArgumentException("Bet out of range. Please enter a bet amount that is lower or the same as the current Pot. ");
}
this.bet = bet;
}

I need advice to fix my HighLow code in java

Im fairly new to java and im having trouble getting this to loop? The code works fine, its just that after the user guesses correctly the code stops.
Here is my code:
import java.util.Random;
import java.util.Scanner;
public class Chapter3HighLow {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
Random random = new Random(); //gives random numbers each time
int correctNum = random.nextInt(100);
int NumberOfTries = 0; // tells how many guesses it took
while (true) {
System.out.println("Hi! Please enter a number between 1-100! (if you would like to quit, please press -1)");
int guess1 = input.nextInt();
NumberOfTries++; //user enters their guesses
if (guess1 == (-1)) {
break; //breaks the loop if the user enters -1
}
if(guess1 < correctNum){
System.out.println("The number inserted is too low!");
}
else if(guess1 > correctNum){
System.out.println("The number inserted is too high!");
}
else if(guess1 == correctNum){
System.out.println("The number you entered was Correct!!");
System.out.println("It took you " + NumberOfTries + " tries"); // Tells how many tries it took
}
}
}
}
I'm not entirely sure what you're asking, however, from what I can understand, you are looking to get your game to loop continuously until the user wants to stop playing. So what you are looking for is a method which gives the user a choice whether they want to play again. My suggesting is using boolean. The following code demonstrates this for your example:
import java.util.Random;
import java.util.Scanner;
public class Chapter3HighLow {
private static boolean playAgain(){
Scanner sc = new Scanner(System.in);
String usrInput = "";
System.out.println("Play again? (Y/N)");
usrInput = sc.next();
if(usrInput.equalsIgnoreCase("Y")){
return true;
}
else if(usrInput.equalsIgnoreCase("N")){
return false;
}
else{
return false;
}
}
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
Random random = new Random(); //gives random numbers each time
int correctNum = random.nextInt(100);
int NumberOfTries = 0; // tells how many guesses it took
int guess1 = 0;
do{
do{
System.out.println("Please guess a number between 1-100!");
guess1 = input.nextInt();
NumberOfTries++; //user enters their guesses
if (guess1 == (-1)) {
break; //breaks the loop if the user enters -1
}
if(guess1 < correctNum){
System.out.println("The number inserted is too low!");
}
else if(guess1 > correctNum){
System.out.println("The number inserted is too high!");
}
else if(guess1 == correctNum){
System.out.println("The number you entered was Correct!!");
System.out.println("It took you " + NumberOfTries + " tries"); // Tells how many tries it took
}
}while(guess1 != correctNum);
correctNum = random.nextInt(100);
NumberOfTries = 0;
}while(playAgain() == true);
}
}
Read more about methods here.
Read more about the boolean data type here.
Your final else appears to be missing a break. Like
else if(guess1 == correctNum){
System.out.println("The number you entered was Correct!!");
System.out.println("It took you " + NumberOfTries + " tries");
break; // <-- add this.
}
or you might make that the while condition. Something like,
int guess1 = -1;
while (guess1 != correctNum) {
System.out.println("Hi! Please enter a number between 1-100! "
+ "(if you would like to quit, please press -1)");
guess1 = input.nextInt();
if (guess1 == (-1)) {
break;
}
NumberOfTries++;
if (guess1 < correctNum) {
System.out.println("The number inserted is too low!");
} else if (guess1 > correctNum) {
System.out.println("The number inserted is too high!");
} else if (guess1 == correctNum) {
System.out.println("The number you entered was Correct!!");
System.out.println("It took you " + NumberOfTries + " tries");
}
}
Does it really stop after guessing it right ?

input of user not looping the right way, loops more than once or loops incorrectly

here is where the user inputs for picking a weapon as he wishes.
String ans = "Y";
while (ans.equalsIgnoreCase("Y"))
{
while (monster >= 0 && you >= 0)
{
System.out.println("\nPick Weapon!");
String input = keyboard.next();
Here is the piece of code i am having a problem with
else if (input.equalsIgnoreCase("energy"))
{
System.out.println("\nYou have 0.0 energy and cannot use you have to wait for 5 seconds for it to charge up half or take a hit and go ALL OUT BABY");
try
{
Thread.sleep(5000);
}catch(InterruptedException e)
{
}
System.out.println("\nSystem has charges and is ready for use, press 1");
int inputattack = keyboard.nextInt();
energy += 3000;
if (inputattack == 1)
{
String inputofretry = "Y";
if ((energy >= 3000) && (inputofretry.equalsIgnoreCase("Y")))
{
System.out.println("\nYou have used all of your energy and did a damage of 1000 to the moster!");
monster -= 1000;
energy -= 3000;
System.out.println("\nNow the monster has a health of " + monster);
----------------->System.out.println("\nWould you like to use up your other half also or use a diffrent weapon");
inputofretry = keyboard.next();
if (inputofretry.equalsIgnoreCase("N"))
{
ans = "Y";
}//end of break out of if
}//end of energy half use
else if (energy <= 0)
{
System.out.println("\nRan out of energy and you have been redirected to the refill of energy");
ans = "Y";
}
}//end of input attack
The problem is happening when the User Presses "Y" to use energy again, it doesn't go through the if again.
Main problem is that when i answer the question wrong like a input of answer to wither pick a weapon or pick a the same one (also when i say yes to the pick the same one option at the line where i have marked ---> it still breaks out of the loop instead of staying in it it) it asks me to pick a weapon 3 times instead of 1 and i don't know why and also SORRY for the long code but i would like to ask users if they could make my code any better if they can thanks i am a SUPER noob to java and i am new to stack so i am a noob to everything.
So please ask me any questions about the code if you can
thank you in advance for anyone who helped!!
WHOLE CODE
import java.util.Scanner;
public class nestedif
{
public static void main(String[] args)
{
System.out.println("\nWelcome\n");
Scanner keyboard = new Scanner(System.in);
int bombs = 60;
int arrows = 2;
boolean bow = true;
double energy = 0.0;
double monster = 3000;
int you = 1000;
System.out.println("\nMonster has appeared!!\n");
System.out.println("\nHeres your inventory");
System.out.println("\n60 bombs");
System.out.println("\n2 ultra arrows");
System.out.println("\n0.0 energy, you can recharge later if you choose to do so");
String ans = "Y";
while (ans.equalsIgnoreCase("Y"))
{
while (monster >= 0 && you >= 0)
{
System.out.println("\nPick Weapon!");
String input = keyboard.next();
if (input.equalsIgnoreCase("bombs"))
{
if (bombs >= 0)
{
System.out.println("\nYou have chosen Bombs\n");
System.out.println("\nHit 9 to attack with big bombs which is -2 bombs or hit 8 for small bomb which is -1\n");
int choice1 = keyboard.nextInt();
if (choice1 == 9)
{
System.out.println("\nYou have chosen to attack with a big bomb 2 bombs ahve been taken from your inventory");
System.out.println("\nYou damaged the monster by 800\n");
monster -= 800;
bombs -= 2;
System.out.println("Now the monster has the health of" + monster + "\n");
System.out.println("\nHit Y to choose another weapon or N to back out of the fight");
ans = keyboard.next();
}//if end
else if (choice1 == 8)
{
System.out.println("\nYou have chosen to attack with a small bomb");
System.out.println("\nYou damaged the monster by 400\n");
monster -= 400;
bombs -= 1;
System.out.println("Now the monster has the health of" + monster + "\n");
System.out.println("\nHit Y to choose another weapon or N to back out of the fight");
ans = keyboard.next();
}//else if end of small bombs
}//end of if loop to do something i dont know i just leant this
else
{
System.out.println("\nYour bombs have depleted, please choose another weapon of your choice!");
}
}//Bombs end
else if (input.equalsIgnoreCase("arrows"))
{
System.out.println("\nYou have chosen arrows as your weapon!");
if ((bow == true) && (arrows >= 1))
{
System.out.println("\nPlease press 1 to attack");
double arrowinput = keyboard.nextInt();
if (arrowinput == 1)
{
System.out.println("\nYou have only " + arrows + " left");
System.out.println("\nYou used Arrow and damaged the monster by 500 ");
monster -= 500;
arrows --;
System.out.println("\nNow the monster has the health of " + monster);
System.out.println("\nThe monster is still alive and is attacking you you have to kill it NOW CHOOSE A WEAPON SIR");
ans = keyboard.next();
}//end of attack and IF
else
{
System.out.println("\nYou have to chosen to be attacked by the monster and be eaten");
ans.equals("Y");
}//end of else
}//end of if to use the arrows and boolean
else
{
System.out.println("\nYou chose not to use the arrows now arrow is or you have run out of arrows arrow is unusable");
bow = false;
}//end of else to check for arrows
}//end of bow
else if (input.equalsIgnoreCase("energy"))
{
System.out.println("\nYou have 0.0 energy and cannot use you have to wait for 5 seconds for it to charge up half or take a hit and go ALL OUT BABY");
try
{
Thread.sleep(5000);
}catch(InterruptedException e)
{
}
System.out.println("\nSystem has charges and is ready for use, press 1");
int inputattack = keyboard.nextInt();
energy += 3000;
if (inputattack == 1)
{
String inputofretry = "Y";
if ((energy >= 3000) && (inputofretry.equalsIgnoreCase("Y")))
{
System.out.println("\nYou have used all of your energy and did a damage of 1000 to the moster!");
monster -= 1000;
energy -= 3000;
System.out.println("\nNow the monster has a health of " + monster);
System.out.println("\nWould you like to use up your other half also or use a diffrent weapon");
inputofretry = keyboard.next();
if (inputofretry.equalsIgnoreCase("N"))
{
break;
}//end of break out of if
}//end of energy half use
else if (energy <= 0)
{
System.out.println("\nRan out of energy and you have been redirected to the refill of energy");
ans = "Y";
}
}//end of input attack
}//end of energy
}//end of check for health
break;
}//While to change weapon
if (you > monster)
{
System.out.println("\t\t\nYou have defeated the monster and have conquered the land of void so be happy!!\n");
}
else
{
System.out.println("\nYou have died and have bought a great dishono..... wait a minute you have 2 lives get yo ass up there and fight like aa man");
}
}//end of main
}//end of class
try this:
I marked the places where I have made some changes.
import java.util.Scanner;
public class nestedif{
public static void main(String[] args){
System.out.println("\nWelcome\n");
Scanner keyboard = new Scanner(System.in);
int bombs = 60;
int arrows = 2;
boolean bow = true;
double energy = 0.0;
double monster = 3000;
int you = 1000;
System.out.println("\nMonster has appeared!!\n");
System.out.println("\nHeres your inventory");
System.out.println("\n60 bombs");
System.out.println("\n2 ultra arrows");
System.out.println("\n0.0 energy, you can recharge later if you choose to do so");
String ans = "Y";
//change 1
System.out.println("\nPick Weapon!");
String input = keyboard.next();
while(!(input.equalsIgnoreCase("bombs")) && !(input.equalsIgnoreCase("arrows")) && !(input.equalsIgnoreCase("energy"))){
System.out.println("\nInvalid Weapon...\nRe-pick Weapon!");
input = keyboard.next();
if((input.equalsIgnoreCase("bombs")) && (input.equalsIgnoreCase("arrows")) && (input.equalsIgnoreCase("energy"))){
break;
}
}
//end of change 1
while (ans.equalsIgnoreCase("Y"))
{
while (monster >= 0 && you >= 0)
{
//change 2.1
if(input.equalsIgnoreCase("")){
System.out.println("\nPick Weapon!");
input = keyboard.next();
}
//end of change 2.1
else{//change 2.2 (the else of the if statement above)
if (input.equalsIgnoreCase("bombs"))
{
if (bombs >= 0)
{
System.out.println("\nYou have chosen Bombs\n");
System.out.println("\nHit 9 to attack with big bombs which is -2 bombs or hit 8 for small bomb which is -1\n");
int choice1 = keyboard.nextInt();
if (choice1 == 9)
{
System.out.println("\nYou have chosen to attack with a big bomb 2 bombs ahve been taken from your inventory");
System.out.println("\nYou damaged the monster by 800\n");
monster -= 800;
bombs -= 2;
System.out.println("Now the monster has the health of" + monster + "\n");
System.out.println("\nHit Y to choose another weapon or N to back out of the fight");
ans = keyboard.next();
}//if end
else if (choice1 == 8)
{
System.out.println("\nYou have chosen to attack with a small bomb");
System.out.println("\nYou damaged the monster by 400\n");
monster -= 400;
bombs -= 1;
System.out.println("Now the monster has the health of" + monster + "\n");
System.out.println("\nHit Y to choose another weapon or N to back out of the fight");
ans = keyboard.next();
}//else if end of small bombs
}//end of if loop to do something i dont know i just leant this
else
{
System.out.println("\nYour bombs have depleted, please choose another weapon of your choice!");
}
}//Bombs end
else if (input.equalsIgnoreCase("arrows"))
{
System.out.println("\nYou have chosen arrows as your weapon!");
if ((bow == true) && (arrows >= 1))
{
System.out.println("\nPlease press 1 to attack");
double arrowinput = keyboard.nextInt();
if (arrowinput == 1)
{
System.out.println("\nYou have only " + arrows + " left");
System.out.println("\nYou used Arrow and damaged the monster by 500 ");
monster -= 500;
arrows --;
System.out.println("\nNow the monster has the health of " + monster);
System.out.println("\nThe monster is still alive and is attacking you you have to kill it NOW CHOOSE A WEAPON SIR");
ans = keyboard.next();
}//end of attack and IF
else
{
System.out.println("\nYou have to chosen to be attacked by the monster and be eaten");
ans.equals("Y");
}//end of else
}//end of if to use the arrows and boolean
else
{
System.out.println("\nYou chose not to use the arrows now arrow is or you have run out of arrows arrow is unusable");
bow = false;
}//end of else to check for arrows
}//end of bow
else if (input.equalsIgnoreCase("energy"))
{
System.out.println("\nYou have 0.0 energy and cannot use you have to wait for 5 seconds for it to charge up half or take a hit and go ALL OUT BABY");
try
{
Thread.sleep(5000);
}catch(InterruptedException e)
{
}
System.out.println("\nSystem has charges and is ready for use, press 1");
int inputattack = keyboard.nextInt();
energy += 3000;
if (inputattack == 1)
{
String inputofretry = "Y";
if ((energy >= 3000) && (inputofretry.equalsIgnoreCase("Y")))
{
System.out.println("\nYou have used all of your energy and did a damage of 1000 to the moster!");
monster -= 1000;
energy -= 3000;
System.out.println("\nNow the monster has a health of " + monster);
System.out.println("\nWould you like to use up your other half also or use a diffrent weapon");
inputofretry = keyboard.next();
if (inputofretry.equalsIgnoreCase("N"))
{
break;
}//end of break out of if
}//end of energy half use
else if (energy <= 0)
{
System.out.println("\nRan out of energy and you have been redirected to the refill of energy");
ans = "Y";
}
}//end of input attack
}//end of energy
}
}//end of check for health
break;
}//While to change weapon
if (you > monster)
{
System.out.println("\t\t\nYou have defeated the monster and have conquered the land of void so be happy!!\n");
}
else
{
System.out.println("\nYou have died and have bought a great dishono..... wait a minute you have 2 lives get yo ass up there and fight like aa man");
}
}//end of main
}//end of class
I dont have java on my pc right now. I suspect it might be due to \n remaining in the buffer because of nextInt().Try this inputofretry.nextLine(); after System.out.println("\nWould you like to use up your other half also or use a diffrent weapon"); and before taking the input.

Categories