//Sample_21_11.java import java.io.Console; public class Sample_21_11 { private static Console cons = System.console(); public static Integer getInteger(String msg){ Integer oi = null; String str = cons.readLine(msg); try{ oi = Integer.valueOf(str); }catch(NumberFormatException e){ System.out.println(e); } return oi; } public static Double getDouble(String msg){ Double od = null; String str = cons.readLine(msg); try{ od = Double.valueOf(str); }catch(NumberFormatException e){ System.out.println(e); } return od; } public static void main(String[] args) { Integer oi = getInteger("Input int: "); System.out.println("input x 2 = " + (oi * 2)); Double od = getDouble("Input double: "); System.out.println("input x 2 = " + (od * 2)); } }
ws>javac Sample_21_11.java ws>java Sample_21_11 Input int:
ws>javac Sample_21_11.java ws>java Sample_21_11 Input int: 357 <---自分で入力した input x 2 = 714 Input double:
ws>javac Sample_21_11.java ws>java Sample_21_11 Input int: 357 input x 2 = 714 Input double: 8.25 <---自分で入力した input x 2 = 16.5