This is a game where you can take biscuits from barrels, either from barrel1, barrel2, or both. The last player to take the last biscuits wins the game. I implemented the game in a do-while loop so that it loops every turn. However, once the number of biscuits in both barrels = 0, the loop doesn't terminate and keeps on taking scanner input.
N.B. This is coursework for university, so please do not tell me exactly what to do or give me exact code, just suggestions or why my code is not working.
import java.util.Scanner;
public class LastBiscuit {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int barrel1 = 6;
int barrel2 = 8;
// Simple turn counter. incremented every loop. if even, player 2 turn
int turnCounter = 0;
do {
turnCounter++;
int howMany = 20;
String turnAction;
// prints out biscuits left in each barrel
String output1 = String.format("Biscuits Left - Barrel 1: %d",barrel1);
String output2 = String.format("Biscuits Left - Barrel 2: %d",barrel2);
System.out.println(output1);
System.out.println(output2);
// Check turn counters value. If even, it is player 2 turn, else player 1 turn.
if (turnCounter % 2 == 0) {
System.out.println("Player Turn: 2");
}
else {
System.out.println("Player Turn: 1");
}
// Player picks what action to take in their turn. Stored in turnAction. Only allows correct input
System.out.print("Choose a barrel: barrel1 (one), barrel2 (two), or both (both), or skip turn (skip)?");
do {
turnAction = in.next();
} while (!turnAction.equalsIgnoreCase("one") && !turnAction.equalsIgnoreCase("two") &&
!turnAction.equalsIgnoreCase("both") && !turnAction.equalsIgnoreCase("skip"));
// Player picks how many biscuits to take, if at all. If biscuits taken larger than biscuits remaining,
// they have to re-enter integer
if (!(turnAction.equalsIgnoreCase("skip"))) {
System.out.print(" How many biscuits are you taking?");
while(!in.hasNextInt()) {
System.out.println("Try again");
in.next();
}
howMany = in.nextInt();
while (barrel1 - howMany < 0 || barrel2 - howMany < 0 || howMany <= 0) {
howMany = in.nextInt();
}
}
// Takes biscuits from barrels chosen
if (turnAction.equalsIgnoreCase("one")) {
barrel1 -= howMany;
}
if (turnAction.equalsIgnoreCase("two")) {
barrel2 -= howMany;
}
if (turnAction.equalsIgnoreCase("both")) {
barrel1 -= howMany;
barrel2 -= howMany;
}
// do nothing on skip
} while (barrel1 > 0 || barrel2 > 0);
//bug? doesnt print? outside of do-while loop
// doesnt exit loop?
System.out.println("YOYYYOOYOY");
if (turnCounter % 2 == 0) {
System.out.println("Player 2 Wins! ");
}
else {
System.out.println("Player 1 Wins! ");
}
}
}
You have an infinite while loop running at:
while (barrel1 - howMany < 0 || barrel2 - howMany < 0 || howMany <= 0) {
howMany = in.nextInt();
}
This only happens when you require more biscuits than a barrel contains or you request a negative number of biscuits. When it does you will be forever in this while loop.
Place a breakpoint at howMany = in.nextInt(); as already suggested and you will see it happening.
Related
import java.util.Scanner;
public class Swap {
// if the number is less than 10, swap the last two numbers and print them.
public static void main(String[] args) {
// User to enter a number between 1 and 10, but not zero.
Scanner number = new Scanner(System. in );
System.out.println("Enter a Integer(whole number) between 1 and 10. : ");
int userNum = number.nextInt();
while (userNum > 10 || userNum < 0) {
System.out.println("Try again: ");
userNum = number.nextInt();
}
System.out.println("Your number loop");
while (userNum <= 10) {
System.out.println(userNum);
userNum++;
}
System.out.println("Guess the two swap numbers:");
}
}
How do I swap the last two numbers? I am a beginner learning java and OOP. I have created this program where the user has to enter a number between 1 and 10. If the user enters a number below 1 and above 10, the user gets prompted to try again. Then it prints the list of numbers based off the users input. e.g. if the user enters 8, its prints the loop 8,9 and 10. I have having trouble, I understand how to swap two variable, not inside a loop. Thank you and much appreciated for your help.
Let's assume that the maximum number is a parameter N, so that you could swap any last two numbers and place N before N - 1
private static final int N = 10;
There are several ways to do this using different Java operators:
if, to update delta parameter
while (userNum <= N) {
int delta = 0;
if (userNum >= N - 1) {
delta = userNum == N - 1 ? 1 : -1;
}
System.out.println(userNum + delta);
userNum++;
}
or simply skip N - 1 and print it after the loop:
while (userNum <= N) {
if (userNum != N - 1) {
System.out.println(userNum);
}
userNum++;
}
System.out.println(N - 1);
switch
while (userNum <= N) {
int printNum = userNum++;
switch(printNum) {
case N:
printNum--; break;
case N - 1:
printNum++; break;
default:
break;
}
System.out.println(printNum);
}
two consequent loops (the second going backwards):
while (userNum < N - 1) {
System.out.println(userNum++);
}
userNum++;
while (userNum >= N - 1) {
System.out.println(userNum--);
}
another way this can be solved is by creating a loop(for or while) and taking the two numbers you can use the math functions- Math.max(a,b) || Math.min(a,b) to find the biggest and smallest numbers. Afterwards you can create more variables- c&d to save the two numbers.
goodluck
a=max
b=min
then
c=a
d=b;
then a=d and b=c.
I have a for-loop which asks for scores between 0 and 10. It asks a certain amount depending on the number of judges.
Here's the code:
System.out.println("Number of judges: ");
int numOfJudges = IO.readInt();
int sum = 0;
for (int i=0; i<numOfJudges; i++) {
System.out.print("Enter judge's score: ");
int score = IO.readInt();
if (score >= 0 && score <= 10) {
sum += score;
} else {
System.out.println("Incorrect number, must be between 0 and 10.");
}
}
System.out.println(sum);
I want to make is so if a number is entered that's not between 0 and 10, it won't count that as one of the conditions as i < numOfJudges.
For example if I have 3 judges and I enter 2 wrong inputs, it will still only run the loop 3 times (and only take the good input into account) while I really want it to run 5 times to make up for the two incorrect inputs.
Increment numOfJudges in case of ELSE condition so that your FOR loop would run until you have desired number of correct inputs.
This is shortest and cleanest solution.
else {
System.out.println("Incorrect number, must be between 0 and 10.");
numOfJudges++;
}
You can use a while loop inside of the for-loop, instead of adjusting the for-loop:
for (int i=0; i<numOfJudges; i++) {
while(true){
System.out.print("Enter judge's score: ");
int score = IO.readInt();
if (score >= 0 && score <= 10) {
sum += score;
break; //jump out of while-loop
}else {
System.out.println("Incorrect number, must be between 0 and 10.");
}
}
}
System.out.println(sum);
The aim of this program is to make a Rock Paper Scissors game. I have succeeded in making it however I can not get it to loop no matter what I try. I tried:
while (index = 0)
while (index < gamesCount)
However, while my index is 0 and my condition says while (index != 0), it seems to be the only condition that runs the program but it will not loop regardless. How can I get my game to loop?
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random randomGen = new Random();
//Variables
String player1;
int cpu;
int start = 1;
int end = 3;
int index = 0;
// 1 = Rock | 2 = Scissors | 3 = Paper
//Code
System.out.println("Welcome to Rock, Paper, Scissors!");
while (index != 0) {
System.out.print("Rock, Paper, or Scissors?: ");
player1 = in.nextLine();
cpu = randomGen.nextInt(3);
System.out.println(cpu);
if (player1.equals("Rock") && (cpu == 2)) {
System.out.println("You lose!");
} else if (player1.equals("Rock") && (cpu == 1)) {
System.out.println("You win!");
} else if (player1.equals("Rock") && (cpu == 0)) {
System.out.println("Draw!");
}
// --------------------
if (player1.equals("Scissors") && (cpu == 2)) {
System.out.println("Draw!");
} else if (player1.equals("Scissors") && (cpu == 1)) {
System.out.println("You win!");
} else if (player1.equals("Scissors") && (cpu == 0)) {
System.out.println("You lose!");
}
//---------------------
if (player1.equals("Paper") && (cpu == 2)) {
System.out.println("You lose!");
} else if (player1.equals("Paper") && (cpu == 1)) {
System.out.println("You win!");
} else if (player1.equals("Paper") && (cpu == 0)) {
System.out.println("Draw!");
}
}
}
}
You have your index variable set to 0. The condition of the while loop is saying, if index does not equal 0, execute the code in the loop. Since your index equals 0, the instructions in the loop will not be executed. Also, you will need to update the index variable in the loop so that if the condition you are looking for is met, the code will stop looping.
ie:
int gamesPlayed = 0;
int gamesRequested = 3; // or get this from the user
while (gamesPlayed < gamesRequested){
String player1Choice = in.nextLine();
if(!"".equals(player1)){
// your code
gamesPlayed++;
} else {
System.out.print("Rock, Paper, or Scissors?: ");
}
}
Two mistakes:
while (index != 0);
this is the entire loop. it ends either at the end of the { } block (which you don't have), or at the first ; which is immediately after the statement.
Correct this, though, and it still won't loop:
int index = 0;
// 1 = Rock | 2 = Scissors | 3 = Paper
//Code
System.out.println("Welcome to Rock, Paper, Scissors!");
while (index != 0);
index = 0, so (index != 0) will never return true.
Your index variable is set to a value of 0.
Your while loop says
while (index != 0);
Which means, while the index isn't 0, run my code. The problem is your code will never run then because your index value is always 0.
Try changing it to another value (say 5 for example), and it should work now.
:)
I don't understand why only my while statement is working and it does not move on to the for statement for the valid integer.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
long posNumber;
long x;
long fact = 1;
do {
System.out.print("Enter a number between 2 and 15: ");
Scanner in = new Scanner(System.in);
posNumber = in.nextLong();
} while (posNumber >= 2 || posNumber <= 15);
for (x = 1; x <= posNumber; x++)
fact = fact*x;
System.out.println("Factorial of " +posNumber+ " is " +fact);
}
}
You should try something like, if you plan to get numbers in a loop:
Scanner in = new Scanner(System.in);
do {
System.out.print("Enter a number between 2 and 15: ");
posNumber = in.nextLong();
for (x = 1; x <= posNumber; x++)
fact = fact*x;
System.out.println("Factorial of " +posNumber+ " is " +fact);
}
} while (posNumber >= 2 || posNumber <= 15);
Or you can change the condition (in case to run it just once):
while (posNumber < 2 || posNumber > 15);
You want your program to continue asking the user if the number is invalid. That means if it is less than 2 or greater than 15. Replace your while condition with:
do {
...
} while (posNumber < 2 || posNumber > 15);
If the user enters a 1, posNumber < 2 will evaluate to true causing the loop to repeat and ask for a new number.
If the user enters 3, both posNumber < 2 and posNumber > 15 will evaluate to false and the loop will break and then your for loop will execute.
Having trouble with this code that am I writing. The purpose of the code is to formulate modifiers for a number sequence and then give the first 10 numbers in that sequence. However, something appears to be wrong with my loop mechanism because it is printing out an infinite amount of values when it should only be doing 10. I plan on including the division and power functions to the code, but ran into this problem.
import java.util.Scanner;
public class PatternCreator {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out
.println("Please enter the starting value of the number sequence.");
double sequence = s.nextInt();
System.out
.println("Please enter the addition/subtraction modifier; e.g. 2,-2.");
double addsub = s.nextInt();
System.out
.println("Please enter the multiplication modifier; 0 for none.");
double mult = s.nextInt();
System.out.println("Please enter the division modifier; 0 for none.");
double divi = s.nextInt();
System.out
.println("Please enter the exponential modifier; 0 for none.");
double power = s.nextInt();
double addonly = sequence + addsub;
while (mult == 0 && divi == 0 && power == 0) {
for (int count1 = 1; count1 <= 10; count1++) {
if (count1 == 1) {
System.out.print(sequence + " ");
} else {
System.out.print(addonly + " ");
addonly = addonly + addsub;
}
}
}
double multadd = sequence + addsub * mult;
while (mult != 0 && divi == 0 && power == 0) {
for (int count2 = 1; count2 <= 10; count2++) {
if (count2 == 1) {
System.out.print(sequence + " ");
} else {
System.out.print(multadd + " ");
multadd += multadd;
}
}
}
}
}
Sounds like mult, divi, and power are all 0, and you never change them-- therefore you're executing your while loop (and therefore your for loop) an infinite number of times.
Why do you have that while loop there at all?
You never, ever, change the values of multi, divi or power. How do you expect the while to end if those values never change?