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
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
– Carol M
@Carolm you want to display the exchanged values of the variables?
– user28595
Articuno - Yes, that’s exactly it! A has to present the value of B and B has to present the value of A!
– Carol M
@Carolm corrected.
– user28595
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)?
– Carol M
@Carolm can only accept one, but you can vote for both, it’s up to you to choose :)
– user28595