Java:边缘情况和添加两个数字的未来要求

时间:2014-02-04 13:44:16

标签: java

有一个面试问题,以找出添加两个数字的边缘情况和未来要求 添加两个号码

public static void main(String[] args) {
    // TODO Auto-generated method stub


    System.out.println("First number: ");
    Scanner input = new Scanner(System.in);

    int a = input.nextInt(); 

    System.out.println("Second number: ");
    int b = input.nextInt();  

    int sum = a+b;

    System.out.println("Result: "+sum); 
}

边缘情况1
空检查。任何输入数字都可以为空。

Edge Case 2
输入数字很大。

最终计划 哪个将使用BigInteger而不是整数。

BigInteger first = new BigInteger(number1);
BigInteger second = new BigInteger(number2);

这里可以考虑其他任何边缘情况或未来要求吗?

1 个答案:

答案 0 :(得分:1)

测试用例可分为两部分:

完整性检查(空或非数字字段) - 这不包含在您的边缘案例中!

因此,您的Edge Case 1 - 不是边缘案例

您的Edge Case 2是一个有效的案例,但它没有指明什么是大号。

Now,您使用基本类型来存储Number1Number2的值,并对基本类型进行算术运算。因此,我们只考虑“原始类型”的评估

根据定义:Edge cases are inputs that the specification for your function allows but that might be tricky to handle. - Josh Zimmerman

在我看来,这两个案例是考虑的;

请记住:The Java virtual machine does not indicate overflow during operations on integer data types.

Overflow Conditions / Information Loss in Arithmetic : (For int, from -2147483648 to 2147483647 (-231 to 231-1), inclusive)

1. Addition of two large negative integer numbers causing an overflow 
2. Addition of two large positive integer number causing an overflow 
3. Addition of two opposite signed numbers 
4. Addition of X =0 and Y=0


Large input where x < MIN INT or > MAX INT on JAVA Platform 

1. Handling LARGE (-/+) numbers as input - can be termed as INFINITE ? How will the system behave ?