完成的任务然而问题与while循环给我错误的答案

Dev*_*ted 0 java

所以我在最近购买的这本书中完成了一些任务,基本上我有一定数量的代码,我必须重新排列它们以便给我以下输出:ab cd

这是代码: 在此输入图像描述您无法更改任何代码只能重新安排它.如果你认为这是不可能的,那么告诉我你将如何使用相同的原则来做到这一点.

这也是相同的代码:

public class carl {
  public static void main(String args[]){
    int x = 3;
    if (x > 2) {
        System.out.print("a");
    }
    while (x > 0){
    x = x - 1;
    System.out.print("-");
    }
    if (x == 2){
        System.out.print("b c");
    }
    if (x == 1){
        System.out.print("d");
        x = x - 1; 
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

让我知道你认为我做错了什么.

我一直在这样做 - 做到这一点.

Ell*_*sch 6

基于你的惊喜,我想你想要

  int x = 3;
  if (x > 2) {
    System.out.print("a");
  }
  while (x > 0) {
    x = x - 1;
    System.out.print("-");
    // } // <-- move this
    if (x == 2) {
      System.out.print("b c");
    }
    if (x == 1) {
      System.out.print("d");
      x = x - 1;
    }
  } // <-- to here.
Run Code Online (Sandbox Code Playgroud)

它会打印出来

a-b c-d
Run Code Online (Sandbox Code Playgroud)