import java.util.Scanner; class T4 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = 0;//接收选择的 float x;//存温度的 System.out.println("请选择操作:\n1.华氏温度--->摄氏温度\n2.摄氏温度--->华氏温度"); a = in.nextInt(); while (a != 1 && a != 2) { //让用户只选择1或2 System.out.println("请选择操作:\n1.华氏温度--->摄氏温度\n2.摄氏温度--->华氏温度"); a = in.nextInt(); } //循环,用户继续转换吗? out: while (true) { switch (a) { case 1: { System.out.println("请输入华氏温度 "); x = in.nextFloat(); System.out.println(x + "'F = " + (x - 32) * 5 / 9 + "'C"); System.out.println("继续转换吗?\n0.退出\n1.继续"); a = in.nextInt(); //让用户只选择1或0 while (a != 1 && a != 0) { System.out.println("0.退出\n1.继续"); a = in.nextInt(); } if (a == 0) { //a=0,用户要退出 break out; } else { //继续选择转换操作 System.out.println("请选择操作:\n1.华氏温度--->摄氏温度\n2.摄氏温度--->华氏温度"); a = in.nextInt(); while (a != 1 && a != 2) { //让用户只选择1或2 System.out.println("请选择操作:\n1.华氏温度--->摄氏温度\n2.摄氏温度--->华氏温度"); a = in.nextInt(); } break; } } case 2: { System.out.println("请输入摄氏度 "); x = in.nextFloat(); System.out.println(x + "'C = " + (x * 9 / 5 + 32) + "'F"); System.out.println("继续转换吗?\n0.退出\n1.继续"); a = in.nextInt(); while (a != 1 && a != 0) { //让用户只选择1或0 System.out.println("0.退出\n1.继续"); a = in.nextInt(); } if (a == 0) { //a=0,用户要退出 break out; } else { //继续选择转换操作 System.out.println("请选择操作:\n1.华氏温度--->摄氏温度\n2.摄氏温度--->华氏温度"); a = in.nextInt(); while (a != 1 && a != 2) { //让用户只选择1或2 System.out.println("请选择操作:\n1.华氏温度--->摄氏温度\n2.摄氏温度--->华氏温度"); a = in.nextInt(); } break; } } default: System.out.println("Error!操作选择有误!"); } } } }
java华氏温度与摄氏度的互相转换
最新推荐文章于 2023-09-26 14:51:28 发布