The goal of the game is to use a while loop. In this loop, it will determine who wins the game based on the number entered. The numbers are from 1 to 20. Challenge is the variable set at 10. If <= Challenge, playerOne loses one point. If > challenge, the monster loses a point. Whoever loses 3 points first loses the game. I do not need to have random number generation, I just need inputs via the scanner function.
I thought variables were necessary for the scanner, which is why I added the Dice variables. They are not used and I am confused if I need them or not to make the scanner work so that the user can make inputs.
I am also confused on how to subtrack from the player and monster when they get hit. Which is why I set the variables under each block for the amount of points they have. This is wrong but I am stuck as to how to properly display this.
I was able to get some messages to display, but any number would give me the same result which was -1 for player one.
Essentially I am stuck on how to write this in code from here. Any help is greatly appreciated.
import java.util.Scanner;
public class Task3 {
public static void main(String[] args) {
task3(20);
}
public static void task3(int challenge) {
challenge = 10;
int player = 3;
int monster = 3;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your dice roll");
int diceRollOne = sc.nextInt();
while (player <= challenge) {
System.out.println("Monster misses");
System.out.println("Enter your dice roll");
int diceRollTwo = sc.nextInt();
continue;
if (player <= challenge) {
System.out.println("-1 for player");
player = 2;
System.out.println("Enter your dice roll");
int diceRollThree = sc.nextInt();
} else if (player > challenge) {
System.out.println("-1 for monster");
monster = 2;
System.out.println("Enter your dice roll");
int diceRollFour = sc.nextInt();
continue;
if (player <= challenge) {
System.out.println("-1 for player");
player = 1;
System.out.println("Enter your dice roll");
int diceRollFive = sc.nextInt();
continue;
if (player > challenge) {
System.out.println("-1 for monster");
monster = 1;
System.out.println("Enter your dice roll");
int diceRollSix = sc.nextInt();
continue;
} else if (player <= challenge) {
System.out.println("-1 for player");
player = 0;
System.out.println("Monster Wins");
int diceRollSeven = sc.nextInt();
continue;
if (player > challenge) {
System.out.println("-1 for monster");
monster = 0;
System.out.println("Player wins!");
int diceRollEight = sc.nextInt();
}
}
}
}
}
}
}
CodePudding user response:
When you are writing a while-loop, you must first think about the condition of when will the loop terminate / when will the loop continue. In your case, you want the loop to end when either player
or monster
become 0
. Therefore the condition for the while-loop to continue running is the opposite, i.e. both of them > 0
.
Then think about what do you want to do in each iteration. In your case, the repetitive tasks are
- Read an integer from user input
- compare the integer with
challenge
- subtract 1 point from the corresponding variable
Finally, after the loop ended, you can use the value of player
and monster
to determine the result and print it out.
import java.util.Scanner;
public class Task3 {
public static void main(String args[]) {
task3(10);
}
public static void task3(int challenge)
{
int player = 3;
int monster = 3;
int dice = 0;
Scanner sc = new Scanner(System.in);
while(player > 0 && monster > 0)
{
System.out.println("Enter your dice roll");
dice = sc.nextInt();
if(dice > challenge)
{
monster--;
}
else
{
player--;
}
}
if(player > monster)
{
System.out.println("Player wins!");
}
else
{
System.out.println("Monster wins!");
}
}
}
P.S. Try to understand the code instead of just copy and paste to your homework :)
CodePudding user response:
You dont need to have a seperate variable to get each input from the scanner.
You can get the input each time in the while loop and compare the value to challenge.
we will exit the loop only when player or monster becomes zero. once outside the loop, you can check who won and print the result accordingly.
import java.util.Scanner;
public class Task3 {
public static void main(String[] args) {
task3(10);
}
public static void task3(int challenge) {
int player = 3;
int monster = 3;
Scanner sc = new Scanner(System.in);
int diceRoll;
while (player == 0 || monster == 0) {
System.out.println("Enter your dice roll");
diceRoll = sc.nextInt();
if(player < challenge)
player--;
else
monster--;
}
if(player < monster)
System.out.println("Player wins!");
else
System.out.println("Monster Wins");
}
}
if you want only numbers between 1 and 20 as the input, you should also add an if condition which checks this after getting the input