带有 if 语句的简单 java 循环语句不正确 运行?
Simple java loop statement with if statements not running correctly?
我正在复习 Java,但无法使该程序正常工作。这是一个计数到100的while计数器循环。如果计数器能被3整除则输出"On",如果计数器能被7整除则输出"Base",如果计数器能被7整除而3则输出"OnBase",否则输出数字。现在该程序甚至无法编译,我不知道问题出在哪里。这是我的程序,如有任何帮助,我们将不胜感激。
public class Counter {
public static void main(String[] args) {
int i = 1;
while(i <= 100)
{
if((i % 3) == 0){
system.out.println("On");
i++;
continue;
}
if((i % 7) == 0){
system.out.println("Base");
i++;
continue;
}
if((i % (3*7) == 0){
system.out.println("OnBase");
i++;
continue;
}
system.out.println(i);
i++;
}
}
}
关于编译有两个问题:
- 将系统更改为系统。
- 第 ----- 行缺少右括号 if ((i % (3 * 7) == 0)) { // 添加了右括号
- 除了编译之外,你要找的逻辑也不对。。。我在下面提供一些示例代码,你也去看看吧。。。。。。。。。。。。。。。。。。。。。
// Sample Code to follow
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (((i % 3) == 0) && ((i % 7) == 0))
System.out.println("OnBase");
else if ((i % 3) == 0)
System.out.println("On");
else if ((i % 7) == 0)
System.out.println("Base");
else
System.out.println(i);
}
}
我正在复习 Java,但无法使该程序正常工作。这是一个计数到100的while计数器循环。如果计数器能被3整除则输出"On",如果计数器能被7整除则输出"Base",如果计数器能被7整除而3则输出"OnBase",否则输出数字。现在该程序甚至无法编译,我不知道问题出在哪里。这是我的程序,如有任何帮助,我们将不胜感激。
public class Counter {
public static void main(String[] args) {
int i = 1;
while(i <= 100)
{
if((i % 3) == 0){
system.out.println("On");
i++;
continue;
}
if((i % 7) == 0){
system.out.println("Base");
i++;
continue;
}
if((i % (3*7) == 0){
system.out.println("OnBase");
i++;
continue;
}
system.out.println(i);
i++;
}
}
}
关于编译有两个问题:
- 将系统更改为系统。
- 第 ----- 行缺少右括号 if ((i % (3 * 7) == 0)) { // 添加了右括号
- 除了编译之外,你要找的逻辑也不对。。。我在下面提供一些示例代码,你也去看看吧。。。。。。。。。。。。。。。。。。。。。
// Sample Code to follow
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (((i % 3) == 0) && ((i % 7) == 0))
System.out.println("OnBase");
else if ((i % 3) == 0)
System.out.println("On");
else if ((i % 7) == 0)
System.out.println("Base");
else
System.out.println(i);
}
}