How to exchange/exchange the value of two variables

Asked

Viewed 894 times

3

I’m having a hard time writing this exercise in the Java language, using the Eclipse IDE:

Read two values for variables A and B, exchange the values so that variable A has the value of variable B and variable B has the value of variable A. Display the exchanged values.

I know I need to declare Variable A, Variable B and An Auxiliary Variable!

In the case I can only get Variable A to keep the value of Variable B! However Variable B has its own value and does not show the Value of A which is what I need!

MY DOG:

import java.io.IOException;
import java.util.Scanner;

public class Variavel {

    public static void main(String[] args) throws IOException {

     int VarA;
     int VarB;
     int Aux;

     System.out.println("Digite a Primeira Variavel");

     Scanner Lera = new Scanner(System.in);
     VarA= Lera.nextInt();

     System.out.println("Digite a Primeira Variavel");
     VarB= Lera.nextInt();



     VarA=VarB;

     Aux=VarA;

     VarB=Aux;

     System.out.println("O valor de A é: " +VarB);
     System.out.println("O valor de B é: " +Aux);
     Lera.close();



    }

}

NOTE: I’m at the beginning of the JAVA Classes, so for now I only use basic commands like INT,Scanner,Read? I haven’t learned FOR,WHILE,IF,ELSE,

RESULT IMAGE

Tela Com O Resultado Usando Eclipse

2 answers

5

Your error was changing the value of the variable VarA before storing its value in the variable Aux:

 Aux=VarA;
 VarA=VarB;
 VarB=Aux;

This way it will work properly. Also change the display:

 System.out.println("O valor de A é: " +VarA);
 System.out.println("O valor de B é: " +VarB);

This way it will display the variables already with the values exchanged, as can be seen in ideone: https://ideone.com/Ur4ImH

Note that, in java, variable names should start with lower case letter, following the pattern Camel Case for compound names.

  • It presents different values, but does not alter them! Type First Variable 2 Type First Variable 6 The value of A is: 2 The value of B is: 6

  • @Carolm you want to display the exchanged values of the variables?

  • Articuno - Yes, that’s exactly it! A has to present the value of B and B has to present the value of A!

  • @Carolm corrected.

  • Article ,two questions out of context : How can I choose more than one answer as the best one? And how can I talk to who answered me (like a chat)?

  • @Carolm can only accept one, but you can vote for both, it’s up to you to choose :)

Show 1 more comment

4


Has two problems.

The way to exchange is wrong as well as the way you use the variables in the printlnalso.

Change to:

//Guarada o valor de A
Aux=VarA;
//A variável VarA, como já foi guardado o valor de A, pode agora receber o valor de B
VarA=VarB;
//O valor de A, anteriormente guardado na variável Aux, é atribuído à variável VarB
VarB=Aux;

//Como as variáveis têm agora os valores trocados deve usá-las no println na sequência correcta
System.out.println("O valor de A é: " +VarA);
System.out.println("O valor de B é: " +VarB);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.