I have written the below code for my practice and it works fine, but I'm curious if the logic used in the code can be written much more efficiently or not.Any insights would be welcome:
public class RangeOfNumbers{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Start? ");
int start = input.nextInt();
System.out.print("End? ");
int end = input.nextInt();
if(start>=end){
for (int i = start; i >=end; i--){
if(i==end){
System.out.print(i);
}else{
System.out.print(i+", ");
}
}
}else{
for (int i = start; i <=end; i++){
if(i==end){
System.out.print(i);
}else{
System.out.print(i+", ");
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire