Home>

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20



91 92 93 94 95 96 97 98 99 100

I want to output an integer from 1 to 100 so that the execution result is as above. In addition, I want to use the while statement only once. (Put a space so that the numbers are neatly aligned.)

public static void main (String [] args) {
           int n = 1;
           while (n<10) {
              System.out.print ("" + n);
              n ++;
           }
           System.out.println ("" + n);
           n ++;
           while (n<100) {
               while ((n% 10)! = 0) {
                   System.out.print ("" + n);
                   n ++;
               }
               System.out.println ("" + n);
               n ++;
           }
       }
  }


I want to make this a program that uses a while statement only once.

  • Answer # 1

    public static void main (String [] args) {
        while (false) {}
        System.out.println ("1 2 3 4 5 6 7 8 9 10");
        System.out.println ("11 12 13 14 15 16 17 18 19 20");
        System.out.println ("21 22 23 24 25 26 27 28 29 30");
        System.out.println ("31 32 33 34 35 36 37 38 39 40");
        System.out.println ("41 42 43 44 45 46 47 48 49 50");
        System.out.println ("51 52 53 54 55 56 57 58 59 60");
        System.out.println ("61 62 63 64 65 66 67 68 69 70");
        System.out.println ("71 72 73 74 75 76 77 78 79 80");
        System.out.println ("81 82 83 84 85 86 87 88 89 90");
        System.out.println ("91 92 93 94 95 96 97 98 99 100");
    }

    In addition, even if one of twowhileis rewritten tofor,whileis used only once Is true.

    Well, I answered the question because it matches, but I don't think I want this kind of answer myself.
    However, I don't know what kind of answer I'm asking for this question.
    If you ask for an answer as it is, I think it will only help you.
    If you do not want such an answer, please clearly indicate what kind of answer you want.

  • Answer # 2

    Hello

    I will answer one proposal.

    class Main {
      public static void main (String [] args) {
        int n = 1;
        String [] a = new String [10];
        while (n<= 100) {
          a [(n-1)% 10] = String.format ("% 2d", n);
          if (n% 10 == 0) {
            System.out.println (String.join ("", a));
          }
          n ++;
        }
      }
    }

    Repl.it for operation check:https://repl.it/@jun68ykt/Q224913

  • Answer # 3

    int n = 0;
    while (++ n<= 100)
       System.out.printf ("% 4d" + (n% 10 == 0? "% N": ""), n);

    Enter a line feed to output a multiple of 10 with 4 digits,
    Only an image because it is no test with tablet input.

  • Answer # 4

    Are there any issues? The following are examples of answers.

    public class Main {
        public static void main (String [] args) {
            int n = 0;
            while (n ++<= 100) {
                if (n% 10 == 0) {
                    System.out.println ("" + n);
                    n ++;
                }
                System.out.print ("" + n);
            }
        }
    }
    1 2 3 4 5 6 7 8 9 10
     11 12 13 14 15 16 17 18 19 20
     21 22 23 24 25 26 27 28 29 30
     31 32 33 34 35 36 37 38 39 40
     41 42 43 44 45 46 47 48 49 50
     51 52 53 54 55 56 57 58 59 60
     61 62 63 64 65 66 67 68 69 70
     71 72 73 74 75 76 77 78 79 80
     81 82 83 84 85 86 87 88 89 90
     91 92 93 94 95 96 97 98 99 100