【问题标题】:How can I get the min and max value of the user input? (Java)如何获取用户输入的最小值和最大值? (爪哇)
【发布时间】:2016-02-10 01:33:51
【问题描述】:

我是 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: ");
    } 
}

【问题讨论】:

    标签: java variables max


    【解决方案1】:
        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
         }
    

    【讨论】:

      【解决方案2】:

      2:您在检查其是否有效的if 语句之外增加总数。因此,您的无效值 200 不会被排除在外。

      3:您需要在单独的变量中跟踪最大值和最小值。

      【讨论】:

      • 谢谢,我试试看!
      【解决方案3】:

      再创建 2 个变量

      //for your case if value is between 0 and 100
      int min = 101;
      int max = 0;
      
      if(note>=1 && note<=100) {
                total=total+note;
                i++;
      
          //checking minimum value
          if(note < min)
             min = note;
      
         //checking maximum value
          if(note > max)
             max = note;
      
       }else {
          System.out.println("Enter integer between 1 and 100");
          //not valid no.
       }
      

      最后打印 min 和 max 变量

      【讨论】:

        【解决方案4】:

        您需要移动要添加到总计的行并将i 增加到if(note&gt;=1 &amp;&amp; note&lt;=100) 的第一部分。您还可以通过添加另外 2 个变量来跟踪最大/最小值:

        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;
                //added min/max
                int baja=-1;
                int alta=-1;
                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) {
                            //check for min
                            if(note<baja || baja==-1){
                                baja = note;
                            }
                            //check for max
                            if(note>alta || alta==-1){
                                alta = note;
                            }
                            //moved from below
                            total=total+note;
                            i++;
                        } else {
                            //como elimino la ultima nota introducida
                            System.out.println("Ingrese valor entre 1 y 100.");
                            number++;
                        }
                    }
                } 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: " + alta);
                System.out.println("La nota mas baja es: " + baja);
            }
        }
        

        【讨论】:

        • 感谢您的解释和使用西班牙语单词:D,我已经成功了!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-12
        • 1970-01-01
        • 1970-01-01
        • 2020-02-28
        • 2020-08-16
        • 2014-12-31
        • 2021-08-05
        相关资源
        最近更新 更多