Reputation:
I am relatively new to java and I am trying to write a program in Eclipse that when the user types 'r', it will randomly pick a gun to "give" them one of five guns...Sort of like the mystery box in Call of Duty Zombies. im confused at why it wont output a random "gun" after I type in 'r'. Please help!!!
import java.util.Random;
import java.util.Scanner;
class apples{
public static void main(String[] args){
System.out.println("Type 'r' for a random gun");
Random dice = new Random();
int number;
Scanner input = new Scanner(System.in);
String userinput = input.nextLine();
if (userinput=="r"){
for (int counter=1; counter<=1; counter++){
number = 1+dice.nextInt(5);
if (number==1){
System.out.println("gun 1");
}else if (number==2){
System.out.println("gun 2");
}else if (number==3){
System.out.println("gun 3");
}else if (number==4){
System.out.println("gun 4");
}else if (number==5){
System.out.println("gun 5");
}
}
}else{
System.out.println(" ");
}
}
}
Upvotes: 0
Views: 259
Reputation: 3984
EDIT- Apologies, huh! I didn't notice @lal's comment , apparently he did notice the bug first , so my answer is bit elaboration on his part.
When you are doing this :
if (userinput=="r")
You are actually comparing objects' references not the actual objects .The operator ==
compares object references, so the output is actually “false”
rather than “true.”
Naturally, this surprises people at first.
To rectify this you should try the equals method like this :
if (userinput.equals("r"))
Please note that the default implementation of equals()
by java.lang.Object
compares memory location and only return true if two reference variable are pointing to same memory location i.e. essentially they are same object .Therefore ,to test whether two objects are equal in the sense of equivalency (containing the same information), you must override the equals()
method.(Without overriding the equals()
method, it will act like ==
. and as I said when you use the ==
operator on objects, it simply checks to see if the references are of same objects. Not if their members contain the same value.)
Here however, you don't need to override because String class already overrides it .
Upvotes: 0
Reputation: 31
Try using
int randomInt = dice.nextInt(4);
if(userInput.equals("r")){
if (randomInt==1){
System.out.println("gun 1");
}else if (randomInt==2){
System.out.println("gun 2");
}else if (randomInt==3){
System.out.println("gun 3");
}else if (randomInt==4){
System.out.println("gun 4");
}else if (randomInt==5){
System.out.println("gun 5");
}
}
so overall final code should look like
public class test {
public static void main(String[] args) {
System.out.println("Type 'r' for a random gun");
Random dice = new Random();
int number;
Scanner input = new Scanner(System.in);
String userinput = input.nextLine();
int randomInt = dice.nextInt(4);
if (userinput.equals("r")) {
if (randomInt == 1) {
System.out.println("gun 1");
} else if (randomInt == 2) {
System.out.println("gun 2");
} else if (randomInt == 3) {
System.out.println("gun 3");
} else if (randomInt == 4) {
System.out.println("gun 4");
} else if (randomInt == 5) {
System.out.println("gun 5");
}
}
}
}
Upvotes: 1