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.
No description
No description
No description
45 Replies
JavaBot
JavaBot9mo ago
This post has been reserved for your question.
Hey @stapps! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan9mo ago
The.... Lenghts? You know the meaning of the word length, right?
JavaBot
JavaBot9mo ago
💤 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.
stapps
stappsOP9mo ago
yea it reads the index of an array right?
Peter Rader
Peter Rader9mo ago
Please replace the if in line 52,56 and 60 by else if. That should fix it. It is a exclusive-if chained.
stapps
stappsOP9mo ago
Still not working correctly ;-;
Peter Rader
Peter Rader9mo ago
Post the code directly. I think you did something wrong.
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Peter Rader
Peter Rader9mo ago
Thats why I told him to replace them by else-ifs
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Peter Rader
Peter Rader9mo ago
Ok, I leave this.
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Peter Rader
Peter Rader9mo ago
I remember. But why join the help-question then?` Hehe, I mean incomplete question, incomplete answer. Idk
stapps
stappsOP9mo ago
The code reaches the word limit
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
stapps
stappsOP9mo ago
/*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); } } }
Peter Rader
Peter Rader9mo ago
I solved it. Works fine. Try to post what I have
Peter Rader
Peter Rader9mo ago
No description
Peter Rader
Peter Rader9mo ago
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.
stapps
stappsOP9mo ago
Also it still chooses the wrong statement, the stone key and door provides the stone armor
Peter Rader
Peter Rader9mo ago
Oh, I did not know this rule.
stapps
stappsOP9mo ago
My b
Peter Rader
Peter Rader9mo ago
What should happen if I choose the plastic-key?
stapps
stappsOP9mo ago
If you choose the plastic key and the plastic door you get the plastic yoyo
Peter Rader
Peter Rader9mo ago
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
stapps
stappsOP9mo ago
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" };
Peter Rader
Peter Rader9mo ago
No, the array is good.
stapps
stappsOP9mo ago
oh it worked thx, really appreciate it Sucks this random practice problem I came up with took me 3 days tho XD
Peter Rader
Peter Rader9mo ago
Next time it will take you only 2 days. Then 1 day.
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
stapps
stappsOP9mo ago
Is there a way to have the entire program repeat? Instead of having to just shutdown it down and run again
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Peter Rader
Peter Rader9mo ago
Ya dont need to repeat the typedeclaration for rewards btw.
String[] rewards = { "Infinite bones", "Stone armor", "Wooden Sword", "plastic yoyo" };
String[] rewards = { "Infinite bones", "Stone armor", "Wooden Sword", "plastic yoyo" };
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Peter Rader
Peter Rader9mo ago
KABOOOOOOOOOOOOOM has no effect 😄
stapps
stappsOP9mo ago
game logic, just revive XD
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
stapps
stappsOP9mo ago
yea i have never heard of Array.of before
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
stapps
stappsOP9mo ago
what does immutable mean?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
stapps
stappsOP9mo ago
getting a "The method readline() is undefined for the type Scanner" from this
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot9mo ago
💤 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.
Want results from more Discord servers?
Add your server