我应该对代码的格式进行任何更改吗?

时间:2019-06-08 22:34:58

标签: formatting

我的老师告诉我,我需要通过正确使用代码块来进行格式化。我已经编辑了代码,并想知道是否有人有任何改进的想法,或者我的代码看起来还可以。我只是不确定是否格式化正确。我在工作上并没有失去很多分,但是如果有改进的余地,我将不胜感激。

import java.util.Scanner;
public class BarChart{ 
public static void main(String[] args){
    System.out.println("Riley Hall - Assignment 3\n");
    Scanner input = new Scanner(System.in);

    int n1 = 0;
    int n2 = 0;
    int n3 = 0;
    int n4 = 0;
    int n5 = 0;
    int i = 1;

    System.out.println("Enter a number between 1 and 30 ");
    n1 = input.nextInt();

    System.out.println("Enter a number between 1 and 30 ");
    n2= input.nextInt();

    System.out.println("Enter a number between 1 and 30 ");
    n3= input.nextInt();

    System.out.println("Enter a number between 1 and 30 ");
    n4= input.nextInt();

    System.out.println("Enter a number between 1 and 30 ");
    n5= input.nextInt();

    for(i = 1; i <= n1; i++){
        System.out.print("*");
    }   
    System.out.println();//new line

    for(i = 1; i <= n2; i++){
        System.out.print("*");
    }   
    System.out.println();//new line

    for(i = 1; i <= n3; i++){
        System.out.print("*");
    }   
    System.out.println();

    for(i = 1; i <= n4; i++){
        System.out.print("*");
    }   
    System.out.println();

    for(i = 1; i <= n5; i++){
        System.out.print("*");}
    }
    System.out.println();

    input.close();
}

}

这是我编辑程序之前的样子。

import java.util.Scanner;
public class BarChart 
{

public static void main(String[] args) 
{
        /* Instructor Comments:  You need to work on the formatting.  Make sure your 
            code lines up properly.  You have most of your code indented too far. 
            Within the Main method it should be indented one tab stop and 
            all the code lined up.  It should only be indented again if 
            it is inside another code structure such as an if statement 
            or for loop.  */

System.out.println("Riley Hall - Assignment 3\n");
    Scanner input = new Scanner(System.in);
    //initializing variables
    int n1 = 0;
    int n2 = 0;
    int n3 = 0;
    int n4 = 0;
    int n5 = 0;
    int i = 1;//index 

     System.out.println("Enter a number between 1 and 30 ");//prompt user input
             /* Instructor Comments: This next line should line up with the rest 
                of the code and not be indented.  */
        n1 = input.nextInt();//store user input

     System.out.println("Enter a number between 1 and 30 ");//prompt user input
        n2= input.nextInt();//store user input

     System.out.println("Enter a number between 1 and 30 ");//prompt user input
        n3= input.nextInt();//store user input

     System.out.println("Enter a number between 1 and 30 ");//prompt user input
        n4= input.nextInt();//store user input

     System.out.println("Enter a number between 1 and 30 ");//prompt user input
        n5= input.nextInt();//store user input

            /* Instructor Comments: Format your for loops properly.  The code blocks 
               should be on there own line.  Not on the same line as the code inside it.
               I am fixing the first one to show you.  */
    for(i = 1; i <= n1; i++)//starts loop at one, loop counts up to the integer the user incremented, increments up to users input one at a time
    {
                System.out.print("*");
            }//prints corresponding amount of *'s based on how many times the loop incremented 

            System.out.println();//new line

    for(i = 1; i <= n2; i++)//starts loop at one, loop counts up to the integer the user incremented, increments up to users input one at a time
    {System.out.print("*");}//prints corresponding amount of *'s based on how many times the loop incremented
     System.out.println();//new line

    for(i = 1; i <= n3; i++)//starts loop at one, loop counts up to the integer the user incremented, increments up to users input one at a time
    {System.out.print("*");}//prints corresponding amount of *'s based on how many times the loop incremented
     System.out.println();//new line

    for(i = 1; i <= n4; i++)//starts loop at one, loop counts up to the integer the user incremented, increments up to users input one at a time
    {System.out.print("*");}//prints corresponding amount of *'s based on how many times the loop incremented
     System.out.println();//new line

    for(i = 1; i <= n5; i++)//starts loop at one, loop counts up to the integer the user incremented, increments up to users input one at a time
    {System.out.print("*");}//prints corresponding amount of *'s based on how many times the loop incremented
     System.out.println();//new line

     input.close();//closes scanner
}

}

2 个答案:

答案 0 :(得分:1)

关于格式设置代码,需要注意的一件事是,在大多数情况下,代码的格式不会影响其运行方式。 (在某些情况下,您可以将大部分代码写在一行上,但仍然可以使用。)

格式化是设计用来简化开发人员调试和更改或添加代码的事情之一。考虑到这一点,每个人都可以以适合自己的方式格式化代码。对于应该起作用的代码,您应该不会丢失标记。

以下是一些著名的格式化代码的做法:

分层

将其视为层次结构。如果将对象放置在类中或将函数放置在函数中,请对其进行格式化以表示该对象。 例如:

import java.util.Scanner;
public class BarChart{ 
public static void main(String[] args){

将成为:

import java.util.Scanner;
public class BarChart{ 
   public static void main(String[] args){

看看这如何表示public static void main(String[] args){public class BarChart{内吗?它使视觉上更容易看到它。用括号表示函数的闭包,并在与声明相同的级别上缩进。当然,这引发了关于是否使用tabspace来缩进代码的永无止境的争论,但实际上-没关系。这是您的选择。

间距

我曾经在某处阅读过,如果您认为注释最适合在两行代码之间,请留一段空白。这尤其适用于两个不同的功能或类似功能(看起来已经完成)之间。

评论

啊,发表评论。它使我免于花费数小时去记住为什么我写了这样的东西,或者为什么有些东西行不通。 始终尝试注释您的代码。这是一次您可以将所有计算机语言转换为人类可读的语言,这将帮助您更好地理解您的代码。如果您需要传递代码,新开发人员将能够更好地理解它。如果您需要在一年后进行修复,则可以回忆起它的工作原理以及可能需要更改的内容。评论,评论,评论。

这些只是我最喜欢的一些实践,您也应该适应一些实践。这些绝不是专业实践,而仅仅是编码的基础。为了回答您的原始问题,您的代码结构很好,但请尝试着重于缩进和注释。

(注意:如果您在谈论代码的结构而不是格式,则应考虑不要重复使用功能。这会使代码看起来很笨重。)

答案 1 :(得分:0)

它们可能意味着您需要重复某些模式时使用循环。您的主要功能可以这样重写:

  public static void main(String[] args) {
    System.out.println("Riley Hall - Assignment 3\n");
    Scanner input = new Scanner(System.in);

    int[] n = new int[5];
    int i, j;

    for (i = 0; i < 5; i++) {
      System.out.println("Enter a number between 1 and 30 ");
      n[i] = input.nextInt();
    }

    for (i = 0; i < 5; i++) {
      for (j = 0; j < n[i]; j++) {
        System.out.print("*");
      }
      System.out.println();
    }

    input.close();
  }

如您所见,我使用了从零索引开始并在达到数组大小之前结束的循环变量。通常认为这是一种好习惯,因为例如数组以索引0开头。如果您真的想打动老师,也可以使用以下方法:

  private static int[] readNumbers(int amount) {
    Scanner input = new Scanner(System.in);

    int[] n = new int[amount];
    int i;

    for (i = 0; i < 5; i++) {
      System.out.println("Enter a number between 1 and 30 ");
      n[i] = input.nextInt();
    }

    input.close();

    return n;
  }

  private static void printStars(int[] amounts) {
    int i, j;

    for (i = 0; i < amounts.length; i++) {
      for (j = 0; j < amounts[i]; j++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }

  public static void main(String[] args) {
    System.out.println("Riley Hall - Assignment 3\n");

    int[] n = readNumbers(5);
    printStars(n);
  }

它们使代码可重用,并且无限有用。

根据格式,您始终可以获取IntelliJ IDEA社区版,并使用其自动格式功能来查看总体上良好的格式外观。除非您对自己保留格式感到满意,否则不要依赖它来完成这项工作。