java使用BigInteger和While

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4146323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:04:09  来源:igfitidea点击:

java using BigInteger and While

java

提问by user503409

I have a problem

我有个问题

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);

}      
}
}
}

I've tried this formula BigInteger cis = new BigInteger("a");for this cis = a ;

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

and while ( cis.compareTo( nul ) > 0 );for this while ( cis > 0 );

while ( cis.compareTo( nul ) > 0 );while ( cis > 0 );

but it doesn't work and I don't know why.

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

When I use this formula, this is the same, but I used only integer when I use the same for Big Integer it doesn't work

当我使用这个公式时,这是相同的,但是当我将相同的用于 Big Integer 时,我只使用了 integer 它不起作用

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);    
}
}
}
}

采纳答案by jjnguy

To declare cisand store ainto cissee the following:

要声明cis并存储acis以下内容中:

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

Assuming this code is the main cause of your problems, and assuming cisis a BigInteger:

假设此代码是您问题的主要原因,并且假设cisBigInteger

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

This should instead be: (This assumes everything is a BigInteger.

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

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


The following code runs for me:

以下代码为我运行:

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);
        }
    }
}

It doesn't produce the neatest results. But it runs.

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

回答by Neil

Wow, that's hard to read.
From what I've gathered, cis isn't declared. I assume you meant to have BigInteger cis; up there somewhere. Please be more specific. What type of problem are you receiving? Compilation error? Problem in what the program actually does?

哇,这很难读。
根据我收集的信息,未声明 cis 。我假设你的意思是 BigInteger cis; 那里的某个地方。请更具体。您遇到什么类型的问题?编译错误?程序实际做什么的问题?