英文:
I can't insert more than 10 digit of integer type of data in java
问题
以下是您要翻译的内容:
我在我的代码中遇到了一个问题。如何插入超过10位数的整数,我已经搜索过,可以使用BigInteger来实现,但我不知道如何在我的代码中使用"System.in"。
package t3;
import java.util.Scanner;
import java.math.BigInteger;
import java.util.Date;
public class t3 {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date.toString());
BigInteger NIM; // 使用BigInteger来存储大整数
Scanner input = new Scanner(System.in);
System.out.print("插入 nim:");
NIM = input.nextBigInteger(); // 使用nextBigInteger() 方法读取大整数
input.close();
System.out.print(NIM);
}
}
有人可以帮帮我吗?在此之前谢谢。
这是我的代码
这是我的错误
英文:
I had a problem by my code. How to insert more than 10 digits of integer, I have searched and can be done by using BigInteger, but I don't know how to use it in my code using "System.in"
package t3;
import java.util.Scanner;
import java.util.Date;
public class t3 {
public static void main(String[]args){
Date date= new Date();
System.out.println(date.toString());
int NIM;
Scanner input = new Scanner(System.in);
System.out.print("insert nim: ");
NIM = input.nextInt();
input.close();
System.out.print(NIM);
}
}
can someone help me? thank you before
This is my code
This my error
答案1
得分: 0
好的,以下是您要求的翻译内容:
如果需要的数字位数在18位或以下,只需切换到 long 类型:
long x = scanner.nextLong();
如果需要的位数超过18位,切换到 BigInteger 类型:
BigInteger x = new BigInteger(scanner.next());
请注意,BigInteger 不是原始类型,不支持运算符。所以,如果您有两个 BigInteger,想要将它们相加,应该这样写:
BigInteger a = a.add(b);
// 而不是:
// a = a + b
如果您想将一个整数添加到 BigInteger,是不可以的;相反地,将整数转换为 BigInteger,然后再进行相加。因此:
BigInteger a = new BigInteger(scanner.next());
a = a.add(BigInteger.ONE); // 这将1相加
a = a.add(new BigInteger("5")); // 这将5相加。
英文:
Well, how many digits do you need? If it is 18 or less, just switch to long:
long x = scanner.nextLong();
If you need more than that, switch to BigInteger:
BigInteger x = new BigInteger(scanner.next());
Note that BigInteger is not a primitive and does not support operators. So, if you have 2 bigintegers and you want to add them together, it's:
BigInteger a = a.add(b);
// and not:
a = a + b
If you want to add an integer to a BigInteger, you can't; instead, convert the integer to a BigInteger and THEN add it. Thus:
BigInteger a = new BigInteger(scanner.next());
a = a.add(BigInteger.ONE); // this adds 1 to
a = a.add(new BigInteger("5")); // this adds 5.
答案2
得分: 0
因为int
的最大值限制为10位数字。您可以使用long
,因为它可以增加到19位数字。下面列出了Integer
和Long
的最大和最小值之间的差异。
Integer.MIN_VALUE: -2,147,483,648
Integer.MAX_VALUE: 2,147,483,647
Long.MIN_VALUE: -9,223,372,036,854,775,808
Long.MAX_VALUE: 9,223,372,036,854,775,807
在您的代码中,只需简单地替换为以下内容。
// int NIM; -- 您可以将其替换为 long
long NIM;
// NIM = input.nextInt(); -- 调用 nextLong 方法代替。
NIM = input.nextLong();
英文:
Simply because int
max value is limited to 10 digits. You can use long
instead as you can add upto 19 digits. Listed below is the difference of Integer
and Long
max and min values.
Integer.MIN_VALUE: -2 147 483 648
Integer.MAX_VALUE: 2 147 483 647
Long.MIN_VALUE: -9 223 372 036 854 775 808
Long.MAX_VALUE: 9 223 372 036 854 775 807
In your code, just simply replace with the following.
// int NIM; -- you can replace it into long
long NIM;
// NIM = input.nextInt(); -- call method nextLong instead.
NIM = input.nextLong();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论