2016-02-09 76 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

完美!剛剛嘗試過,感謝隊友! –

相關問題