为什么将正数相乘会得出负数值?

huangapple go评论149阅读模式
英文:

Why does multiplying positive numbers result in negative values?

问题

对于某些输入,例如5、10、10,输出中存在负值。这是一个通过相邻数字相乘来创建一系列数字的程序。我是一个Java初学者,请帮我相应地修改我的代码。

import java.util.*;
public class Program
{
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.println("请输入第一个数字");
        int a=in.nextInt();
        System.out.println("请输入第二个数字");
        int b=in.nextInt();
        System.out.println("请输入项数");
        int c=in.nextInt();
        if(a>0 && b>0 && c>0)
        {
            for(int i=0; i<c+2; i++)
            {
                System.out.println(a + ",");
                int mul=a*b;
                a=b;
                b=mul;
            }
        }
        else
        {
            System.out.println("无效输入");
        }
        
            
    }
}
请输入第一个数字
5
请输入第二个数字
10
请输入项数
10
5,
10,
50,
500,
25000,
12500000,
-1032612608,
-975265792,
1931476992,
0,
0,
0,
英文:

for certain inputs like 5,10,10, there are negative values in the output.this is a program in which series of numbers are created by multiplying adjacent numbers.I am a java beginner,please help me to modify my code accordingly.

import java.util.*;
public class Program
{
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.println(&quot;Enter first number&quot;);
        int a=in.nextInt();
        System.out.println(&quot;Enter the second number&quot;);
        int b=in.nextInt();
        System.out.println(&quot;Enter the no of terms&quot;);
        int c=in.nextInt();
        if(a&gt;0 &amp;&amp; b&gt;0 &amp;&amp; c&gt;0)
        {
            for(int i=0; i&lt;c+2; i++)
            {
                System.out.println(a + &quot;,&quot;);
                int mul=a*b;
                a=b;
                b=mul;
            }
        }
        else
        {
            System.out.println(&quot;Invalid Input&quot;);
        }
        
            
    }
}
Enter first number
5
Enter the second number
10
Enter the no of terms
10
5,
10,
50,
500,
25000,
12500000,
-1032612608,
-975265792,
1931476992,
0,
0,
0,

答案1

得分: 4

以下是翻译好的内容:

当一个 int 的值溢出时,它会从另一端重新开始,也就是说,一旦你越过了 int 可以持有的最大值,它将从 int 的最小限制开始。对于下溢也是一样的。请查看以下程序以更好地理解:

public class Program {

    public static void main(String[] args) {
        int x = Integer.MAX_VALUE;
        int y = Integer.MIN_VALUE;
        System.out.println(x);
        System.out.println(x + 1);
        System.out.println(y);
        System.out.println(y - 1);
    }
}

输出:

2147483647
-2147483648
-2147483648
2147483647

对于更大的整数计算,你应该使用 long。对于更大的整数计算,你需要使用 BigInteger

public class Program {

    public static void main(String[] args) {
        long x = Integer.MAX_VALUE + 1L;
        long y = Integer.MIN_VALUE - 1L;
        System.out.println(Integer.MAX_VALUE);
        System.out.println(x);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(y);
    }
}

输出:

2147483647
2147483648
-2147483648
-2147483649
英文:

Whenever the value of an int overflows, it starts from the other end i.e. once you cross the maximum value an int can hold, it will start from the minimum limit of int. The same happens for underflows as well. Check the following program to understand it better:

public class Program {

	public static void main(String[] args) {
		int x = Integer.MAX_VALUE;
		int y = Integer.MIN_VALUE;
		System.out.println(x);
		System.out.println(x + 1);
		System.out.println(y);
		System.out.println(y - 1);
	}
}

Output:

2147483647
-2147483648
-2147483648
2147483647

For bigger integer calculations, you should use long. For even bigger integer calculations, you need BigInteger.

public class Program {

	public static void main(String[] args) {
		long x = Integer.MAX_VALUE + 1L;
		long y = Integer.MIN_VALUE - 1L;
		System.out.println(Integer.MAX_VALUE);
		System.out.println(x);
		System.out.println(Integer.MIN_VALUE);
		System.out.println(y);
	}
}

Output:

2147483647
2147483648
-2147483648
-2147483649

huangapple
  • 本文由 发表于 2020年4月5日 20:55:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61042934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定