簡體   English   中英

混合分數計算器的ArithmeticException

[英]ArithmeticException with Mixed Fractions calculator

該程序可以正確計算混合分數。 輸入兩個零后,我希望while循環終止。 但是,當我輸入由空格分隔的兩個零時,我在線程“ main”中得到“ Exception” java.lang.ArithmeticException:/在MixedFractions.main等處為零。我只希望用戶不能為以下項輸入值a和be一旦它們為兩個變量輸入0,謝謝

import java.util.Scanner;
 public class MixedFractions  {
public static void main (String [] args)
{

    Scanner scan = new Scanner(System.in);
    int a = scan.nextInt(); int b = scan.nextInt();
    int c = Math.abs(a / b);
    int d = c * b;
    int e = c * b;
    int f = a - e;
    while ( a != 0 && b!= 0)
    {


        if(c == 0)
        {
            System.out.println(c + " " + a  + " / " + b);
        }
        else if(d == a)
        {
            a = 0;
            System.out.println(c + " " + a  + " / " + b);

        }
        else if( c != a)
        {
             e = c * b;
             f = a - e;
            System.out.println(c + " " + f  + " / " + b);
        }
        a = scan.nextInt(); b = scan.nextInt();
        c = Math.abs(a/b);
    }
}

}

試試這個:

public static void main (String [] args) {

    Scanner scan = new Scanner(System.in);
    int a = scan.nextInt(); 
    int b = scan.nextInt();
    while ( a != 0 && b!= 0) {
        int c = Math.abs(a / b);
        int d = c * b;
        if(c == 0) {
            System.out.println(c + " " + a  + " / " + b);
        } else if(d == a) {
            a = 0;
            System.out.println(c + " " + a  + " / " + b);
        } else if( c != a) {
            int e = c * b;
            int f = a - e;
            System.out.println(c + " " + f  + " / " + b);
        }
        a = scan.nextInt(); 
        b = scan.nextInt();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM