2_Algo_Java_Exemples
1
Déclaration, affectation, E/S
Algo test
Action principale
Constantes :
PREM = 1 : entier
DEUZE = 2.8 : réel
Variables : i : entier t(1..10) de réel
Début
t(1) <- PREM t(2) <- DEUZE t(3) <- 15.12 * 6 + 100
Afficher "Valeur de t(4) ?"
Saisir t(4) i <- 3
Afficher "t(",i,") contient :"
, t(i)
Fin Action
2
2.1
import java.io.*; public class Java1 {
// constantes static final int PREM = 1; static final double DEUZE = 2.8; public static void main(String[] args) throws IOException{
//variables
int i; double[] t = new double[11];
//indice de 0 à 10
//debut
t[1] = PREM; t[2] = DEUZE; t[3] = 15.12 * 6 + 100;
System.out.println(
"Entre la valeur de t(4) ?"); t[4] = Clavier.saisie_double(); i = 3;
System.out.println(
"t(" + i + ") contient :"
+ t[i]);
}
}
Structures de base
Tests
2.1.1 Si
Si a = 0 b <c <Sinon d <z <Finsi
Alors
3
7
5
8
if (a == 0) { b = 3; c = 7;
} else { d = 5; z = 8;
}
1
Denis B RODARD
Codage en Java
2/3
2.1.2 Selon
Selon choix
= 1 : Appel sp1
= 2 : b <- 6 c <- 7
= 3 : Afficher "Erreur !"
Autrement :
Afficher "C’est grave."
FinSelon
2.2
switch (choix) { case 1 : sp1(); break; case 2 : b = 6; c = 7; break; case 3 :
System.out.println("Erreur !"); break; default :
System.out.println("C’est grave."); break; }
Boucles
2.2.1 Tant que a <- .faux b <- 12
Tq (non a) et (b > 0) b <- sp(a, b)
Ftq
a = false; b = 12; while (!a && b > 0) { b = sp(a, b);
}
2.2.2 Répéter ... jusqu’à a <- .faux b <- 12
Répéter
b <- sp(a, b)
Jusqu’à (a) ou (b <= 0)
a = false; b = 12; do { b = sp(a, b);
} while (!a && b > 0);
2.2.3 Pour
Pour i de 1 à 10
Afficher i, "au carré = ", i^2
Finpour
for (i=1 ; i <= 10 ; i++) {
System.out.println(
i + " au carré = " + Math.pow(i, 2) );
}
Denis B RODARD
3
3.1
Codage en Java
3/3
Sous programmes
Procédure
Action Principale
...
Appel calc(a, b, c)
...
Fin Action
public static void main(String[] args) {
...
calc(a, b, c);
...
}
Action calc(x , y, z)
Données : x, y : entiers ; z : chaîne
Début
Si z = "+" Alors