使用 40 个元素的数字数组的 HugeInteger

HugeInteger using 40-element array of digits

提问人:julio11 提问时间:5/25/2023 更新时间:5/25/2023 访问量:94

问:

我正在挑战,当我运行编译器时,我不断遇到错误。我是java的新手,我看不出有什么问题。我不是在寻找答案,而是更多地澄清我需要寻找的地方。

我将创建一个类 HugeInteger,它使用 40 个元素的数字数组来 存储每个整数大至 40 位数字。提供 parse、toString、add 和 subtract 方法。方法 parse 应该接收一个 String,使用 charAt 方法提取每个数字并放置整数 等效于整数数组中的每个数字。要比较 HugeInteger 对象,请提供以下信息 方法:isEqualTo、isNotEqualTo、isGreaterThan、isLessThan、isGreaterThanOrEqualTo 和 isLessThanOrEqualTo。其中每个方法都是一个谓词方法,如果关系 在两个 HugeInteger 对象之间保持,如果关系不成立,则返回 false。提供 谓词方法为 Zero。

public class HugeInteger{
  private int[] intArray;
  private int numDigits;  // stores the number of digits in intArray
  //static String ss;

  public HugeInteger(String s){
    intArray = new int[40];
    numDigits = 0;
    parse(s);// call parse(s)
  }

  public HugeInteger( ){
    intArray = new int[40];
    numDigits = 0;
    
  }
  
  public void parse(String s){
     // Add each digit to the arrays
     // update numDigits
     for (int i = 0; i < s.length(); i++){
         intArray[i] = s.charAt(i) - '0';
         System.out.print(intArray[i] + "");
         numDigits++;
         System.out.print(intArray[i] + "" + numDigits);
      }
  }

  public static HugeInteger add(HugeInteger hugeInt1, HugeInteger hugeInt2){
     // Create hugeInt3
     // Loop 
     //    Add digits from hugeInt1 and hugeInt2, 
     //    Store in corresponding hugeInt3
     // End
     //
     // return hugeInt3  
      HugeInteger hugeInt3 = new HugeInteger();
      for (int i = 0; i < hugeInt3.intArray.length; i++){
      hugeInt3.intArray[i] = hugeInt1.intArray[i] + hugeInt2.intArray[i];
  }
      String data = hugeInt3.intArray.toString();
      for (int j = 0; j < data.length(); j++)
          System.out.println(hugeInt3.intArray[j]);
      return hugeInt3;
  }

  public static HugeInteger subtract(HugeInteger hugeInt1, HugeInteger hugeInt2){
     // Create hugeInt3
     // Loop 
     //    Subtract hugeInt2 digit from  hugeInt1, 
     //    Store in corresponding hugeInt3
     // End
     //
     // return hugeInt3  
      HugeInteger hugeInt4 = new HugeInteger();
      for (int i = 0; i < hugeInt1.intArray.length; i++){
          if (hugeInt1.intArray[i] > hugeInt2.intArray[i])
              hugeInt4.intArray[i] = hugeInt1.intArray[i] - hugeInt2.intArray[i];
          else
              hugeInt4.intArray[i] = hugeInt2.intArray[i] - hugeInt1.intArray[i];
      }
      
      String data = hugeInt4.intArray.toString();
      for (int j = 0; j < data.length(); j++)
          System.out.println(hugeInt4.intArray[j]);
      return hugeInt4;

  }

  public static boolean isEqualTo(HugeInteger hugeInt1, HugeInteger hugeInt2){
    // return true if the value represented by 
    // elements of hugeInt1.intArray is equal to
    // value represented by elements of hughInt2.intArray
      for (int i = 0; i < hugeInt2.intArray.length; i++)
          System.out.println(hugeInt2.intArray[i]);
      int count = 0;
      
      for (int i = 0; i < hugeInt2.numDigits; i++){
          int a1= hugeInt1.intArray[i];
          int a2 = hugeInt2.intArray[i];
          System.out.println(a1 + " " + a2);
          if (a1 != a2){
              System.out.println(hugeInt2.intArray[i]);
          count++;
      }
      }
      
      System.out.println("count" + count);
      return count <= 0;
  }

  public static boolean isNotEqualTo(HugeInteger hugeInt1, HugeInteger hugeInt2){
    // return true if the value represented by 
    // elements of hugeInt1.intArray is not equal to
    // value represented by elements of hughInt2.intArray
      
      for (int i = 0; i < hugeInt2.intArray.length; i++)
          System.out.println(hugeInt2.intArray[i]);
      int count = 0;
      
      for (int i = 0; i < hugeInt2.numDigits; i++){
      int a1 = hugeInt1.intArray[i];
      int a2 = hugeInt2.intArray[i];
      System.out.println(a1 + " " + a2);
      if (a1 != a2){
          System.out.println(hugeInt2.intArray[i]);
          count++;
      }
  }
      System.out.println("count" + count);
      return count > 0;
  }

  public static boolean isGreaterThan(HugeInteger hugeInt1, HugeInteger hugeInt2){
    // return true if the value represented by 
    // elements of hugeInt1.intArray is greater than 
    // value represented by elements of hughInt2.intArray
      int sum1 = 0;
      int sum2 = 0;
      for (int i = 0; i < hugeInt1.intArray.length; i++){
          sum1 += hugeInt1.intArray[i];
          sum2 += hugeInt1.intArray[i];
      }
      
      return sum1 > sum2;
      
  }

  public static boolean isLessThan(HugeInteger hugeInt1, HugeInteger hugeInt2){
    // return true if the value represented by 
    // elements of hugeInt1.intArray is less than
    // value represented by elements of hughInt2.intArray
      
      int sum1 = 0;
      int sum2 = 0;
      for (int i = 0; i < hugeInt1.intArray.length; i++){
          sum1 += hugeInt1.intArray[i];
          sum2 += hugeInt1.intArray[i];
      }
      return sum1 >= sum2;
  }

  public static boolean isGreaterThanOrEqualTo(HugeInteger hugeInt1, HugeInteger hugeInt2){
    // return true if the value represented by 
    // elements of hugeInt1.intArray is greater than or equal to
    // value represented by elements of hughInt2.intArray
      int sum1 = 0;
      int sum2 = 0;
      for (int i = 0; i < hugeInt1.intArray.length; i++){
          sum1 += hugeInt1.intArray[i];
          sum2 += hugeInt1.intArray[i];
      }
      return sum1 >= sum2;
  }

  public static boolean isZero(HugeInteger hugeInt1 ){
    // return true if the value represented by 
    // elements of hugeInt1.intArray is 0 
      int count = 0;
              for (int i = 0; i < hugeInt1.intArray.length; i++){
                  if (hugeInt1.intArray[i]==0)
                      count++;
              }
              
      return count > 0;
  }

  public String toString( ){
     // return string representation of this object
     return toString();
  }
  
  public static void main(String[] args){
      HugeInteger h = new HugeInteger("abc");
      HugeInteger h2 = new HugeInteger("xyz");
      HugeInteger h3 = new HugeInteger();
      HugeInteger hall = new h3.add(h,h2);
      boolean b = HugeInteger.isNotEqualTo(h,h2);
      System.out.println(b);
      boolean iszero = HugeInteger.isZero(h);
      System.out.println(iszero);
      System.out.println(HugeInteger.isEqualTo(h,h2));
      System.out.println(HugeInteger.isNotEqualTo(h,h2));
      System.out.println(HugeInteger.isGreaterThan(h,h2));
      System.out.println(HugeInteger.isGreaterThanOrEqualTo(h,h2));
      System.out.println(HugeInteger.isLessThan(h,h2));
      
     }      
}

我认为当我测试它时会出现这个问题,更具体地说是这里: HugeInteger hall = new h3.add(h,h2);

Java 数组

评论

1赞 rzwitserloot 5/25/2023
“问题”——什么问题?编译器错误?粘贴它。运行时错误?粘贴堆栈跟踪。一个你没想到的答案?粘贴该答案并粘贴您想看的内容。
1赞 rzwitserloot 5/25/2023
new h3.add不是合法的 java,我很难理解你认为这可能做了什么。 是。但是一种静态方法。您可能想要而不是两行 ( 和 .这可能不是此代码中唯一的错误。少写,多测试。new HugeInteger()addHugeInteger h3 = HugeInteger.add(h, h2);HugeInteger h3 = ...HugeINteger hall = ...
1赞 aled 5/25/2023
请阅读 stackoverflow.com/help/how-to-ask
1赞 Reilas 5/25/2023
您希望完成的任务是什么?应该是 和 的总和 ?hall = new h3.add(h,h2)hallhh2
1赞 Ishan 5/25/2023
根本没有必要定义。 是一种静态方法。因此,它可以在没有对象的情况下直接调用。是的,是完全无效的 - 编译错误的来源。 就够了。删除这 2 行并仅用这一行替换这些行可以解决编译错误。HugeInteger h3 = new HugeInteger()HugeInteger.add()HugeInteger hall = new h3.add(h,h2)HugeInteger hall = HugeInteger.add(h, h2);

答: 暂无答案