重排代码片段后出现额外的 '-'。

huangapple go评论111阅读模式
英文:

Getting a extra '-' after rearranging the code snippets

问题

  1. 我从《Head First Java》这本书开始学习 Java,偶然间遇到了一个练习。我需要重新排列这些代码片段,以获得以下输出:

a-b c-d

  1. 代码片段如下:

if (x == 1) {
System.out.print("d");
x = x - 1;
}

if (x == 2) {
System.out.print("b c");
}

if (x > 2) {
System.out.print("a");
}

while (x > 0) {

x = x - 1;
System.out.print("-");

int x = 3;

  1. 所以我做了类似这样的操作:

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("-");
}
}
}

  1. 我得到的输出是:

a-b c-d-

  1. 我做错了什么?
英文:

So I started learning java from the Head First Java book and stumbled upon a exercise.I need to rearrange these code snippets to obtain a output like this:

  1. a-b c-d

The code snippets are:

  1. if (x == 1) {
  2. System.out.print("d");
  3. x = x - 1
  4. }
  1. if (x == 2) {
  2. System.out.print("b c");
  3. }
  1. if (x > 2) {
  2. System.out.print("a");
  3. }
  1. while (x > 0) {
  1. x = x - 1;
  2. System.out.print("-");
  1. int x = 3;

So I did something like this:

  1. public class cc {
  2. public static void main(String [] args) {
  3. int x = 3;
  4. while (x > 0) {
  5. if (x == 2) {
  6. System.out.print("b c");
  7. }
  8. if (x > 2) {
  9. System.out.print("a");
  10. }
  11. if (x == 1) {
  12. System.out.print("d");
  13. }
  14. x = x - 1;
  15. System.out.print("-");
  16. }
  17. }
  18. }

The output I am getting is :

  1. a-b c-d-

What did I do wrong

答案1

得分: 4

你在一个if语句中漏掉了一个x = x - 1;,并且把打印语句放错了位置:

  1. public class cc {
  2. public static void main(String [] args) {
  3. int x = 3;
  4. while (x > 0) {
  5. if (x == 2) {
  6. System.out.print("b c");
  7. }
  8. if (x > 2) {
  9. System.out.print("a");
  10. }
  11. x = x - 1;
  12. System.out.print("-");
  13. if (x == 1) {
  14. System.out.print("d");
  15. x = x - 1;
  16. }
  17. }
  18. }
  19. }
英文:

You missed one x = x - 1; in one of the if statements, and put the print statement in the wrong place:

  1. public class cc {
  2. public static void main(String [] args) {
  3. int x = 3;
  4. while (x > 0) {
  5. if (x == 2) {
  6. System.out.print("b c");
  7. }
  8. if (x > 2) {
  9. System.out.print("a");
  10. }
  11. x = x - 1;
  12. System.out.print("-");
  13. if (x == 1) {
  14. System.out.print("d");
  15. x = x - 1;
  16. }
  17. }
  18. }
  19. }

答案2

得分: 1

  1. public class cc {
  2. public static void main(String [] args) {
  3. int x = 3;
  4. while (x > 0) {
  5. if (x > 2) {
  6. System.out.print("a");
  7. }
  8. if (x == 2) {
  9. System.out.print("b c");
  10. }
  11. x = x - 1;
  12. System.out.print("-");
  13. if (x == 1) {
  14. System.out.print("d");
  15. x = x - 1;
  16. }
  17. }
  18. }
  19. }
英文:
  1. public class cc {
  2. public static void main(String [] args) {
  3. int x = 3;
  4. while (x > 0) {
  5. if (x == 2) {
  6. System.out.print("b c");
  7. }
  8. if (x > 2) {
  9. System.out.print("a");
  10. }
  11. if (x == 1) {
  12. System.out.print("d");
  13. }
  14. x = x - 1;
  15. System.out.print("-"); // will run for every iteration of the loop
  16. }
  17. }
  18. }

Looking at your code here, after each iteration of the loop regardless of the value of x, it will always print a dash after the output. You are also missing x = x - 1; from

  1. if (x == 1) {
  2. System.out.print("d");
  3. x = x - 1; // you were missing this
  4. }

The if statement above should also go below

  1. x = x - 1;
  2. System.out.print("-");

so that we don't add an unnecessary - at the end by setting x == 1 before the condition is checked so we don't go through another iteration.

Putting this all together we get this

  1. public class cc {
  2. public static void main(String [] args) {
  3. int x = 3;
  4. while (x > 0) {
  5. if (x > 2) {
  6. System.out.print("a");
  7. }
  8. if (x == 2) {
  9. System.out.print("b c");
  10. }
  11. x = x - 1;
  12. System.out.print("-");
  13. if (x == 1) {
  14. System.out.print("d");
  15. x = x - 1;
  16. }
  17. }
  18. }
  19. }

EDIT: I also rearranged the if statements for you as > 2 coming before == 2 makes more logical sense

答案3

得分: -1

以下是翻译好的部分:

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

因此,假设您有

  1. String input = "abcd";

您需要编写什么代码才能获得输出

  1. a-b c-d

其中一种可能性是以下内容

  1. char[] chars = input.toCharArray();
  2. int i = 0;
  3. while (i < chars.length) {
  4. String separator;
  5. System.out.print(chars[i]);
  6. if (i == chars.length - 1) {
  7. separator = "\n";
  8. else if (i % 2 != 0) {
  9. separator = " ";
  10. } else {
  11. separator = "-";
  12. }
  13. System.out.print(separator);
  14. i++;
  15. }
英文:

The point of the exercise is to understand loops (in this case a while loop), if statements and modifying the value stored in a variable. To that end I would suggest also looking at arrays and trying to generate the desired output. For example this specific case will print a-b c-d but what's the general case? If you have a bunch of characters it looks like they are to be broken into pairs where each pair is separated by a space and between each element of any given pair there is a hyphen.

So supposing you have

  1. String input = &quot;abcd&quot;;

What do you have to write to get

  1. a-b c-d

In output?

One possibility is the following

  1. char[] chars = input.toCharArray();
  2. int i = 0;
  3. while (i &lt; chars.length) {
  4. String separator;
  5. System.out.print(chars[i]);
  6. if (i == chars.length - 1) {
  7. separator = &quot;\n&quot;;
  8. else if (i % 2 != 0) {
  9. separator = &quot; &quot;;
  10. } else {
  11. separator = &quot;-&quot;;
  12. }
  13. System.out.print(separator);
  14. i++;
  15. }

huangapple
  • 本文由 发表于 2020年8月22日 02:08:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63527994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定