1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| import java.util.Scanner;
import java.io.*;
public class moyennePreFinal {
public static void ecriture(float[][] tab) throws IOException {
FileWriter monFichier = null;
BufferedWriter tampon = null;
try {
monFichier = new FileWriter("essai.txt", true);
tampon = new BufferedWriter(monFichier);
for(int i = 0 ; i < tab[0].length ; i++) {
tampon.write(tab[0][i] + "\n");
tampon.write(tab[1][i] + "\n");
}
}catch(IOException e) {
e.printStackTrace();
System.out.println("error fatal");
}
finally {
try {
tampon.flush();
tampon.close();
monFichier.close();
}catch(Exception e1) {
System.out.println("error fatal 2");
e1.printStackTrace();
}
}
}
public static float[][] lecture(float[][] tab) throws IOException {
tab = new float[2][1000];
FileReader monFichier = null;
BufferedReader tampon = null;
try {
monFichier = new FileReader("essai.txt");
tampon = new BufferedReader(monFichier);
int i = 0;
while(true) {
String lecteurOctet = tampon.readLine();
if(lecteurOctet == null)
break;
float note = Integer.valueOf(lecteurOctet).floatValue();
tab[0][i] = note;
i++;
lecteurOctet = tampon.readLine();
if(lecteurOctet == null)
break;
float note1 = Integer.valueOf(lecteurOctet).floatValue();
tab[1][i] = note1;
i++;
}
for(int y = 0 ; y < tab[0].length ; y++) {
if(tab[1][y] == 0)
tab = new float[2][y];
break;
}
}catch(IOException e) {
e.printStackTrace();
System.out.println("error fatal");
}
finally {
try {
monFichier.close();
tampon.close();
}catch(Exception e1) {
System.out.println("error fatal 2");
e1.printStackTrace();
}
}
return tab;
}
public static float[] transformer(float[][] tab1, float[] tab2, int nombreDeNote) {
float resultat;
float dividende;
float diviseur;
for(int i = 0; i < nombreDeNote; i++) {
dividende = tab1[0][i];
diviseur = tab1[1][i];
resultat = dividende / diviseur*20;
tab2[i] = resultat;
}
return tab2;
}
public static void creationTableau(float[] tabis) {
int i = 0;
do {
System.out.println("" + tabis[i]);
i++;
}while(i < tabis.length);
}
public static void main(String[] args) throws IOException{
Scanner sc2 = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
float moyenne;
float somme = 0;
System.out.println("Programme de moyenne");
System.out.println("choisis le nombre de note");
int str = sc.nextInt();
System.out.println("Ecrivez vos notes, appuyez sur entree et ecrivez sur combien elle est et rappuyé sur entrée: ");
float str1[][] = new float[2][str];
float tableauFinal[] = new float[str];
for(int i = 0; i < str; i++) {
System.out.println("note: ");
float str2 = sc1.nextFloat();
str1[0][i] = str2;
System.out.println("sur: ");
float str3 = sc2.nextFloat();
str1[1][i] = str3;
}
ecriture(str1);
lecture(str1);
transformer(str1, tableauFinal, str);
for(int i = 0; i < str; i++) {
somme = somme + tableauFinal[i];
}
System.out.println("vous avez rentré");
creationTableau(tableauFinal);
moyenne = somme/str;
System.out.println("votre moyenne est " + moyenne + " sur 20");
}
} |
Partager