【问题标题】:JAVA - Dividing by Zero & Returning a Correct Answer = UndefinedJAVA - 除以零并返回正确答案 = 未定义
【发布时间】:2013-09-17 15:11:39
【问题描述】:

我该如何做到,所以当 Wholenumber2 = 0 时,它会显示一条不同的消息,然后如果 Wholenumber 是 但不是 = 0,它会显示另一条消息?我试图防止跳水的基本错误为 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);
    }

【问题讨论】:

  • 使用if语句判断wholenumber2 == 0

标签: java


【解决方案1】:

您可以使用try catch 处理(除以零)ArithmeticException 或者您可以检查wholenumber2 是否为零

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

检查this link 以及它建议处理这种情况的方法数

【讨论】:

    【解决方案2】:

    一个简单的方法是 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");
    }
    

    顺便说一句:作为对其他一些答案的评论,您应该避免使用异常来控制执行流程。

    【讨论】:

    • 这非常有效。这只是我在JAVA课堂编程的第二天,没有经验。我明白你现在在这里做了什么。我将来应该可以使用这些行。谢谢。
    【解决方案3】:

    我该如何做到,所以当 Wholenumber2 = 0 时,它会显示不同的消息,然后如果 Wholenumber 是 但不是 = 0,它将显示另一条消息?我试图防止跳水的基本错误为 0。

    一个简单的如果你需要在这里检查。

    if (wholenumber2  != 0) {
        // calculate   
    }else{
      //show error message
    }
    

    【讨论】:

      【解决方案4】:

      整数除以零得到ArithmeticException。所以你可以抓住它并显示你想要的消息。

      try{
           quotient = wholenumber1 / wholenumber2;
           System.out.printf( "Quotient   = %d", quotient);
      }catch(ArithmeticException ae){
           System.out.print( "Result is Undefined");
      }
      

      【讨论】:

        【解决方案5】:

        在请求输入时添加条件 ->

        int wholenumber2 = 0
        while (wholenumber2 < 1) {
            System.out.print( "Please enter second whole number..." );
            wholenumber2 = input.nextInt();
        }
        

        【讨论】:

        • 这将帮助您验证输入并防止此类情况发生。
        【解决方案6】:
        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;
            }
        }
        

        【讨论】:

          【解决方案7】:

          考虑到您同时进行所有操作,我 建议你一输入就检查 Wholenumber2 的值 即,

          Scanner input = new Scanner(System.in);
          
          int wholenumber2 = input.nextInt();
          
          while(wholenumber2==0)
          {
          System.out.println( "Whole Number cannot be zero!!" ); 
          System.out.print( "Please enter Second whole number..." );
          wholenumber2 = input.nextInt();
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-10-24
            • 1970-01-01
            • 1970-01-01
            • 2022-06-15
            • 2019-09-03
            • 2020-08-25
            • 2017-01-07
            • 1970-01-01
            相关资源
            最近更新 更多