//Sample_21_16.java import java.io.Console; public class Sample_21_16 { private static Console cons = System.console(); public static Boolean getBoolean(String msg){ Boolean yes = null; char ch = ' '; String str = cons.readLine(msg); if(str.length() != 0){ ch = str.charAt(0); } if(ch=='y' || ch=='Y' || ch=='n' || ch=='N'){ yes = ch!='n' && ch!='N'; } return yes; } public static void main(String[] args) { int floor = 1;//階 boolean bool = getBoolean("階段を上りますか? (y/n): "); System.out.println("echo : " + bool); if(bool){ floor++; System.out.println(floor + "階に上った。"); }else{ System.out.println(floor + "階にいます。"); } } }
ws>javac -encoding UTF-8 Sample_21_16.java ws>java Sample_21_16 階段を上りますか? (y/n):
ws>javac Sample_21_16.java ws>java Sample_21_16 階段を上りますか? (y/n): y <---自分で入力した echo : true 2階に上った。