zoukankan      html  css  js  c++  java
  • 四则运算

    之前发到文章里去了,主页竟然没显示。。。

    四则运算我用的是java写的,在编写的过程中我感觉因为java没有scanf所以输入上确实比c和c++过程上繁琐一点,而且我还犯了一个低级的错误,代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package myMaths;
    import java.util.Scanner;
    public class Maths {
     
        public static void main(String[] args) {
            System.out.println("请输入运算方式(加 is 1,减is 2,乘is 3,除is 4)");
            Scanner  choose = new Scanner(System.in);
            int c =  choose.nextInt();
            if(c != 1 && c !=2  && c != 3 && c != 4 ) {
                System.out.println("非法输入!");
                System.exit(0);
                }
            else
            System.out.println("选择为:"+c);
            System.out.println("输入练习数目");
            Scanner  num = new Scanner(System.in);
            System.out.println("选择数目为:"+num.nextInt());    //此处错误
            int t =  num.nextInt();                            //此处错误
                 
                    ...
     
        }
     
    }

    这个错误导致了当我输入练习的种类和数目后程序没继续向下执行,如图:

    后经过翻阅资料及同学的帮助才发现应该先赋值在打印,因为num.nextInt()会自动扫描下一个整数,而我一次只输入一个数,但我先用.nextInt()把他打印出来已经算扫描一次,所以下面把.nextInt()赋值给t应是扫描的下一个整数但我只输入一个数,所以并没有给t赋值,他会一直等待下一次输入所以程序没往下进行。

    改正方法很简单就是先把输入的数赋值给t,以后使用t来打印和判断循环即可。

    最终程序如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    package add;
     
    import java.util.Scanner;
     
    public class ad {
     
        public static void main(String[] args) {
             
            System.out.println("请输入运算方式(加 is 1,减is 2,乘is 3,除is 4)");
            Scanner  choose = new Scanner(System.in);
            int c =  choose.nextInt();
            if(c != 1 && c !=2  && c != 3 && c != 4 ) {
                System.out.println("非法输入!");
                System.exit(0);
                }
            else
            System.out.println("选择为:"+c);
            System.out.println("输入练习数目");
            Scanner  num = new Scanner(System.in);
            int t =  num.nextInt();
            System.out.println("选择数目为:"+t);
                 
        switch(c) {
             
        case 1:
            {
            for(int n = 0;n < t;n++) {
            int t3 = (int) Math.floor(100*Math.random()) + 1;
            int t2 = (int) Math.floor(100*Math.random()) + 1;
            System.out.println( t3 + "+" + t2 );
            int t1 = t3 + t2;
            System.out.println("结果为" + t1  );
                }
            break;
            }
             
            case 2:
            {
            for(int n = 0;n < t;n++) {
            int t3 = (int) Math.floor(100*Math.random()) + 1;
            int t2 = (int) Math.floor(100*Math.random()) + 1;
            int t1 = t3 + t2;
            System.out.println( t1 + "-" + t2 );
            System.out.println("结果为" + t3  );
                }
            break;
            }
             
            case 3:
            {
            for(int n = 0;n < t;n++) {
            int t3 = (int) Math.floor(100*Math.random()) + 1;
            int t2 = (int) Math.floor(100*Math.random()) + 1;
            System.out.println( t3 + "*" + t2 );
            int t1 = t3 * t2;
            System.out.println("结果为" + t1  );
                }
            break;
            }
             
            case 4:
            {
            for(int n = 0;n < t;n++) {
            int t3 = (int) Math.floor(100*Math.random()) + 1;
            int t2 = (int) Math.floor(100*Math.random()) + 1;
            int t1 = t3 * t2;
            System.out.println( t1 + "/" + t2 );
            System.out.println("结果为" + t3  );
                }
            break;
            }
             
            }
        }
    }
        

      

    运行结果如下:

    这次我编写的四则运算程序由于功能比较单一,所以并没用多少时间,大概十几分钟就完工了,相比起来还是改错的过程比较痛苦,因为犯的是运行错误,编译时并没有发现,所以比较头疼。下次我一定会更加努力!

  • 相关阅读:
    探究操作系统的内存分配(malloc)对齐策略
    三十一个实用的小常识
    防止网页后退
    郁闷的一天
    脑袋不行
    家的开张
    猴子定律
    赴微软onsite!谁有C++/HTML/JavaScript开发工程师推荐?
    卡马克的求平方根函数代码的陷阱
    动作游戏自定义技能探讨
  • 原文地址:https://www.cnblogs.com/yzdd/p/5268534.html
Copyright © 2011-2022 走看看