#Java Scanner
21 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @wheat tree! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
import java.util.Random;
import java.io.*;
import java.util.Scanner; ```
public class Main {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
Random random = new Random();
int command;
// goblin stats
int goblinhp = 20;
// player stats
int playerhp = 30;
int playerdef = 1;
int playerattack = random.nextInt(5);
System.out.println("You approached an innocent Goblin!");
while (true) {
// player stats in battle
playerdef = 1;
// player attack
playerattack = random.nextInt(5);
int playercrit = random.nextInt(9);
// goblin attack
int goblinattack = random.nextInt(3);
int bite = 2, slash = 5, stab = 7;
// hp display
System.out.println("\nYour Hp:" + playerhp + "\nGoblin Hp:" + goblinhp);
// fight intro
System.out.println("\nThe Goblin is terrified");
// commands list
System.out.println("\nEnter 1 to Attack \nEnter 2 to Guard \nEnter other numbers to skip your turn");
System.out.print("\nEnter command:");
command = input.nextInt();
// command choice logic
if (command == 1) {
if (playerattack <= 0) {
System.out.println("\nAttack missed!");
}
else if (playercrit == 3 && playerattack > 0) {
System.out.println("\nYou attacked the the Goblin \nA decisive strike!");
goblinhp -= playerattack * 2;
System.out.println("\nThe Goblin shrieks in pain! \nIt lost " + (playerattack * 2) + " Hit point(s)");
} ```
else {
System.out.println("\nYou attacked the Goblin");
goblinhp -= playerattack;
This message has been formatted automatically. You can disable this using /preferences.
System.out.println("It lost " + playerattack + " Hit point(s)");
} ```
if (goblinhp <= 0) {
System.out.println("\nThe innocent goblin has been brutally murdered");
break;
}
}
else if (command == 2) {
playerdef = 100;
System.out.println("\nYou raised your guard");
}
else {
System.out.println("\nYou skipped your turn");
}
// goblin attack logic
if (bite <= 0 || slash <= 0 || stab <= 0) {
System.out.println("\nGoblin's attack missed!");
}
else if (bite <= playerdef || slash <= playerdef || stab <= playerdef) {
System.out.println("You are too impervious \nDamage negated");
}
else if (goblinattack == 0) {
System.out.println("\nThe goblin used Bite");
playerhp -= (bite - playerdef);
System.out.println("You lost " + (bite - playerdef) + " Hit point(s)!");
}
else if (goblinattack == 1) {
System.out.println("\nThe Goblin used Slash");
playerhp -= (slash - playerdef);
System.out.println("You lost " + (slash - playerdef) + " Hit point(s)!");
}
else {
System.out.println("\nThe Goblin used Stab");
playerhp -= (stab - playerdef);
System.out.println("You lost " + (stab - playerdef) + " Hit point(s)!");
}
// exits if conditions are met otherwise loops
if (playerhp <= 0) {
System.out.println("You have been slain!");
break;
}
else {
continue;
}
// reserved
}
}
} ```
This message has been formatted automatically. You can disable this using /preferences.
Well everyone can have a future in coding. And judging by how most of my co-students don’t understand how loops work you might have a bright future even.
Thank you really
I needed that
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
surprisingly creative and committed. If you feel like making games on a proper minimal system. Try libGDX
You can learn a lot of things by trying out random ideas
Though for your while loop, I would recommend a static int named "stage". Which you can change to connect the game events which you design
using a switch case
for example, if stage is 1, it's on home menu. if it's 2, then it's on settings menu etc etc
inside the while loop can be
switch (stage) {
case 1:
//show home menu
break;
case 2:
//show settings menu
break;
}
I usually teach this to students who are creating console menus to make it easier to navigate
you can make the stage an array as well. To categories inner stages and outer stages
0, 0 is quit game, 1, 0 is menu. 1, 1 is settings. 2, 0 is gameplay
with an arrayed stage, just put a switch case in another switch case
switch (stage[0]) {
case 1:
//show home menu
switch (stage[1]) {
case 0:
break;
}
break;
case 2:
//show settings menu
break;
}
@wheat tree
Using too many if else statements is a sign that you can design it better