java - Why does this switch statement not work? -
this question has answer here:
import java.util.scanner; public class verticalwire { public static void main(string[] args) { scanner in = new scanner(system.in); system.out.println("how many wires"); string howmanywires = in.next(); switch(howmanywires) { case "3": { system.out.println("true"); break; } case "3 5": { system.out.println("false");break;} } } } i tested if enter "3 5" returns true though think should return false! wrong?
use in.nextline() instead of in.next().
documentation of scanner.next() states that
finds , returns next complete token scanner.
so when enter 3 5 input, read 3 when call next method , if again call next method read 5.
Comments
Post a Comment