Pet*_*tha 0 java for-loop min while-loop
没有额外的错误。我只是无法打印最小的输入。
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Marks of a student(-1 to terminate the inputting marks): ");
int x=input.nextInt();
int total=0,large=0,y=0,small=0,z;
z=x;
while(x!=-1){
System.out.print("Marks of a student(-1 to terminate the inputting marks): ");
x=input.nextInt();
total+=x;
y++;
if(x>=large){
large=x;
}
if(x<small){
small=x;
}
}
int sum=total+z+1;
double avg=(double)total/y;
System.out.println("No of students: "+y);
System.out.println("Total marks : "+sum);
System.out.println("Maximum : "+large);
System.out.println("Minimum : "+small);
System.out.println("Average : "+avg);
}
Run Code Online (Sandbox Code Playgroud)
small用 初始化0,因此任何大于该值的输入都将被忽略。
一种方法是用 初始化它Integer.MAX_VALUE,因此任何输入的数字都会小于或等于它。同样,large可以用 初始化Integer.MIN_VALUE。
| 归档时间: |
|
| 查看次数: |
62 次 |
| 最近记录: |