Java 获得额外的'-';在重新排列代码片段之后

Java 获得额外的'-';在重新排列代码片段之后,java,Java,因此,我从第一本java书籍开始学习java,偶然发现了一个练习。我需要重新排列这些代码片段以获得如下输出: a-b c-d public class cc { public static void main(String [] args) { int x = 3; while (x > 0) { if (x == 2) { System.out.print("b c");

因此,我从第一本java书籍开始学习java,偶然发现了一个练习。我需要重新排列这些代码片段以获得如下输出:

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

}
代码片段包括:

if (x == 1) {
                System.out.print("d");
                x = x - 1
            }
所以我做了这样的事情:

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

}
我得到的结果是:

a-b c-d-

我做错了什么

你错过了一个
x=x-1编码到其中一个if语句中,并将print语句放在错误的位置:

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

    }
}

本练习的重点是理解循环(在本例中为while循环)、if语句和修改存储在变量中的值。为此,我还建议您查看数组并尝试生成所需的输出。例如,此特定案例将打印a-b c-d,但一般情况如何?如果你有一堆字符,看起来它们将被分成几对,每对字符之间用空格隔开,在任何给定字符对的每个元素之间有一个连字符

如果你有

String input = "abcd";
你要写什么才能得到

a-b c-d
产量

一种可能性是:

char[] chars = input.toCharArray();
int i = 0;
while (i < chars.length) {
    String separator;
    System.out.print(chars[i]);
    if (i == chars.length - 1) {
        separator = "\n";
    else if (i % 2 != 0) {
        separator = " ";
    } else {
        separator = "-";
    }
    System.out.print(separator);
    i++;
}
char[]chars=input.tocharray();
int i=0;
而(i<字符长度){
串分离器;
系统输出打印(字符[i]);
如果(i==字符长度-1){
分隔符=“\n”;
如果(i%2!=0)为else{
分隔符=”;
}否则{
分隔符=“-”;
}
系统输出打印(分隔符);
i++;
}
在这里查看您的代码,在循环的每次迭代之后,不管x的值是多少,它都会在输出后打印一个破折号。您还缺少中的
x=x-1;

if (x == 1) {
                System.out.print("d");
                x = x - 1; // you were missing this
            }
上面的if语句也应该放在下面

x = x - 1;
System.out.print("-");
这样我们就不会在检查条件之前通过设置
x==1
在最后添加一个不必要的-,这样我们就不会进行另一次迭代

把这些放在一起,我们得到了这个

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

    }
}
编辑:我还为您重新安排了if语句,因为
>2
出现在
==2
之前更有逻辑意义

public class cc {
    public static void main(String [] args) {
        int x = 3;
        while (x > 0) {
            if (x == 2) {
                System.out.print("b c");
            }
            if (x > 2) {
                System.out.print("a");
            }
            if (x == 1) {
                System.out.print("d");
            }
            x = x - 1;
            System.out.print("-"); // will run for every iteration of the loop
        }
        
    }

}
if (x == 1) {
                System.out.print("d");
                x = x - 1; // you were missing this
            }
x = x - 1;
System.out.print("-");
public class cc {
    public static void main(String [] args) {
        int x = 3;
        while (x > 0) {
            if (x > 2) {
                System.out.print("a");
            }
            if (x == 2) {
                System.out.print("b c");
            }
            x = x - 1;
            System.out.print("-");
            if (x == 1) {
                System.out.print("d");
                x = x - 1;
            }
        }

    }
}