java - Simple switch in "for" loop and toLowerCase() method -
i beginner in java. making simple switch, in user entering number in words. training added "for" loop.
package javaexc; import java.util.scanner; public class javastart { public static void main(string[] args) { scanner sck = new scanner(system.in); system.out.println("type loop counter "); int counter = sck.nextint(); system.out.println("counter is: " + counter ); (int = 0; <= counter; i++) { system.out.println("type number in words"); string ch = sck.nextline(); switch (ch.tolowercase()) { case "one": system.out.println("choice 1"); break; case "two": system.out.println("choice 2"); break; case "three": system.out.println("choice 3"); break; case "four": system.out.println("choice 4"); break; case "five": system.out.println("choice 5"); break; default: system.out.println("wrong data"); break; } system.out.println("loops left:\n " + counter--); } system.out.println("end of switch"); }}
here result:
type loop counter 5 counter is: 5 type number in words wrong data // ?? loops left: 5 type number in words 1 choice 1 loops left: 4 type number in words 3 // should ok, wrong here? wrong data loops left: 3 end of switch //to break loop think process finished exit code 0
my questions are: why first loop make default code? should make 2 scanners? 1 take value counter , second switch? why counter , numbers in words not working properly?
i know can tables etc. generally, purpose of program test "tolowercase()" method.
you're checking string in lower case ("three") against upper case ("three"). make sure they're both identical , shouldn't fail.
Comments
Post a Comment