/* * Fichier: Decryptage.java * Crée le: 03 décembre 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 chapitre3; import java.text.DecimalFormat; import javax.swing.JOptionPane; /** *
Décrypte un nombre saisi par l'utilisateur.
* @author Sébastien ESTIENNE. */ public class Decryptage { /** *Débute l'exécution de l'application.
* @param args Les paramètres de la ligne de commande. */ public static void main(String[] args) { // Le code entré par l'utilisateur. String sCode = ""; // Le code converti. int code = -1; // Les différents chiffres du nombre. int chiffre1, chiffre2, chiffre3, chiffre4; // Le code décrypté. int nombre; // Sert à afficher les nombres sur 4 chiffres. DecimalFormat quatreChiffres = new DecimalFormat("0000"); do { // Demander à l'utilisateur le code à 4 chiffres. sCode = JOptionPane.showInputDialog("Entrez un nombre de 4 chiffres :\n\n"); // Convertir sNombre de String en int. try { code = Integer.parseInt(sCode); } // Si la valeur n'est pas un nombre entier, cela génère une exception qui implique un // traitement particulier. catch(NumberFormatException e) { // Ne rien faire. } } while(sCode == null || sCode.length() != 4 || code == -1); // Récupérer les 4 chiffres du nombre dans les différentes variables. chiffre1 = code / 1000; chiffre2 = code / 100 % 10; chiffre3 = code / 10 % 10; chiffre4 = code % 10; // Appliquer la première partie du décryptage ( c = (c + 3) % 10 ). chiffre1 = (chiffre1 + 3) % 10; chiffre2 = (chiffre2 + 3) % 10; chiffre3 = (chiffre3 + 3) % 10; chiffre4 = (chiffre4 + 3) % 10; // Appliquer la première partie du décryptage : inverser les chiffres 1, 3 et 2, 4 du code. nombre = chiffre3 * 1000 + chiffre4 * 100 + chiffre1 * 10 + chiffre2; // Afficher le résultat du décryptage. JOptionPane.showMessageDialog(null, "Le nombre " + quatreChiffres.format(code) + " décrypté donne " + quatreChiffres.format(nombre), "Décryptage", JOptionPane.INFORMATION_MESSAGE); // Terminer l'application. System.exit(0); } }