这可能不是主题,但是我的一位老师最近说我的缩进是不正确的,但是给出了错误原因的模糊原因,我认为我的缩进在编码时没有任何问题。以下是我编写的程序,我的老师说缩进很烦人,而且阅读不正确。
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代码,我认为我所做的一切都正确。我不知道她在说什么?
答案 0 :(得分:3)
不久前,我回顾了Oracle,Google,Twitter和Spring等公司的Java Coding Conventions
。令人惊讶的是,对于indentation
和formatting
来说有很多话要说,这里是一个简短的摘要和有用的链接,它们可以帮助您编写easy to read
和maintainable
代码。
我为有兴趣阅读更多内容的人写了short article关于Java编码的最佳实践。
70 to 120 (max)
after comma
和before an operator
{
保留在方法和关键字(即if,for等关键字)签名的同一行末尾end of line comments //
。如果评论的长度超过几个单词,则应使用/* */
或/* */
进行多行评论8 spaces
区分方法参数和方法主体int i,j,k;
中声明method name
和括号(
之间没有空格,但是,保留关键字(例如,for)和括号之间应该有1个空格-这将有助于在视觉上轻松地区分方法和其他结构< / li>
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);
}
}
以下是您可以在代码中进行改进的地方
您的代码中也有很多优点。我喜欢方法命名,即您将其划分为方法的方式。您走在正确的道路上,阅读以下一些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
答案 1 :(得分:0)
您的方法未在类内缩进。它们似乎与您代码中的类处于同一级别。其他方法级别的缩进看起来很好。尝试在您的IDE中对其进行格式化,您将看到不同之处。结构应类似于下面。
class Order
{
// fields
// constructors
// methods
}
答案 2 :(得分:-1)
缩进是一种特殊的标准,我们统一遵循所有类中的代码。首先,我可以说
说过,任何人都可以拥有自己的格式化和缩进样式,这没什么问题。