英文:
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("Enter first number");
int a=in.nextInt();
System.out.println("Enter the second number");
int b=in.nextInt();
System.out.println("Enter the no of terms");
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("Invalid Input");
}
}
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论