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("您的输入有误,请重新输入:");
    		}
    	}
    
    }
    

      

  • 相关阅读:
    解决VsCode中Go插件依赖安装失败问题
    C# httpclient获取cookies实现模拟web登录
    C#中调用HttpWebRequest类中Get/Post请求无故失效的诡异问题
    VisualSVN 5.1.7破译License Key
    AutoResetEvent类的使用
    26种设计模式之单例模式
    WPF的一些感悟
    vim 常用指令
    myeclipse 的.jsp文件中的<option>无法使用
    flume部署问题解决
  • 原文地址:https://www.cnblogs.com/bravolove/p/5797535.html
Copyright © 2011-2022 走看看