Java 如何嵌套此条件语句?在爪哇
我是一名Java学生,在嵌套这个程序的条件语句时遇到了麻烦 练习CozaLozaWoza(循环和条件):编写一个名为 CozaLozaWoza,打印数字1到110,每行11个数字。 程序应打印“Coza”以代替下列数字: 3的倍数,“Loza”表示5的倍数,“Woza”表示7的倍数, “CozaLoza”表示3和5的倍数,依此类推。输出应为 比如: 我设法做到了这一点Java 如何嵌套此条件语句?在爪哇,java,Java,我是一名Java学生,在嵌套这个程序的条件语句时遇到了麻烦 练习CozaLozaWoza(循环和条件):编写一个名为 CozaLozaWoza,打印数字1到110,每行11个数字。 程序应打印“Coza”以代替下列数字: 3的倍数,“Loza”表示5的倍数,“Woza”表示7的倍数, “CozaLoza”表示3和5的倍数,依此类推。输出应为 比如: 我设法做到了这一点 public class CozaLozaWoza { public static void main(String[] arg
public class CozaLozaWoza {
public static void main(String[] args) {
for (int x = 1; x <= 110; x +=1) {
if (x % 3 == 0) {
System.out.print(" Coza");
}else if (x % 5 == 0) {
System.out.print(" Loza");
}else if (x % 7 == 0) {
System.out.print(" Woza");
}else if (x % 3 != 0 && x % 5 != 0 && x % 7 != 0) {
System.out.print(" " + x);
}
if (x % 11 == 0) {
System.out.println();
}
}
}
公共类CozaLozaWoza{
公共静态void main(字符串[]args){
对于(int x=1;xif语句应该相互独立,因为对于同一个数字可以有多个语句为真(例如“CozaLoza”表示3和5的倍数)
for(int x=1;xpackage homePrac;
公共级LOZAMOZACOZA
{
公共静态void main(字符串[]args)
{
int max=110;
对于(int i=1;i您确定需要嵌套吗?这是我的原始代码,但练习要求嵌套它尝试:修改程序以使用嵌套if(if…else if…else if…else)相反。@ahmadaziz这会让你的代码更难看。你必须先测试这个数字是否可以被3*5*7整除,然后是5*7整除,然后是3*7整除,然后是3*5整除,然后是7整除,最后是3整除。这不能解释换行条件(如果你想在相同的if…else if…中包含它,你需要测试这个数字是否也可以被77,55,33和11整除)。
public class CozaLozaWoza {
public static void main(String[] args) {
for (int x = 1; x <= 110; x +=1) {
if (x % 3 == 0) {
System.out.print(" Coza");
}else if (x % 5 == 0) {
System.out.print(" Loza");
}else if (x % 7 == 0) {
System.out.print(" Woza");
}else if (x % 3 != 0 && x % 5 != 0 && x % 7 != 0) {
System.out.print(" " + x);
}
if (x % 11 == 0) {
System.out.println();
}
}
}
for (int x = 1; x <= 110; x +=1) {
boolean regular = true;
System.out.print (" ");
if (x % 3 == 0) {
System.out.print("Coza");
regular = false;
}
if (x % 5 == 0) {
System.out.print("Loza");
regular = false;
}
if (x % 7 == 0) {
System.out.print("Woza");
regular = false;
}
if (regular) {
System.out.print(x);
}
if (x % 11 == 0) {
System.out.println();
}
}
package homePrac;
public class LOZAMOZACOZA
{
public static void main (String []args)
{
int max = 110;
for (int i=1; i<=max; i++)
{
if (i%3==0)
System.out.print("Coza");
else if (i%5==0)
System.out.print ("Woza");
else if (i%7==0)
System.out.print("CozaLoza");
else if (i%3!=0 || i%5!=0 || i%7!=0)
System.out.print(i);
if(i%11==0)
System.out.println("");
}
}
}