gpt4 book ai didi

java - 如何获取用户输入的最小值和最大值? ( java )

转载 作者:太空宇宙 更新时间:2023-11-04 12:57:06 24 4
gpt4 key购买 nike

我是 Java 新手,我正在尝试编写一个要求用户提供以下信息的程序:

  1. 学生人数(必须为 1 至 35 人)。我成功地做到了这一点。
  2. 每个学生的成绩(从 0 到 100)。我设法做到了这一点,但是如果有人引入了例如 200,系统会通知这是不允许的,但该变量仍然会影响成绩的平均值(我如何删除最后插入的变量?)。
  3. 最后,我想给出一个我设法做到的平均值,但我找不到显示最高成绩和最低成绩的方法。
<小时/>
public class ExtraClase {
public static void main(String[] args) {
Estudiantes estudiantes = new Estudiantes();
estudiantes.numeroEstudiantes();
}
}
<小时/>
public class Estudiantes {
public void numeroEstudiantes() {
System.out.print("Introduzca numero de estudiantes: ");
Scanner cantidad = new Scanner(System.in);
int number = cantidad.nextInt();
int i=1;
int total=0;
int promedio;
if(number>=1 && number<=35){
while(i<=number){
System.out.print("Ingrese la nota del 1-100: ");
Scanner nota = new Scanner(System.in);
int note = nota.nextInt();
if(note>=1 && note<=100) {
}else {
//como elimino la ultima nota introducida
System.out.println("Ingrese valor entre 1 y 100.");
number++;
}
total=total+note;
i++;
}
}else {
System.out.println("Digite numero entre 1 y 35.");
}
promedio=total/number;
System.out.println("El promedio de notas es: "+promedio);
System.out.println("La nota mas alta es: ");
System.out.println("La nota mas baja es: ");
}
}

最佳答案

    if(note>=1 && note<=100) {
total=total+note;
i++;

}else {
//como elimino la ultima nota introducida
System.out.println("Ingrese valor entre 1 y 100.");
//there's no need to increment number here, just don't increment i if a variable has invalid value
}

关于java - 如何获取用户输入的最小值和最大值? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35297822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com