20165303 2017-2018-2 《Java程式設計》結對程式設計練習_四則運算

來源:互聯網
上載者:User

標籤:out   簡單   main   基本   result   OLE   分析   number   equals   

  • 需求分析
    能產生簡單四則運算題目並判斷使用者回答對錯。
    能正確統計回答正確的機率。
    能正確處理混合四則運算的優先順序的問題。
    能正確輸出負數等結果。
    能處理簡單的加,減,乘,除運算。
    能正確的處理有括弧的情況並輸出正確的運算結果,能正確處理輸入異常的情況。
  • 運行

  • 代碼實現
    import java.util.;
    public class sizeyunsuan {
    public static void main(String[] args) {
    System.out.println("輸入測試題數目:");
    Scanner sc=new Scanner(System.in);
    int number=sc.nextInt();
    double right = 0;
    double wrong = 0;
    for (int i = 0; i < number; i++) {
    System.out.println("第"+(i+1)+"道題,請在下方輸入答案:");
    double op1=(int)(Math.random()
    10);
    double op2=(int)(Math.random()10);
    Random r3 = new Random();
    int m = r3.nextInt(4);
    char[] chs = {‘+‘,‘-‘,‘
    ‘,‘/‘};
    String Operator = String.valueOf(chs[m]);
    //產生題目
    if(Operator.equals("+")){
    System.out.println(op1+"+"+op2+"=");
    boolean b = add(op1,op2);
    if(b == true){
    right++;System.out.println("right! ");
    }
    else{
    wrong++;System.out.println("no ");
    System.out.println("right answer: "+(op1+op2));
    }
    }
    else if(Operator.equals("-")){
    System.out.println(op1+"-"+op2+"=");
    boolean b =minus(op1,op2);
    if(b == true){
    right++;System.out.println("right! ");
    }
    else{
    wrong++;System.out.println("no ");
    System.out.println("right answer: "+(op1-op2));
    }
    }
    else if(Operator.equals("")){
    System.out.println(op1+"×"+op2+"=");
    boolean b =times(op1,op2);
    if(b == true){
    right++;System.out.println("right! ");
    }
    else{
    wrong++;System.out.println("no ");
    System.out.println("right answer: "+(op1
    op2));
    }
    }
    else{
    System.out.println(op1+"÷"+op2+"=");
    boolean b =Div(op1,op2);
    if(b == true){
    right++;System.out.println("right! ");
    }else{
    wrong++;System.out.println("no ");
    System.out.println("right answer: "+op1/op2);
    }
    }
    }
    System.out.println("做對了"+right+"道題.");
    System.out.println("做錯了"+wrong+"道題.");
    System.out.println("正確率為"+(right/(wrong+right))100+"%");
    }
    private static boolean add(double x,double y) {
    Scanner sc = new Scanner(System.in);
    double num1 = sc.nextDouble();
    double result = x + y;
    if(num1 == result){
    return true;
    }else{
    return false;
    }
    }
    private static boolean minus(double x,double y) {
    Scanner sc = new Scanner(System.in);
    double num1 = sc.nextDouble();
    double result = x - y;
    if(num1 == result){
    return true;
    }else{
    return false;
    }
    }
    private static boolean times (double x,double y) {
    Scanner sc = new Scanner(System.in);
    double num1 = sc.nextDouble();
    double result = x
    y;
    if(num1 == result){
    return true;
    }else{
    return false;
    }
    }
    private static boolean Div(double x,double y) {
    Scanner sc = new Scanner(System.in);
    double num1 = sc.nextDouble();
    double result =x/y;
    if(num1 == result){
    return true;
    }else{
    return false;
    }
    }
    }

  • whitestar運行圖
  • 碼雲連結
    https://gitee.com/BESTI-IS-JAVA-2018/20165303WeiYujava/tree/master/src

  • 結對感受
    通過這周的不斷修改代碼,不斷地和小夥伴爭論,終於修改出了基本實現功能的代碼,然後就是實現功能,但是在啟動並執行過程中還是有一些bug,不是太完美,由於時間緊張,本周的實驗比較多,然後實現的比較粗糙,我會在以後的學習過程中繼續實現這個代碼。

20165303 2017-2018-2 《Java程式設計》結對程式設計練習_四則運算

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.