for loop to while loop

后端 未结 4 986
北海茫月
北海茫月 2021-01-17 01:53

I am using a for loop to print the backwards alphabet in uppercase but I would like to know how to do the same thing with a while loop, which I am

4条回答
  •  北海茫月
    2021-01-17 02:09

    Try this:

    public static void main(String[] args) {
      char[] alphabet =  "abcdefghijklmnopqstuvwxyz".toCharArray();
      int index = alphabet.length - 1;
    
      while (index >= 0) {
        System.out.println(alphabet[index--]);
      }
    }
    

    This is a most efficient solution with minimal memory overhead.

提交回复
热议问题