简体   繁体   中英

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.

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

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 .

I've tried using:

Integer.toString(); , Integer.valueOf(); , 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

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. You can just use the i counter itself.

Also you should place the check for both being divisible by 3 and 5 first:

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.

You need to put your last else/if statement at the beginning. The problem is, when one of your if statements is valid, it will execute that statement and end the if statement. 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. 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

Try this one

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();
    }
}
    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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