/* * Fichier: Calculs.java * Crée le: 30 novembre 2006. * Modifié: 7 juillet 2007. * Auteurs: Sébastien ESTIENNE. * SiteWeb: http://www.prog-info.org/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ package chapitre1; import javax.swing.JOptionPane; /** *
Effectue des opérations arithmétiques.
* @author Sébastien ESTIENNE. */ public class Calculs { /** *Débute l'exécution de l'application.
* @param args Les paramètres de la ligne de commande. */ public static void main(String[] args) { // Chaines saisies par l'utilisateur. String premierNombre = "", deuxiemeNombre = "", troisiemeNombre = ""; // Chaine de sortie. String resultat = ""; // Nombres correspondants aux chaines saisies. int nombre1 = 0, nombre2 = 0, nombre3 = 0; // Lis 3 nombres saisis par l'utilisateur sous forme de String. premierNombre = JOptionPane.showInputDialog("Entrer le premier entier :"); deuxiemeNombre = JOptionPane.showInputDialog("Entrer le deuxième entier :"); troisiemeNombre = JOptionPane.showInputDialog("Entrer le troisième entier :"); // Convertir les nombres du type String au type int. try { nombre1 = Integer.parseInt(premierNombre); nombre2 = Integer.parseInt(deuxiemeNombre); nombre3 = Integer.parseInt(troisiemeNombre); } // Si au moins une des trois valeurs n'est pas un nombre entier, cela génère exception qui // implique un traitement particulier. catch(NumberFormatException e) { // Afficher le message d'erreur. JOptionPane.showMessageDialog(null, "Erreur, les 3 saisies doivent être des nombres entiers.", "Calculs", JOptionPane.ERROR_MESSAGE); // Terminer l'application. System.exit(0); } // Nombres entrés par l'utilisateur. resultat = "Vous avez saisi les nombres : " + nombre1 + ", " + nombre2 + " et " + nombre3 + ".\n"; // Calculer la somme. resultat = resultat + "La somme est : " + (nombre1 + nombre2 + nombre3) + ".\n"; // Calculer la moyenne. resultat = resultat + "La moyenne est : " + ((nombre1 + nombre2 + nombre3) / 3) + ".\n"; // Calculer le produit. resultat = resultat + "Le produit est : " + (nombre1 * nombre2 * nombre3) + ".\n"; // Afficher les résultat. JOptionPane.showMessageDialog(null, resultat, "Calculs", JOptionPane.INFORMATION_MESSAGE); // Terminer l'application. System.exit(0); } }