i trying build array user input of numbers. have been able calculate min , max. code below comes first number input user both min , max. suggestions on how fix appreciated:
scanner in=new scanner (system.in); int num[]=new int[5]; int average=0; int i=0; int sum=0; (i=0;i<num.length;i++) { system.out.println("enter number"); num[i]=in.nextint(); sum=sum+num[i]; int min=num[0]; int max=num[0]; if (num[i]<min) { min=num[i]; } if (num[i]<max) { max=num[i]; } average=sum/6; system.out.println("average="+ average); system.out.println("min="+ min); system.out.println("max="+ max); system.out.println("sum="+ sum);
your code wrong. here's bit improved one:
scanner in=new scanner (system.in); double num[]=new double[5]; double average=0; int i=0; double sum=0; double min = double.max_value; double max = double.min_value; (i=0;i<num.length;i++) { system.out.println("enter number"); num[i]=in.nextdouble(); sum=sum+num[i]; if (num[i]<min) { min=num[i]; } else if (num[i] > max) { max=num[i]; } } average=sum/num.length; system.out.println(string.format("average=%.3d", average)); system.out.println(string.format("min=%.3d", min)); system.out.println(string.format("max=%.3d", max)); system.out.println(string.format("sum=%.3d", sum));
also, replaced ints doubles, wished :)
Comments
Post a Comment