2016-02-09 130 views
1

我是新来的Java和我试图做一个程序,要求用户输入:如何获取用户输入的最小值和最大值? (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: "); 
    } 
} 

回答

0
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 
    } 
+0

这工作,非常感谢! –

+0

很高兴帮助。 –

0

2:你递增if声明,检查其是否有效外面总。因此,您的无效值200不会排除在外。

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

+0

谢谢,我会试试看! –

0

您需要将行添加到总数并将i增加到if(note>=1 && note<=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); 
    } 
} 
+0

感谢您的解释和使用西班牙文单词:D,我已经设法做到了! –

1

创建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. 
} 

最终打印的最小和最大可变

+0

完美!刚刚尝试过,感谢队友! –

相关问题