jeudi 2 décembre 2021

Finding value for ABC

class Main {
  public static void main(String[] args) {
    int abc;
    int total = 1000;
    for (abc = 220000000; abc < 240000000; abc++) {
      String string1 = Integer.toString(abc);
      int a, b, c;
      a = 0;
      b = 0;
      c = 0;
      int a2 = squared(a);
      int b2 = squared(b);
      int c2 = squared(c);
      a = Integer.parseInt(string1.substring(0, 3));
      b = Integer.parseInt(string1.substring(3, 6));
      c = Integer.parseInt(string1.substring(6, 9));
      if (a < b && a < c && b < c && a2 < b2 && b2 < c2 && a + b + c == total) {
        System.out.println("The answer is " + abc);
      } else {
        System.exit(0); // testing
      }
    }
  }

  public static int squared(int x) {
    return (x * x);
  }
}

This is my code. I am trying to have the program print the value of abc so that a is less than b which is less than c. And where a squared is less than b squared which is less than c squared. Also, a+b+c must equal 1000. I tried making abc one whole value and using substrings to differ between a, b, and c. An example would be like 224,356,446. An with the substrings it would make a=224, b=356, and c=446. When I run the program, nothing is being printed. The else statement is being invoked because the system exits. Can anyone help?

Aucun commentaire:

Enregistrer un commentaire