Exercice Java permutation du contenu de deux variables

[tab name=’Exercice Java’]

Implémentez en Java un algorithme vous permettant de permuter les valeurs de deux variables..

Exemple d’exécution:

  Entrez x: 1
  Entrez y: 2
  Avant permutation:
  x : 1
  y : 2
  Après permutation:
  x : 2
  y : 1

[/tab][tab name=’Correction’]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;
class Permutation {
    private static Scanner scanner = new Scanner(System.in);
 
    public static void main(String[] args) {
        System.out.print("Entrez x: ");
        int x = scanner.nextInt();
 
        System.out.print("Entrez y: ");
        int y = scanner.nextInt();
 
        System.out.println("Avant permutation: ");
        System.out.println("x : " + x);
        System.out.println("y : " + y);
 
        int tmp = x;
        x = y;
        y = tmp;
 
        System.out.println("Après permutation: ");
        System.out.println("x : " + x);
        System.out.println("y : " + y);
    }
}

[/tab][end_tabset skin= »ginger » ]

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Besoin d'aide ?