1 java
public static void gramstoAtoms()
{
System.out.println("Enter Amount of grams");
Scanner keyboard = new Scanner(System.in);
long x = keyboard.nextLong();
System.out.println("Enter Unit Grams");
long y = keyboard.nextLong();
long result = x/y;
long answer = result*60200000000000000000000;
System.out.println(answer + "Atoms");
}
Run Code Online (Sandbox Code Playgroud)
如何更改此代码,以便我不会得到一个整数太长的错误?
只需使用BigInteger而不是long
BigInteger bi=new BigInteger("6020000000000000000000");
Run Code Online (Sandbox Code Playgroud)
对于你的方法:
public static void gramstoAtoms()
{
System.out.println("Enter Amount of grams");
Scanner keyboard = new Scanner(System.in);
String x = keyboard.nextLine();
System.out.println("Enter Unit Grams");
String y = keyboard.nextLine();
BigInteger result = new BigInteger(x).divide(new BigInteger(y));
BigInteger answer = result.multiply(new BigInteger("60200000000000000000000"));
System.out.println(answer + "Atoms");
}
Run Code Online (Sandbox Code Playgroud)
如果你x和y小,你可以使用简单的长,但乘以602 ....... 0000使用BigInteger
| 归档时间: |
|
| 查看次数: |
1098 次 |
| 最近记录: |