java - 我的缩进有问题吗?

标签 java indentation code-formatting

这可能偏离主题,但我的一位老师最近说我的缩进不正确,但给出了一个非常模糊的错误原因,而且我不认为我编码时的缩进有任何问题。下面是我写的一个程序,我的老师说缩进很烦人,而且读起来不正确。

    import java.util.Scanner; 

    public class SecretCode {


    public static boolean isValidLength (String stringChecker) // Checks to see is user the String that was input by the user is valid. 
    {
        if (stringChecker.length() >= 2 && stringChecker.length() <= 12) // Determines how long the String is. 
        {
            return true; // It is valid. 
        }
        else 
        {
            return false; // It is invalid. 
        }
    }

    public static int charToAscii (String stringToAscii) // Converts the String inputted by the user and converts it to an Ascii value. The values are stored and return as an int. 
    {
        int stringValue = 0;
        for (int x = 0; x < stringToAscii.length(); x++) // Loop to parse through the String and add all the Ascii values. 
        {
            stringValue += (int) stringToAscii.charAt(x); // Adder. 
        }

        return stringValue; // Returns final value of String. 
    }

    public static int getNumDigits (int totalDigits) // Gets the total number of digits in an int. 
    {
        return (int) (Math.log10(totalDigits) + 1); // This will return the total amount of digits using a logarithmic function You can also do String.valueOf(totalDigits).length(); 
    }

    public static String getSecretCode (int secretCodeConversion) // Converts Ascii values into two separate characters. 
    {
        String secretCode = new String (); // String instantiation. Proper syntax for declaring a String. There is not 

        int num1, num2; // Variable declaration

        num1 = (secretCodeConversion % 10000) / 100; // Splits the numbers into two. This gets first two digits.
        num2 = (secretCodeConversion % 100); // Second two digits.

        if (num1 <= 65)
        {
            num1 += 61; // Ensures a letter is possible.
        }
        if (num2 <= 65)
        {
            num2 += 61; // Ensures a letter is possible for num2. This could have been short-circuited by an 'or' operator. 
        }

        secretCode += Character.toString((char) num1) + Character.toString((char) num2); // Concatenates the two numbers back into a String.

        return secretCode; // Return secret code. 
    }


    public static void main(String[] args) {
        String secretCode = new String (); // User input
        Scanner sc = new Scanner (System.in); // Instantiates Scanner object to read input. 
        System.out.println ("Please enter the String you would like to make a secret code?");
        secretCode = sc.next(); // Input

        while (isValidLength(secretCode) == false) // Checks to see low long secret message it is to make sure it is valid. 
        {
            System.out.println ("Please enter the String you would like to make a secret code?");
            secretCode = sc.next(); // Loops until true. 
        }

        sc.close(); // No more need for Scanner.

        int sumOfDigits = charToAscii(secretCode); // Data for output requirements. 

        if (getNumDigits(sumOfDigits) % 2 != 0)
        {
            sumOfDigits *= 10; 
        }

        if (sumOfDigits <= 4000)
        {
            sumOfDigits *= 4;
        }

        if (getNumDigits(sumOfDigits) % 2 != 0)
        {
            sumOfDigits /= 10;
        }

        System.out.println();
        System.out.println();

        System.out.println ("Your Secret Coded Message Is: " + getSecretCode(sumOfDigits));

        System.out.println();
        System.out.println();

        System.out.println ("Additional Data:");
        System.out.println ("_______________________");

        System.out.println ("String Input: " + '"' + secretCode + '"');
        System.out.println ("Sum of Ascii Codes: " + charToAscii(secretCode));
        System.out.println ("Secret Code in Ascii Numbers " + sumOfDigits);
    }
} // This bracket is part of the code, but I could not get the formatting to work. 

我的老师说一切都在左边界上,但我相信左边界上的东西没有其他地方可去。我看过其他程序员,也看过 Github 上的其他 Java 代码,我认为我做的一切都是正确的。我不知道她在说什么?

最佳答案

不久前,我回顾了Java Coding Conventions来自 Oracle、Google、Twitter 和 Spring 等公司。令人惊讶的是,当谈到 indentation 时,有很多话要说。和formatting这里有一个简短的摘要和有用的链接,可以帮助您编写 easy to readmaintainable代码。

我写了一个short article有关 Java 编码最佳实践的信息,有兴趣的人可以阅读更多内容。

  1. 缩进 2-4 个空格(Google 建议 +2 个空格)
  2. 建议的行长度范围应介于 70 to 120 (max) 之间
  3. 当谈到换行时 after commabefore an operator
  4. 保留左大括号 {与方法签名和关键字(即 if、for 等关键字)位于同一行的末尾
  5. 使用end of line comments //只有当你对这条线有几句话要说时。如果评论的长度超过几个字,您应该使用 /* *//* */对于多行注释
  6. 当方法签名很长时,换行并使用 8 spaces区分方法参数和方法体
  7. 在自己的行中声明每个变量,而不是 int i,j,k;
  8. method name 之间没有空格和括号(但是,保留关键字(即 if、for)和括号之间应该有 1 个空格 - 这将有助于在视觉上轻松地区分方法和其他结构
  9. 方法之间用空行分隔
  10. 参数列表中逗号后面应出现空格
  11. 空格键operators (i.e. +,-,%, etc.) and =`
  12. 还有更多 - 请参阅下面的链接

这是使用上述一些要点的代码

import java.util.Scanner; 

public class SecretCode {

    // Checks to see is user the String that was input by the user is valid.

    public static boolean isValidLength(String stringChecker) {
        // Determines length check
        if ((stringChecker.length() >= 2) && (stringChecker.length() <= 12)) { 
            return true;
        } else {
            return false; 
        }
    }

    /* 
     * Converts the String inputted by the user and converts it to an Ascii value. 
     * The values are stored and return as an int. 
     */
    public static int charToAscii (String stringToAscii) {
        int stringValue = 0;

        // Loop to parse through the String and add all the Ascii values. 

        for (int x = 0; x < stringToAscii.length(); x++) {
            stringValue += (int) stringToAscii.charAt(x); // Adder. 
        }

        return stringValue; 
    }

    /* 
    * Gets the total number of digits in an int.  
    * This will return the total amount of digits using a logarithmic function You 
    * can also do String.valueOf(totalDigits).length();
    */

    public static int getNumDigits (int totalDigits)  {
        return (int) (Math.log10(totalDigits) + 1); 
    }

    // Converts Ascii values into two separate characters.

    public static String getSecretCode (int secretCodeConversion) {
        String secretCode = new String (); 

        // Splits the numbers into two. This gets first two digits
        int num1 = (secretCodeConversion % 10000) / 100; 

        // Second two digits - use proper variable naming i.e. firstTwoDigits, secondDigits etc.
        int num2 = (secretCodeConversion % 100);

        if (num1 <= 65)  {
            num1 += 61; // Ensures a letter is possible.
        }
        if (num2 <= 65) {
            // Ensures a letter is possible for num2. This could have been short-circuited by an 'or' operator.
            num2 += 61;  
        }

        secretCode += Character.toString((char) num1) + Character.toString((char) num2); 

        return secretCode; 
    }

    public static void main(String[] args) {
        String secretCode = new String (); 

        // Instantiates Scanner object to read input. 
        Scanner sc = new Scanner (System.in); 

        System.out.println ("Please enter the String you would like to make a secret code?");
        secretCode = sc.next();

        // Checks to see low long secret message it is to make sure it is valid. 

        while (isValidLength(secretCode) == false) {
            System.out.println ("Please enter the String you would like to make a secret code?");
            secretCode = sc.next(); // Loops until true. 
        }

        sc.close(); // No more need for Scanner.

        int sumOfDigits = charToAscii(secretCode); 

        if (getNumDigits(sumOfDigits) % 2 != 0) {
            sumOfDigits *= 10; 
        }

        if (sumOfDigits <= 4000) {
            sumOfDigits *= 4;
        }

        if (getNumDigits(sumOfDigits) % 2 != 0) {
            sumOfDigits /= 10;
        }

        System.out.println();
        System.out.println();

        System.out.println ("Your Secret Coded Message Is: " + getSecretCode(sumOfDigits));

        System.out.println();
        System.out.println();

        System.out.println ("Additional Data:");
        System.out.println ("_______________________");

        System.out.println ("String Input: " + '"' + secretCode + '"');
        System.out.println ("Sum of Ascii Codes: " + charToAscii(secretCode));
        System.out.println ("Secret Code in Ascii Numbers " + sumOfDigits);
    }
} 

以下是您可以在代码中改进的地方

  • 注释过多 - 有时很清楚一行在做什么,不要添加注释,只有在代码复杂且难以阅读时才添加注释
  • 变量命名,方便理解变量存储的内容
  • 缩进
  • 使用评论类型
  • 缺少 javadoc 注释

您的代码中也有很多积极的一面。我喜欢方法命名,以及将其划分为方法的方式。您走在正确的道路上,阅读以下一些 Java 编码约定,一切就都准备好了。

Oracle Java 风格指南 - https://www.oracle.com/technetwork/java/codeconventions-150003.pdf

Google Java 风格指南 - https://google.github.io/styleguide/javaguide.html

Spring 框架风格指南 - https://github.com/spring-projects/spring-framework/wiki/Code-Style

Twitter Java 风格指南 - https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/styleguide.md

关于java - 我的缩进有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53377133/

相关文章:

java - 如何使用流在没有冗余代码的情况下获取嵌套集合的最小/最大元素?

ocaml - 如何缩进现有的 OCaml 代码

php - 防止 PHP 解释源代码中字符串中的新行

visual-studio - 在 Visual Studio 中关闭自动格式化

java - 二叉树最大和级别 - 更好的设计?

java - 如何解决servlet类和jsp.file之间传递属性时出现空指针异常

java - 什么时候应该使用网络服务?

qt - Qt Creator 中的缩进指南?

CSS/HTML : indent whole block of text containing hard returns

eclipse-plugin - 完全找不到 jp.sourceforge.pdt_tools.formatter