小學生四則運算出題程式 無操作介面java版 簡單的運用java中一些基本操作,四則運算java

來源:互聯網
上載者:User

小學生四則運算出題程式 無操作介面java版 簡單的運用java中一些基本操作,四則運算java

這是本學期java課中實驗大綱裡的第一個實驗,這裡簡單做了一個無使用者介面版本。

import java.util.Random;import java.util.Scanner;public class MathTest {    public static void main(String[] args) {        // TODO Auto-generated method stub                double right = 0;        double wrong = 0;        /*Scanner sc2 = new Scanner(System.in);        int num2 = sc2.nextInt();*/        for (int i = 0; i < 10; i++) {            System.out.println("第"+(i+1)+"道題,請在下方輸入答案:");                            //產生兩個隨機數        Random r1 = new Random();        int x = r1.nextInt(10)+1;        Random r2 = new Random();        int y = r2.nextInt(10)+1;        //產生隨機運算子    + - * /        Random r3 = new Random();        int z = r3.nextInt(4);        char[] chs = {'+','-','*','/'};        String Operator = String.valueOf(chs[z]);                //產生題目        if(Operator.equals("+")){            System.out.println(x+"+"+y+"=");            boolean b = add(x,y);            if(b == true){                right++;System.out.println("bingo! ");            }else{                wrong++;System.out.println("oh~no ");            }            System.out.println("the right answer: "+(x+y));                    }else if(Operator.equals("-")){            System.out.println(x+"-"+y+"=");            boolean b =minus(x,y);            if(b == true){                right++;System.out.println("bingo! ");            }else{                wrong++;System.out.println("oh~no ");            }            System.out.println("the right answer: "+(x-y));                    }else if(Operator.equals("*")){            System.out.println(x+"×"+y+"=");            boolean b =times(x,y);            if(b == true){                right++;System.out.println("bingo! ");            }else{                wrong++;System.out.println("oh~no ");            }            System.out.println("the right answer: "+(x*y));        }else{            System.out.println(x+"÷"+y+"=");            boolean b =divide(x,y);            if(b == true){                right++;System.out.println("bingo! ");            }else{                wrong++;System.out.println("oh~no ");            }              System.out.println("the right answer: "+((float)x /(float) y));        }        System.out.println("-------------------------------");        }        System.out.println("您一共做對了"+right+"道題.");        System.out.println("您一共做錯了"+wrong+"道題.");        if(wrong > 0){        System.out.println("正確率為"+(right/(wrong+right))*100+"%");        }else{            System.out.println("沒法除了啊   。。。。");        }    }    private static boolean add(int x,int y) {        // TODO Auto-generated method stub        Scanner sc = new Scanner(System.in);        int num1 = sc.nextInt();        int result = x + y;        if(num1 == result){            return true;        }else{            return false;        }            }    private static boolean minus(int x,int y) {        // TODO Auto-generated method stub        Scanner sc = new Scanner(System.in);        int num1 = sc.nextInt();        int result = x - y;        if(num1 == result){            return true;        }else{            return false;        }            }    private static boolean times (int x,int y) {        // TODO Auto-generated method stub        Scanner sc = new Scanner(System.in);        int num1 = sc.nextInt();        int result = x * y;        if(num1 == result){            return true;        }else{            return false;        }        }    private static boolean divide(int x,int y) {        // TODO Auto-generated method stub        Scanner sc = new Scanner(System.in);        float num1 = sc.nextFloat();        float result =(float)x /(float) y;        if(num1 == result){            return true;        }else{            return false;        }    }}

能看到判斷對錯的方法運用了直接運算子計算結果與函數定義後的運算結果相比較,相等得分,不相等則不得分。

編程中出現的問題:在計算結果為小數的除法時,一開始會出現結果怎麼算都不對的情況,他的正確答案也是一個向偶舍入的數值,那肯定是不可取的

解決方案:原因是x和y預設int型,在float除法中要將兩者都強制類型轉換。【這樣他的精度為小數點後7位】

附上最後的一部分結果展示。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.