zoukankan      html  css  js  c++  java
  • 无限循环输入

    1.推荐 呕心沥血,自己写的!

    public class Hello {
    	public static void main(String[] args)  {
    		inputScore();
    	}
    	
    	public static void inputScore(){
    
    		while(true){
    			try {
    				System.out.println("请输入您的分数:");
    				BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    				String score = bf.readLine();
    				String regex = "^[0-9]\d*$";
    				if(score.equalsIgnoreCase("quit")){
    					System.out.println("谢谢你的使用,欢迎下次光临.");
    					System.exit(0);
    				}
    				if(score.matches(regex)){
    					getGrade(Integer.parseInt(score));
    				}
    				System.out.println("如果想退出输入,请输入quit");
    			} catch (Exception e) {
    				System.out.println("2222");
    				throw new RuntimeException("您的输入有误...");
    			}
    		}
    	}
    	
    	public static void getGrade(int score){
    		if(score >= 90){
    			System.out.println("优秀");
    		}else if(score >= 80){
    			System.out.println("良好");
    		}else if(score >= 70){
    			System.out.println("一般");
    		}else if(score >= 60){
    			System.out.println("差强人意");
    		}else if( score >= 0){
    			System.out.println("补考");
    		}else{
    			System.out.println("您的输入有误,请重新输入:");
    		}
    	}
    
    }
    

      2. 普通的单次输入

    public class Hello {
    	public static void main(String[] args)  {
    		Scanner scanner = new Scanner(System.in);
    		boolean flag = true;
    		while(flag){
    			try {
    				System.out.println("请输入您的分数:");
    				int score = scanner.nextInt();
    				getGrade(score);
    			
    			} catch (Exception e) {
    				System.out.println("分数必须是数字! 请重新输入:");
    				flag = false;
    			}
    		}
    	}
    
        public static void getGrade(int score){
    		if(score >= 90){
    			System.out.println("优秀");
    		}else if(score >= 80){
    			System.out.println("良好");
    		}else if(score >= 70){
    			System.out.println("一般");
    		}else if(score >= 60){
    			System.out.println("差强人意");
    		}else if( score >= 0){
    			System.out.println("补考");
    		}else{
    			System.out.println("您的输入有误,请重新输入:");
    		}
    	}
    
    }
    

      

  • 相关阅读:
    SQLServer 日期函数大全 SQLServer 时间函数大全
    matlab练习程序(最小二乘多项式拟合)
    常用的docker命令
    【Python】keras使用LSTM拟合曲线
    【Python】keras使用Lenet5识别mnist
    【Python】pydot安装失败解决方法
    【Python】keras卷积神经网络识别mnist
    【Python】keras神经网络识别mnist
    matlab练习程序(神经网络识别mnist手写数据集)
    判断exe是64位还是32位
  • 原文地址:https://www.cnblogs.com/bravolove/p/5797535.html
Copyright © 2011-2022 走看看