使用 main 方法和 public static final int 计数到 2 个不同的值
counting to 2 different values with main method and public static final int
我目前 运行 遇到 "practice it" 问题,我被困在这个问题上:
“假设您正在尝试编写一个使用循环产生以下输出的程序。下面的程序是对解决方案的尝试,但它至少包含四个主要错误。识别并修复它们。
1 3 5 7 9 11 13 15 17 19 21
1 3 5 7 9 11 “
public class BadNews {
public static final int MAX_ODD = 21;
public static void writeOdds() {
// print each odd number
for (int count = 1; count <= (MAX_ODD - 2); count++) {
System.out.print(count + " ");
count = count + 2;
}
// print the last odd number
System.out.print(count + 2);
}
public static void main(String[] args) {
// write all odds up to 21
writeOdds();
// now, write all odds up to 11
MAX_ODD = 11;
writeOdds();
}
}
我已将代码更改为:
public class BadNews {
public static final int MAX_ODD = 21;
public static void writeOdds() {
// print each odd number
for (int count = 1; count <= (MAX_ODD); count++) {
System.out.print(count + " ");
count = count +1; //tried to chacge count++ into count + 2 but it was a miss
}
}
public static void main(String[] args) {
// write all odds up to 21
writeOdds();
// now, write all odds up to 11
MAX_ODD = 11;
writeOdds();
}
}
我以为最后一个问题是 final int MAX_ODD 应该移到 main 中并更改为 "normal" 变量 (int MAX_ODD) 但它以问题失败和评论结束 "Your solution must have a class constant. A constant must be declared as 'public static final' outside of any methods in your class."
有什么解决办法吗?
此代码将为您提供预期的输出。
public static void main(String[] args) {
writeOdds(21);
writeOdds(11);
}
public static void writeOdds(int n) {
// print each odd number
for (int count = 1; count <= (n); count++) {
System.out.print(count + " ");
count = count +1; //tried to chacge count++ into count + 2 but it was a miss
}
}
一个声明为final的属性在设置初始值后不能改变它的值。这个初始值是最终的。该变量必须保持在 class 级别,在任何方法之外,以便每个方法都可以访问它。
因此,干脆把final这个词删掉。
我目前 运行 遇到 "practice it" 问题,我被困在这个问题上: “假设您正在尝试编写一个使用循环产生以下输出的程序。下面的程序是对解决方案的尝试,但它至少包含四个主要错误。识别并修复它们。 1 3 5 7 9 11 13 15 17 19 21 1 3 5 7 9 11 “
public class BadNews {
public static final int MAX_ODD = 21;
public static void writeOdds() {
// print each odd number
for (int count = 1; count <= (MAX_ODD - 2); count++) {
System.out.print(count + " ");
count = count + 2;
}
// print the last odd number
System.out.print(count + 2);
}
public static void main(String[] args) {
// write all odds up to 21
writeOdds();
// now, write all odds up to 11
MAX_ODD = 11;
writeOdds();
}
}
我已将代码更改为:
public class BadNews {
public static final int MAX_ODD = 21;
public static void writeOdds() {
// print each odd number
for (int count = 1; count <= (MAX_ODD); count++) {
System.out.print(count + " ");
count = count +1; //tried to chacge count++ into count + 2 but it was a miss
}
}
public static void main(String[] args) {
// write all odds up to 21
writeOdds();
// now, write all odds up to 11
MAX_ODD = 11;
writeOdds();
}
}
我以为最后一个问题是 final int MAX_ODD 应该移到 main 中并更改为 "normal" 变量 (int MAX_ODD) 但它以问题失败和评论结束 "Your solution must have a class constant. A constant must be declared as 'public static final' outside of any methods in your class." 有什么解决办法吗?
此代码将为您提供预期的输出。
public static void main(String[] args) {
writeOdds(21);
writeOdds(11);
}
public static void writeOdds(int n) {
// print each odd number
for (int count = 1; count <= (n); count++) {
System.out.print(count + " ");
count = count +1; //tried to chacge count++ into count + 2 but it was a miss
}
}
一个声明为final的属性在设置初始值后不能改变它的值。这个初始值是最终的。该变量必须保持在 class 级别,在任何方法之外,以便每个方法都可以访问它。 因此,干脆把final这个词删掉。