用单词Java替换数字

时间:2014-07-01 20:16:06

标签: java

一直在寻找解决这个问题的解决方案。我刚刚开始编程大约2个星期,而且事实上我并不知道正确的java术语,但这可能是我找不到我想要的东西的原因。

无论如何,程序应该是:

打印出从1到100的所有数字,如果在迭代过程中程序遇到一个可被3整除的数字,它必须用“Crackle”替换该数字,如果它遇到一个可被5整除的数字用“Pop”代替那个数字,它会遇到一个可被两者整除的数字,然后它必须用“CracklePop

替换这个数字

我无法打印CracklePop以获得可被3和5分割的数字,但我可以将它打印到Crackle&弹出但不替换数字

我尝试过使用:

Integer.toString(); ,Integer.valueOf(); ,Integer.parseInt();

我无法开展工作。这是到目前为止的代码:

int counter = 0;
int max = 100;

for (int i = 1; i <= max; i++){ 
    if (counter % 3 == 0) { 
        System.out.println("Crackle"); 
    }
    else if(counter % 5 == 0){ 
        System.out.println("Pop"); 
    }
    else if (counter % 3 == 0 && counter % 5 == 0){ 
        System.out.println("CracklePop"); 
    }
    System.out.println(counter); 
    counter++;      
}

如果您还可以建议一个解决方案,这将是编写此类程序的最强大的方式,那也很好。

7 个答案:

答案 0 :(得分:1)

counter = 15您的最后2 else if将永远不会被调用,您需要重新订购,否则就像

if (counter % 5 == 0 && counter % 3 == 0){ 
    System.out.println("CracklePop"); 
} else if (counter % 3 == 0) { 
    System.out.println("Crackle"); 
} else if(counter % 5 == 0){ 
    System.out.println("Pop"); 
} else{
    System.out.println(counter);
}

仍然可以将%的结果存储在布尔变量中,以避免在最坏的情况下进行多次计算

答案 1 :(得分:1)

不需要counter变量。您只需使用i计数器本身。

此外,你应该检查两者是否可以被3和5整除:

int max = 100;

for (int i = 1; i <= max; i++){ 
    if (i % 3 == 0 && i % 5 == 0){ 
        System.out.println("CracklePop"); 
    } else  if (i % 3 == 0) { 
        System.out.println("Crackle"); 
    } else if(i % 5 == 0){ 
        System.out.println("Pop"); 
    } else {
        System.out.println(i); 
    }
}

最后else将确保您使用相应的字符串替换该数字,如果它与可被3和/或5整除的条件之一匹配。

答案 2 :(得分:0)

您需要将最后一个else / if语句放在开头。问题是,当你的一个if语句有效时,它将执行该语句并结束if语句。你的最后一句话永远不会被执行。你的陈述应该是:

public class CodeCracklePop {

public static void main(String[] args) {

int counter = 0;
int max = 100;

for (int i = 1; i <= max; i++){
    if (counter % 3 == 0 && counter % 5 == 0){ 
        System.out.println("CracklePop"); 
    } 
    else if (counter % 3 == 0) { 
        System.out.println("Crackle"); 
    }
    else if(counter % 5 == 0){ 
        System.out.println("Pop"); 
    }

    System.out.println(counter); 
    counter++;      
 }       

}

}

答案 3 :(得分:0)

数字未被替换的原因是因为打印了Crackle和Pop,但之后也会打印数字。 CracklePop永远不会被打印,因为任何可以实现它的东西都只会打印Crackle。您需要对逻辑进行重新排序,以使其符合您的需求。

答案 4 :(得分:0)

替换

System.out.println(counter); 

使用

else {
System.out.println(counter); 
}

此外,正如Jigar上面指出的那样,你应该先测试3和5

答案 5 :(得分:0)

试试这个

public static void main(String [] args){

    int max = 100;

    for (int i = 0; i < max; i++) {
        if (i % 3 == 0 || i % 5 == 0) {
            if (i % 3 == 0) {
                System.out.print("Crackle");
            }
            if (i % 5 == 0) {
                System.out.print("Pop");
            }
        } else {
            System.out.print(i);
        }
        System.out.println();
    }
}

答案 6 :(得分:0)

    public class CodeCracklePop {

    public static void main(String[] args) {

       /* int counter = 0; omit this*/
        int max = 100;
    boolean flag = false;

        for (int i = 1; i <= max; i++){ 

    if(i % 3 == 0){

        System.out.print("Crackle");
        flag = true;

    }

    if(i%5==0){

           System.out.print("Pop");
           flag = true;

    }
    if(!flag)
       System.out.print(""+i);

    flag = false;

    System.out.println("");
        }       

    }

    }

完成工作的有趣方式。

相关问题