+ 1
Can I do this code simpler? //Sum of 5 input numbers
import java.util.*; public class Summ { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] arr = new int[5]; int st = 0; int i; for(i=0; !(i>=5); i++) { arr[i] = sc.nextInt(); st += arr[i]; } System.out.println(st); } }
2 Answers
+ 7
Yes.
int[] arr = new int[5];
int st = 0;
for(int i: arr) {
i = sc.nextInt();
st += i;
}
System.out.println(st);
+ 3
public static void main(String[] args){
int sum=0;
for(int i=0; i<=4; i++)
sum+=new java.util.Scanner(System.in).nextInt();
System.out.println(sum);
}