这可能偏离主题,但我的一位老师最近说我的缩进不正确,但给出了一个非常模糊的错误原因,而且我不认为我编码时的缩进有任何问题。下面是我写的一个程序,我的老师说缩进很烦人,而且读起来不正确。
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 read
和maintainable
代码。
我写了一个short article有关 Java 编码最佳实践的信息,有兴趣的人可以阅读更多内容。
- 缩进 2-4 个空格(Google 建议 +2 个空格)
- 建议的行长度范围应介于
70 to 120 (max)
之间 - 当谈到换行时
after comma
和before an operator
- 保留左大括号
{
与方法签名和关键字(即 if、for 等关键字)位于同一行的末尾 - 使用
end of line comments //
只有当你对这条线有几句话要说时。如果评论的长度超过几个字,您应该使用/* */
或/* */
对于多行注释 - 当方法签名很长时,换行并使用
8 spaces
区分方法参数和方法体 - 在自己的行中声明每个变量,而不是
int i,j,k;
method name
之间没有空格和括号(
但是,保留关键字(即 if、for)和括号之间应该有 1 个空格 - 这将有助于在视觉上轻松地区分方法和其他结构- 方法之间用空行分隔
- 参数列表中逗号后面应出现空格
- 空格键
operators (i.e. +,-,%, etc.) and
=` - 还有更多 - 请参阅下面的链接
这是使用上述一些要点的代码
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/