简体   繁体   English

用单词Java替换数字

[英]Replacing a number with a word Java

Been digging around the internet just about all day looking for a solution to this problem. 整天都在互联网上寻找有关此问题的解决方案。 I've just started programming about 2 weeks ago nearly and the fact that I don't really know the proper java lingo yet could be the reason I'm not finding what I'm looking for. 我差不多在2周前才开始编程,而我实际上还不太了解Java语言,这可能是我找不到所需内容的原因。

Anyway the program is supposed to: 无论如何,该程序应该:

print out all the numbers from 1 to 100 and if during the iteration the program comes across a number that's divisible by 3 it has to replace that number with "Crackle", if it comes across a number that's divisible by 5 it has to replace that number with "Pop" and it comes across a number that's divisible by both then it has to replace that number with "CracklePop 打印出从1到100的所有数字,如果程序在迭代过程中遇到一个可被3整除的数字,则必须用“ Crackle”替换该数字,如果它遇到一个可以被5整除的数字,则必须替换该数字用“ Pop”数字,并且遇到一个可被两者整除的数字,则必须用“ CracklePop”替换该数字

I can't get it to print CracklePop for numbers that are divisable by both 3 and 5, but I can get it to print Crackle & Pop but not replace the numbers . 我无法打印出可以同时被3和5分开的数字的CracklePop,但是我可以打印出Crackle&Pop 而不是替换数字

I've tried using: 我试过使用:

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

None of which I can get to work. 我都无法上班。 This is the code so far: 到目前为止,这是代码:

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++;      
}

If if you could also suggest a solution that would be the most robust way of writing a program like this that would be good too. 如果您还可以提出一种解决方案,那将是编写这样的程序的最可靠方法,那也很好。

for counter = 15 your last 2 else if will never get invoked, you need to re order your if else like for counter = 15您的最后2个else if永远不会被调用,则需要重新排序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);
}

still you can store result of % in a boolean variable to avoid multiple time calculation in worst case 您仍然可以将%结果存储在布尔变量中,以避免在最坏的情况下进行多次计算

There is no need for the counter variable. 不需要counter变量。 You can just use the i counter itself. 您可以只使用i计数器本身。

Also you should place the check for both being divisible by 3 and 5 first: 另外,您还应该先将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); 
    }
}

The last else will make sure you replace the number with the corresponding string if it matches one of the conditions of being divisible by 3 and/or 5. 最后else将确保您用相应的字符串替换号码,如果它匹配的3和/或5整除是条件之一。

You need to put your last else/if statement at the beginning. 您需要将最后一个else / if语句放在开头。 The problem is, when one of your if statements is valid, it will execute that statement and end the if statement. 问题是,当您的if语句之一有效时,它将执行该语句并结束if语句。 Your last statement will never be executed. 您的最后一条语句将永远不会执行。 Your statement should be: 您的声明应为:

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++;      
 }       

}

}

The reason the numbers aren't replaced is because the Crackle and Pop are printed, but the numbers are also printed afterwards. 之所以不替换数字,是因为打印了“裂纹”和“流行音乐”,但随后也打印了数字。 CracklePop never is printed because anything that would fulfill it would just print Crackle instead. 从不打印CracklePop,因为满足要求的任何东西都只会打印Crackle。 You need to reorder your logic in order for it to fit your needs. 您需要重新排序逻辑,以使其符合您的需求。

Replace 更换

System.out.println(counter); 

With

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

In addition, as Jigar pointed out above you should test for 3 and 5 first 另外,正如Jigar上面指出的,您应该先测试3和5

Try this one 试试这个

public static void main(String[] args) { 公共静态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();
    }
}
    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("");
        }       

    }

    }

A fun way of getting the job done. 完成工作的有趣方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM