Java编程 – 基于流程图的编码

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

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

Java编程 – 基于流程图的编码

Code

	// r is row, c is column
	int r = 1, c = 1;
	
	do { 
	    if (r &lt;= 4)
	    {  
	      if (c &lt;= 10)
	      {
	          System.out.print(&quot;*&quot;);
	          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&#39;t have this condition.

Notice how your `if (r &lt;= 4)` statement is directly nested in the `do...while`. This means you can reduce to a single `while`:

while (r < = 4) {
...
}


Similarly `c &lt;= 10` should be implemented as a loop instead of just an `if`.

The flow chart doesn&#39;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 &lt;= 4)
        {
          if (column &lt;= 10) {
              System.out.print(&quot;*&quot;);
              column += 1;
          }
          else {
              row += 1;
              column = 1;
              //System.out.println();
          }
        }

huangapple
  • 本文由 发表于 2020年8月19日 23:00:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63489827.html
匿名

发表评论

匿名网友

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

确定