正答例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package challenge05; import java.util.Scanner; public class Henkan { public static void main(String[] args) { int showa = 1926; int heisei = 1989; int reiwa = 2019; String str=""; int num; Scanner scan = new Scanner(System.in); System.out.println("西暦年を入力してください。"); int seireki = scan.nextInt(); if(seireki >= showa ){ if(seireki >= heisei){ if(seireki >= reiwa){ num = seireki - reiwa + 1; str = "令和"+num + "年"; }else{ num = seireki - heisei + 1; str = "平成"+num + "年"; } }else{ num = seireki - showa + 1; str = "昭和"+num + "年"; } } System.out.println(seireki+"年は"+str); } } |