gpt4 book ai didi

java 使用 BigInteger 和 While

转载 作者:搜寻专家 更新时间:2023-11-01 03:44:52 25 4
gpt4 key购买 nike

我有一个问题

import java.math.BigInteger;
import java.io.*;
import java.util.*;
import java.lang.*;

public class medici {
public static void main(String[] arg) {
{

BigInteger zac = new BigInteger("3");
zac = zac.pow(399);

BigInteger kon = new BigInteger("3");
kon = kon.pow(400);

BigInteger nul = new BigInteger("0");
BigInteger jed = new BigInteger("1");
BigInteger detel = new BigInteger("3");


for (BigInteger a = zac; a.compareTo( kon ) <= 0; a = a.add(jed)) {


cis = a ; // THIS A PROBLEM
String retez = "";

while ( cis > 0 ); // THIS IS A PROBLEM

retez = ( cis.mod(detel) ) + retez;

cis = cis.divide(detel);

System.out.println(retez);

}
}
}
}

我试过这个公式 BigInteger cis = new BigInteger("a"); 用于 cis = a ;

while ( cis.compareTo( nul ) > 0 ); 对于这个 while ( cis > 0 );

但它不起作用,我不知道为什么。

当我使用这个公式时,这是相同的,但是当我对大整数使用相同的公式时,我只使用整数它不起作用

import java.io.*;
import java.util.*;
import java.lang.*;

public class netik {
public static void main(String[] arg) {
{
int a ;
int cis;
int detel = 3;

for ( a = 567880; a <= 567890; a++ ){

cis = a;

String retez = "";

while (cis > 0) {

retez = (cis % detel) + retez;

cis /= detel;

}
System.out.println(retez);
}
}
}
}

最佳答案

要声明 cis 并将 a 存储到 cis 中,请参见以下内容:

BigInteger cis = new BigInteger(""+a);

假设这段代码是导致问题的主要原因,并且假设 cis 是一个 BigInteger:

while (cis > 0)  {
retez = (cis % detel) + retez;
cis /= detel;
}

这应该是:(假设一切都是 BigInteger

while (cis.compareTo(new BigInteger("0")) > 0)  {
retez = (cis.mod(detel)).add(retez);
cis = cis.divide(detel);
}

以下代码为我运行:

public static void main(String[] arg) {

BigInteger zac = new BigInteger("3");
zac = zac.pow(399);

BigInteger kon = new BigInteger("3");
kon = kon.pow(400);

BigInteger nul = new BigInteger("0");
BigInteger jed = new BigInteger("1");
BigInteger detel = new BigInteger("3");

for (BigInteger a = zac; a.compareTo(kon) <= 0; a = a.add(jed)) {

BigInteger cis = a; // THIS A PROBLEM
String retez = "";

while (cis.compareTo(new BigInteger("0")) >= 0) {
retez = (cis.mod(detel)) + retez;
cis = cis.divide(detel);
System.out.println(retez);
}
}
}

它不会产生最整洁的结​​果。但它运行。

关于java 使用 BigInteger 和 While,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4146323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com