英文:
Getting a extra '-' after rearranging the code snippets
问题
我从《Head First Java》这本书开始学习 Java,偶然间遇到了一个练习。我需要重新排列这些代码片段,以获得以下输出:
a-b c-d
代码片段如下:
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;
所以我做了类似这样的操作:
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-
我做错了什么?
英文:
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:
a-b c-d
The code snippets are:
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;
So I did something like this:
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("-");
}
}
}
The output I am getting is :
a-b c-d-
What did I do wrong
答案1
得分: 4
你在一个if语句中漏掉了一个x = x - 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");
}
x = x - 1;
System.out.print("-");
if (x == 1) {
System.out.print("d");
x = x - 1;
}
}
}
}
英文:
You missed one x = x - 1;
in one of the if statements, and put the print statement in the wrong place:
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;
}
}
}
}
答案2
得分: 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;
}
}
}
}
英文:
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
}
}
}
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
if (x == 1) {
System.out.print("d");
x = x - 1; // you were missing this
}
The if statement above should also go below
x = x - 1;
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
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;
}
}
}
}
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,但一般情况是什么呢?如果您有一堆字符,看起来它们应该被分成成对的元素,其中每对元素由空格分隔,任何给定对的每个元素之间都有一个连字符。
因此,假设您有
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++;
}
英文:
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
String input = "abcd";
What do you have to write to get
a-b c-d
In output?
One possibility is the following
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++;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论