tadawit
VIB
Bonsoir,
Je rencontre un petit problème en Java, la moyenne que j'essaye de calculer ne correspond pas aux valeurs de mon array!
Je pense que le problème est au niveau de l'itération non ?
Voici ce que j'ai codé tout à l'heure...
Je rencontre un petit problème en Java, la moyenne que j'essaye de calculer ne correspond pas aux valeurs de mon array!
Je pense que le problème est au niveau de l'itération non ?
Voici ce que j'ai codé tout à l'heure...
Code:
import java.util.Scanner;
public class lol {
public static void main(String[] args) {
Scanner enter = new Scanner(System.in);
int sousmenu = 0;
boolean arret = false;
int tab[] = {3,16,32,67,178,13};
int somme = 0;
float moyenne = 0;
while (!arret){
System.out.println("1 - le nombre le plus Grand : ");
System.out.println("2 - le nombre le plus Petit :");
System.out.println("3 - la somme des nombres : ");
System.out.println("4 - la moyenne des nombres : ");
System.out.println("5 - Afficher la différence entre le plus Grand et le plus Petit : ");
System.out.println("6 - Afficher les nombres pairs : ");
System.out.println("7 - Afficher les nombres impairs : ");
System.out.println("8 - Afficher le tableau : ");
System.out.println("9 - Quitter");
System.out.println("--------------------");
System.out.print("Taper votre choix svp : ");
sousmenu = enter.nextInt();
switch(sousmenu) {
case 1: {
int grand = 0;
System.out.println("---Option 1---");
for (int i = 0; i < tab.length; i++) {
if (tab[i] > grand) {
grand = tab[i];
}
}
System.out.println("le nombre le plus grand est => " + grand);
System.out.println("--------------------");
}
break;
case 2: {
int petit = 99;
System.out.println("---Option 2---");
for (int i = 0; i < tab.length; i++) {
if (tab[i] < petit) {
petit = tab[i];
}
}
System.out.println("le nombre le plus petit est => " + petit);
System.out.println("--------------------");
}
break;
case 3: {
System.out.println("---Option 3---");
for (int i = 0; i < tab.length; i++) {
somme = somme + tab[i];
}
}
System.out.println("le nombre Total est => " + somme);
System.out.println("--------------------");
break;
case 4 : {
System.out.println("---Option 4---");
for (int i = 0; i < tab.length; i++) {
somme = somme + tab[i];
}
for (int j = 0; j < tab.length; j++) {
moyenne = somme / tab.length;
}
}
System.out.println("la Moyenne est => " + moyenne);
System.out.println("--------------------");
break;