dimanche 23 août 2020

How can I condense a bunch of if/else statements

So I have a big lump of code that is taking up a large area of space + I have many of these. What would be the best way to condense something like what I have attached to this post?

if(number == 1) {
    System.out.println("1");
}
else if(number == 2) {
    System.out.println("2");
}
else if(number == 3) {
    System.out.println("3");
}
else if(number == 4) {
    System.out.println("4");
}
else if(number == 5) {
    System.out.println("5");
}
else if(number == 6) {
    System.out.println("6");
}
else if(number == 7) {
    System.out.println("7");
}
else if(number == 8) {
    System.out.println("8");
}

I'm just getting into to programming, so this would probably be a good learning experience, to understand how it works too! Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire