Monday, 26 August 2013

java nested for loop not working...help please

java nested for loop not working...help please

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(" ");
}
}
}

No comments:

Post a Comment