Académique Documents
Professionnel Documents
Culture Documents
Sans commentaire...
/* A */
String s = String.valueOf(i);
System.out.println("rsultat: \"" + s + "\"");
/* B */
i = Integer.parseInt(s) + 1;
System.out.println("rsultat: " + i);
/* C */
double x = Double.parseDouble(s) + 1;
System.out.println("rsultat: " + x);
/* D */
/* E */
/* F */
/* G */
i = s1.indexOf(s2);
if (i >= 0)
s1 = s1.substring(0, i) + s1.substring(i + s2.length());
System.out.println("s1 sans s2 : '" + s1 + "'");
/* I */
1.4. Random
public class RandomUn {
/* question A */
static long factorielle1(int n) {
long result = 1;
while (n > 0)
result *= (n--);
return result;
}
/* question B */
static BigInteger factorielle2(int n) {
BigInteger result = BigInteger.ONE;
while (n > 0)
result = result.multiply(BigInteger.valueOf(n--));
return result;
}
System.out.println("Test 1");
long factPrec1 = 1;
for (int n = 1; n < 25; n++) {
long fact = factorielle1(n);
System.out.println(" " + n + " ! = " + fact
+ " (rapport: " + fact / factPrec1 + ")");
factPrec1 = fact;
}
System.out.println("Test 2");
BigInteger factPrec2 = BigInteger.ONE;
for (int n = 1; n < 100; n++) {
BigInteger fact = factorielle2(n);
System.out.println(" " + n + " ! = " + fact
+ " (rapport: " + fact.divide(factPrec2) + ")");
factPrec2 = fact;
}
}
}
// deuxime manire
Date d = new Date();
System.out.println("maintenant: " + d);
// troisime manire
String[] jour = { null, "Dimanche", "Lundi", "Mardi", "Mercredi",
"Jeudi", "Vendredi", "Samedi" };
String[] mois = { "janvier", "fvrier", "mars", "avril", "mai",
"juin", "juillet", "aot", "septembre", "octobre", "novembre",
"dcembre" };
Calendar c = Calendar.getInstance();
System.out.println("maintenant: " + c.get(Calendar.HOUR) + " heures
"
+ c.get(Calendar.MINUTE) + ", le "
+ jour[c.get(Calendar.DAY_OF_WEEK)] + " "
+ c.get(Calendar.DAY_OF_MONTH) + " "
+ mois[c.get(Calendar.MONTH)] + " " +
c.get(Calendar.YEAR));
La troisime manire est la plus souple, mais la plus laborieuse. Une qualit de la
quatrime manire est quelle intgre les particularits locales. Ainsi, par dfaut, on
obtient 14/11/04 11:31, laffichage court la franaise .
int nombre = 0;
float somme = 0;