[Solved] How can i simplify these if-statements?


One thing that seems obvious is that you cannot reduce the number of String messages.
However, you can still remove the clutter by replacing consecutive System.out.println statements with a single System.out.println statement as follows :

Your code :

System.out.println(specialWelcome1);
System.out.println(joinLogin1);

Formatted code :

System.out.println(specialWelcome1 +”\n”+joinLogin1);

The above formatted code can look ugly for cases where there are many more System.out.println statements.In that case you can make good use of indentations as follows:

> System.out.println(specialWelcome1 
>               +"\n"+joinLogin1
>               +"\n"+joinLogin2
>               +"\n"+joinLogin3
>               +"\n"+joinLogin4
>               +"\n"+joinLogin5
>               +"\n"+joinLogin6
>               +"\n"+joinLogin7
>               );

Also, the switch version by Suchen Oguri is better. You should prefer switch statements over if-else as they are better performance-wise. The if-else will go on checking conditions one-by-one until it finds one that matches, which is not the case with switch statements.

1

solved How can i simplify these if-statements?