mercredi 29 août 2018

if else structure in java printing out strange code

Hi there I have become stuck with an exercise on if-else statements in java the problem is when I run the code it prints; Enter a card: 9 S end 9ofS

please see below for my code example:

 String suit;
 String rank;
 String userOne; 
 Scanner sc = new Scanner( System.in );
 System.out.print("Enter a card: ");
 rank= sc.next(); 
 suit= sc.next();
 userOne= sc.next();
 if (userOne.equals("9")){ System.out.println("Nine");
 } else if (userOne.equals("S")){ System.out.println("Spades");       
 } System.out.print(rank+ "of" +suit);

had to squeeze the code in at the bottom I know its not in the right format but for just getting an answer to this question I had to lay it out like that.

Why is the code printing out- Enter a card: 9 S end 9ofS instead of Enter a card: 9 S Nine of Spades ?     

Aucun commentaire:

Enregistrer un commentaire