gpt4 book ai didi

Java 标识符需要分号,给出 : Variable naming conventions

转载 作者:行者123 更新时间:2023-12-01 07:52:57 25 4
gpt4 key购买 nike

为什么我不能在 Java 代码中使用这样的变量名称?

int 6q1, 6q2, 6q3, 6q4;
int 7q1, 7q2, 7q3, 7q4;
int 8q1, 8q2, 8q3, 8q4;

错误:需要标识符

最佳答案

变量名称不能以数字开头。它们只能以字母或 (IIRC) 下划线开头。

... 6q1, 6q2, 6q3, 6q4;

这些都是无效的。

int 7q1, 7q2, 7q3, 7q4;
int 8q1, 8q2, 8q3, 8q4;

这些也是。

将它们重命名为以 i_ 开头。

来自documentation (斜体和粗体是我为了强调而添加的):

Every programming language has its own set of rules and conventions for the kinds of names that you're allowed to use, and the Java programming language is no different. The rules and conventions for naming your variables can be summarized as follows:

Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_". The convention, however, is to always begin your variable names with a letter, not "$" or "_". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted.

Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named cadence, speed, and gear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name you choose must not be a keyword or reserved word.

If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.

关于Java 标识符需要分号,给出 : Variable naming conventions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33981361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com