-2

整数 2 = ゼロの場合に別のメッセージが表示されるようにするにはどうすればよいですか? 素潜りの基本エラーを0で防ごうとしています。

package SumDifProPACKAGE;     // Assigns package code.
import java.util.Scanner;     // Program uses class Scanner.
public class SumDifProCLASS { // Public class.
    public static void main( String[] args ){
      // Displays Welcome, information on calculations & creators name.
        System.out.printf( "%s\n%s\n%s\n",
         "--Welcome to JAVA Calculator!--", " --Sum, Difference, Product--", "  --??--");
        // Creates a Scanner to obtain input from the command window.
       Scanner input = new Scanner( System.in );
        // Listed integers:
        int wholenumber1; // First whole number input.
        int wholenumber2; // Second whole number input.
        int sum;          // Sum of First & Second whole number input.
        int difference;   // Difference of First & Second whole number input.
        int product;      // Product of First & Second whole number input.
        int quotient;     // Quotient of First & Second whole number input.
        int remainder;    // Remainder of the Quotient.
        int zero = 0;
        // Requests input for First whole number.
        System.out.print( "Please enter first whole number..." );
        wholenumber1 = input.nextInt();
        // Requests input for Second whole number.
        System.out.print( "Please enter first whole number..." );
        wholenumber2 = input.nextInt();
        // Displays the sum of First & Second input.
        sum = wholenumber1 + wholenumber2;
        System.out.printf( "Sum        = %d\n", sum);
        // Displays the difference of First & Second input.
        difference = wholenumber1 - wholenumber2;
        System.out.printf( "Difference = %d\n", difference);
        // Displays the product of the First & Second input.
        product = wholenumber1 * wholenumber2;
        System.out.printf( "Product    = %d\n", product);
        // Displays the quotient without the remainder of the First & Second input.   
        quotient = wholenumber1 / wholenumber2;
        System.out.printf( "Quotient   = %d", quotient);
       // Displays the remainder, continuation of quotient.
        remainder = wholenumber1 % wholenumber2;
        System.out.printf( "r%d\n", remainder);
    }
4

7 に答える 7

1

処理するために使用できますtry catch(ゼロで割る)か、ゼロかArithmeticExceptionどうかを確認できますwholenumber2

if(wholenumber2==0){
    // handle the case
} else{
    // handle the else case
}

このリンクも確認してください。この状況を処理する方法がいくつか提案されています

于 2013-09-17T05:33:16.133 に答える
0

整数 2 = ゼロの場合に別のメッセージが表示されるようにするにはどうすればよいですか? 素潜りの基本エラーを0で防ごうとしています。

ここで必要な単純な If チェック。

if (wholenumber2  != 0) {
    // calculate   
}else{
  //show error message
}
于 2013-09-17T05:32:30.900 に答える
0

整数をゼロで除算すると、ArithmeticExceptionが返されます。したがって、それをキャッチして、必要なメッセージを表示できます。

try{
     quotient = wholenumber1 / wholenumber2;
     System.out.printf( "Quotient   = %d", quotient);
}catch(ArithmeticException ae){
     System.out.print( "Result is Undefined");
}
于 2013-09-17T05:33:12.387 に答える
0

簡単な方法の 1 つは、if ステートメントです。

// Displays the quotient without the remainder of the First & Second input.   
if(wholenumber2 != 0){
    quotient = wholenumber1 / wholenumber2;
    System.out.printf( "Quotient   = %d", quotient);
}
else{
     System.out.printf("Cannot divide by zero");
}

ところで:他のいくつかの回答へのコメントとして、例外を使用して実行フローを制御することは控えてください。

于 2013-09-17T05:35:54.547 に答える
0
Long division_operation (Long a, Long b) throws Exception
{
    Long t = new Long (1);

    try
    {
        t = (a.longValue() / b.longValue());
    }
    catch (ArithmeticException e)
    {
        t = null;
    }
    finally
    {
        return t;
    }
}
于 2013-09-17T05:49:49.847 に答える