Java初学者使用 break continue switch 运用实例

来源:互联网 发布:私密空间软件 编辑:程序博客网 时间:2024/06/11 14:13
package mypackage;
import javax.swing.JOptionPane;public class Test1 {//通过用户输入1-12的整数,输出对应的月份英文public static void main(String[] args) {// TODO Auto-generated method stubint i,a=0;String   month=null;for(i=1;i<=11;i++)\\for循环为了让用户在输入范围错误之后 能重新输入 {      String  input=JOptionPane.showInputDialog("请输入1-12之间的整数");a=Integer.parseInt(input);if (a>0&&a<13) break;//注意此处的逻辑关系  刚开始实验了条件为if(a<1||a>12)   因为a为int型,有取值范围限制,而a<1或者是a>12都没有限制边界else System.out.println("请重新输入");continue;}switch(a)//switch的用法{case 1:month="January ";break;case 2:month="February ";break;case 3:month="March";break;case 4:month="April";break;case 5:month="May ";break;case 6:month="June";break;case 7:month="July";break;case 8:month="August";break;case 9:month="September";break;case 10:month="October ";break;case 11:month="November ";break;case 12:month="December";}System.out.println(a+" "+month);}}


原创粉丝点击