英文:
Java Programming - Coding based on flowchart
问题
我刚开始学习编程,希望有经验的专家能提供建议。
根据所给的流程图,我现在的方向正确吗?
如何改进我的代码以确保稳健性?
**流程图**
![点击此处查看流程图][1]
**代码**
// r代表行,c代表列
int r = 1, c = 1;
do {
if (r <= 4)
{
if (c <= 10)
{
System.out.print("*");
c += 1;
}
else {
r += 1;
c = 1;
System.out.println();
}
}
else {
System.exit(1);
}
}while (c != 12);
**输出**
**********
**********
**********
**********
[1]: https://i.stack.imgur.com/bV4AO.png
英文:
I am new to programming and would appreciate any experts to provide suggestions.
Based on the given flowchart, am I on the right track?
How do I make improvement to my code to ensure robustness?
Flowchart
Code
// r is row, c is column
int r = 1, c = 1;
do {
if (r <= 4)
{
if (c <= 10)
{
System.out.print("*");
c += 1;
}
else {
r += 1;
c = 1;
System.out.println();
}
}
else {
System.exit(1);
}
}while (c != 12);
Output
**********
**********
**********
**********
答案1
得分: 2
while (c != 12);
流程图中没有这个条件。
注意你的 `if (r <= 4)` 语句直接嵌套在 `do...while` 中。这意味着你可以简化为一个单独的 `while`:
```python
while (r <= 4) {
...
}
同样地,c <= 10
应当使用循环来实现,而不仅仅是一个 if
语句。
流程图中没有要求像 System.out.println();
那样另起一行。所以从字面上理解,这一行是不正确的。然而,我怀疑流程图忽略了这个细节,存在错误。
<details>
<summary>英文:</summary>
while (c != 12);
The flow chart doesn't have this condition.
Notice how your `if (r <= 4)` statement is directly nested in the `do...while`. This means you can reduce to a single `while`:
while (r < = 4) {
...
}
Similarly `c <= 10` should be implemented as a loop instead of just an `if`.
The flow chart doesn't say to do start a new line like you do with `System.out.println();`. So taken literally, this line is incorrect. However, I suspect the flow chart omitted this detail and is in error.
</details>
# 答案2
**得分**: 1
根据提出的算法,可以看出其代码遵循了指南。\n你的代码看起来很棒,只是尝试将变量命名更类似于您要复制的内容。
<details>
<summary>英文:</summary>
According to the algorithm proposed, its code is seen to follow or comply with the guideline.
Your code looks great, it just tries to name the variables in a more similar way to what you are trying to replicate.
</details>
# 答案3
**得分**: 0
我尝试缩短了编码中的行数。
```java
int row = 1, column = 1;
while (row <= 4)
{
if (column <= 10) {
System.out.print("*");
column += 1;
}
else {
row += 1;
column = 1;
//System.out.println();
}
}
英文:
I have tried to shorten the number line of coding.
int row = 1, column = 1;
while (row <= 4)
{
if (column <= 10) {
System.out.print("*");
column += 1;
}
else {
row += 1;
column = 1;
//System.out.println();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论