我在重新分配x&的值时遇到了麻烦y以便在控制台日志中使它们成为正确的数字。虽然我很想使用其他数字,但我必须牢记以下规则。 规则:
您根本不可以更改或更改console.log语句。
var x = 2;
var y = 3;
var z = 0;
// your code here
x = (y+z);
// y = ?
console.log("The value of x is: " + x + " (x should be 3)");
console.log("The value of y is: " + y + " (y should be 2)");
答案 0 :(得分:0)
你的问题并不清楚,但我相信你正在寻找一个" swap"功能性:
var x = 2;
var y = 3;
var z = 0;
// your code here
z = x; // Temporarily store x in z
x = y; // Overwrite x with y
y = z; // Store the original value of x in y
console.log("The value of x is: " + x + " (x should be 3)");
console.log("The value of y is: " + y + " (y should be 2)");
答案 1 :(得分:0)
首先分配z
z = x
z现在是2 ...
x = y
x现在是3
y = z
现在是2
答案 2 :(得分:0)
public class SO35618914 {
public static void main(String[] args) {
int x = 10;
int y = 15;
x = x + y;
y = x - y;
x = x - y;
System.out.println("x: " + x + "; y: " + y);
}
}
答案 3 :(得分:0)
如果您尝试交换x
和y
,则无需z
var x = 2;
var y = 3;
// swap x and y
x = x + y;
y = x - y;
x = x - y;
console.log("The value of x is: " + x + " (x should be 3)");
console.log("The value of y is: " + y + " (y should be 2)");
输出:
x的值为:3
y的值为:2