How do you reverse the variables?

Asked

Viewed 413 times

-7

Write a program in Java with what is requested below.

A - Declare the variables X, Y and Z of type int. B - Assign 10 to variable X. C - Assign 2 to variable Y. D - With the help of the variable Z invert the values of X and Y. E - Print the values of variables X, Y and Z respectively.

obs. I’m starting the Java course now and I’m not getting to ask this question, can anyone help me?

  • 1

    Demonstrate that you had some effort and show what you tried by editing the question and adding, even if you did not request the problem.

  • 1

    What have you tried? What difficulty are you having? Post your code. Stackoverflow is not a site to search for ready-made codes

  • Add a minimum, complete, verifiable example https://answall.com/help/mcve

1 answer

-2


Try this way:

public int X,Y,Z;

X = 10;
Y = 2;

//Inversão
Z = Y; //Z = 2
Y = X; //Y = 10
X = Z; //X = 2

//Assim inverteu-se os valores de X e Y
  • 1

    Do you think it’s cool to have a ready-made code that you’re learning from? That way you’re not helping anyone learn anything

  • But what code is ready? This is a simple inversion problem, how else would it help you? I didn’t just post code, I was explaining what was happening. It’s so bad to learn from examples?

  • 1

    Yes Voce gave a code ready, nor waited for the author to edit the question with what he tried. or explain what was his doubt, specifically.

  • I can’t figure out why you guys spend more time on these things than helping the poor kid. I was just practical and tried to help him the best I could. I think he’s going to learn, because I was explaining what was going on, I didn’t give the code away. Have you noticed that in four comments from you, none of them helped the boy in his doubt?

  • 1

    Did you read the two comments in the question? The site does not work like this, here we help those who demonstrate effort to learn, the question of the way it is, even if it is not the intention of the author, seems request code for school exercise, thing that is not accepted on the site. Therefore we encourage him to show what he tried, and try to help him in his doubt, and not deliver ready, Oce is not helping anyone by delivering the answer of an exercise, instead of clarifying the difficulty of who is doing

  • Maybe I was wrong, I was just trying to help

Show 1 more comment

Browser other questions tagged

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