if statement problem
The if statements seem to get mixed up for some reason, when I run it the chosen if statement is always wrong? I have the .lengths of the arrays correct so i cant seem to figure out what I'm doing wrong here.
45 Replies
⌛
This post has been reserved for your question.
Hey @stapps! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
The.... Lenghts?
You know the meaning of the word length, right?
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
yea it reads the index of an array right?
Please replace the
if
in line 52,56 and 60 by else if
. That should fix it. It is a exclusive-if chained.Still not working correctly ;-;
Post the code directly. I think you did something wrong.
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Thats why I told him to replace them by else-ifs
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Ok, I leave this.
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
I remember. But why join the help-question then?`
Hehe, I mean incomplete question, incomplete answer. Idk
The code reaches the word limit
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
/*program asks the user to use specific keys to unlock specific keys,
if the wrong key is used on the wrong key it explodes and ends the program */
import java.util.Scanner;
public class Keygame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] key = { "Skeleton key", "Stone key", "Wood key", "plastic key" };
System.out.println(
"Before you are 4 keys. \n Each key take a different key. \n You have each key in your bag but be careful. \n Using the wrong key will cause the key to explode.\n");
System.out.println("please type a number corresponding to the one beside each key \n");
for (int i = 0; i < key.length; i++) {
System.out.println(i + 1 + "." + key[i]);
}
int pkey = input.nextInt();
if (pkey >= 1 && pkey <= key.length) {
int chosenkey = pkey - 1;
System.out.println(" you chose " + key[chosenkey]);
} else {
System.out.println(
"You have chosen the (INSTANT DEATH IF YOU CHOOSE IT key) \n you will now instantly die \n game over ");
System.exit(0);
}
String[] door = { "Skeleton door", "Stone door", "Wood door", "plastic door" };
System.out.println("\nplease type a number corresponding to the one beside each door");
for (int i = 0; i < door.length; i++) {
System.out.println(i + 1 + "." + door[i]);
}
int pdoor = input.nextInt();
if (pdoor >= 1 && pdoor <= door.length) {
int chosendoor = pdoor - 1;
System.out.println(" you chose " + door[chosendoor]);
} else {
System.out.println(
"You have chosen the (INSTANT DEATH IF YOU CHOOSE IT door) \n you will now instantly die \n game over ");
System.exit(0);
}
System.out.println("you are unlocking the " + door[pdoor - 1] + " with " + key[pkey - 1]);
if (key.length == 0 && door.length == 0) {
System.out.println(
"You have chosen \n................ \n Wisely!!! \n Your prize is Infinite bones :D \n enjoy");
}
else if (door.length == 1 && key.length == 1) {
System.out.println(
"You have chosen \n................ \n Wisely!!! \n Your prize is Stone armor :D \n enjoy");
}
else if (door.length == 2 && key.length == 2) {
System.out.println(
"You have chosen \n................ \n Wisely!!! \n Your prize is a Wooden Sword :D \n enjoy");
}
else if (door.length == 3 && key.length == 3) {
System.out.println(
"You have chosen \n................ \n Wisely!!! \n Your prize is a plastic yoyo :D \n enjoy");
} else {
System.out.println(
"Wrong door \n \n self-destruct in \n 5 \n 4 \n 3 \n 2 \n 1 \n 0 \n KABOOOOOOOOOOOOOM!!!!!!!!! \n gameover ");
System.exit(0);
}
}
}
I solved it. Works fine. Try to post what I have
The mistake you made was: You compared the length of the name of the doors and keys. But you need to compare the key-numbers and the door-numbers.
Oh btw, remove that xxx package.
Also it still chooses the wrong statement, the stone key and door provides the stone armor
Oh, I did not know this rule.
My b
What should happen if I choose the plastic-key?
If you choose the plastic key and the plastic door you get the plastic yoyo
Plastic uses Key No 4 and Door No 4. But the plastic Jojo is behind door 3 and key 3.
You need to change the conditions for the plastic Yoyo to key4 and door4
Accordingly Wooden Sword is not behind door2/key2 but door3/key3
Accordingly Stone armor is not behind door1/key1 but door2/key2
Accordingly Infinite bones is not behind door0/key0 but door1/key1
i think i may have gotten something wrong with arrays, i thought pdoor == 2 && pkey == 2 would look at index 1 of String[] key = { "Skeleton key", "Stone key", "Wood key", "plastic key" }; and String[] door = { "Skeleton door", "Stone door", "Wood door", "plastic door" };
No, the array is good.
oh
it worked
thx, really appreciate it
Sucks this random practice problem I came up with took me 3 days tho XD
Next time it will take you only 2 days. Then 1 day.
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Is there a way to have the entire program repeat?
Instead of having to just shutdown it down and run again
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Ya dont need to repeat the typedeclaration for rewards btw.
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
KABOOOOOOOOOOOOOM has no effect 😄
game logic, just revive XD
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
yea i have never heard of Array.of before
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
what does immutable mean?
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
getting a "The method readline() is undefined for the type Scanner" from this
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.