• 2018/12/06 L1-025 正整数A+B Java


    这题的解法使用了一个非常巧妙的方法, 使用try报错来判断输入的值是否全为数字, 如果发生报错, 那么就表明无法通过Integer.parseInt(str.[0或1])将输入的值转换为整型值, 说明输入的值有字母, 判断成功.

    考验人的分析能力, 使用Scanner的代码:

    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();
            s = s.trim();
            String[] num = s.split("\s+");//以空格拆分字符串
            int flag1 = 1;
            int flag2 = 1;
            int a = 0, b = 0;
            
            if (num.length > 2) {    //如果拆分数组的长度大于2说明有多个空格
                flag2 = 0;
            }
            try {
                a = Integer.valueOf(num[0]);
                if (a < 1 || a > 1000) {
                    flag1 = 0;
                }
            } catch (Exception e) {
                flag1 = 0;
            }
     
            try {
                b = Integer.valueOf(num[num.length - 1]);
                if (b < 1 || b > 1000) {
                    flag2 = 0;
                }
            } catch (Exception e) {
                flag2 = 0;
            }
            
            
            if (flag1 == 0) {
                System.out.print("? + ");
            } else {
                System.out.print(a + " + ");
            }
     
            if (flag2 == 0) {
                System.out.print("? = ");
            } else {
                System.out.print(b + " = ");
            }
     
            if (flag1 == 0 || flag2 == 0) {
                System.out.println("?");
            } else {
                System.out.println(a + b);
            }
        }
    }

     我是用BufferedReader和InputStreamReader的方法写的答案:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class Main2 {
    
        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String[] str = br.readLine().split(" ");
            int flag1 = 1;
            int flag2 = 1;
            int a = 0;
            int b = 0; 
            
            try {
                a=Integer.parseInt(str[0]);
                if( a < 1 || a > 1000 ) {
                    flag1 = 0;
                }
            } catch( Exception e) {
                flag1 = 0;  // 如果a无法改为整数, 就表明输入的第一个不是数字, try...catch的方式成功的实现了判断输入的字符串是否全是数字.
            }
            try {
                b=Integer.parseInt(str[1]);
                if( b < 1 || b > 1000 ) {
                    flag2 = 0;
                }
            } catch( Exception e) {
                flag2 = 0;  // 如果b无法改为整数, 就表明输入的第二个不是数字, try...catch的方式成功的实现了判断输入的字符串是否全是数字.
            }
            
            if (str.length > 2) {
                // 说明有超过两个数被输入.
                flag2 = 0;  // 设置第二个数为不合法输入.
            }
            
            if (flag1 == 0) {
                System.out.print("? + ");
            } else {
                System.out.print(a + " + ");
            }
     
            if (flag2 == 0) {
                System.out.print("? = ");
            } else {
                System.out.print(b + " = ");
            }
     
            if (flag1 == 0 || flag2 == 0) {
                System.out.println("?");
            } else {
                System.out.println(a + b);
            }
            
        }
    
    }
  • 相关阅读:
    Jmeter测试接口详细步骤(三)跨线程组传值-BeanShell 后置处理程序
    Jmeter测试接口详细步骤(二)HTTP Cookie管理器
    Jmeter测试接口详细步骤(一)基础操作
    最优化学习3
    最优化学习2
    最优化学习---从解方程到最优化1
    最优化学习---从解方程到最优化
    博客园 文章和随笔区别 (转
    refinedet tensorRT实现
    crnn pytorch 训练、测试
  • 原文地址:https://www.cnblogs.com/huangZ-H/p/10077304.html
Copyright © 2020-2023  润新知