-1
最近我試圖自動化JavaScript的編碼,當我遇到一些我的變量的問題。基於Java中的另一個int更改int
Scanner scan = new Scanner(System.in);
System.out.print("Enter x start coordinate: ");
int xCoord = scan.nextInt();
System.out.print("Enter y start coordinate: ");
int yCoord = scan.nextInt();
String more = "y";
int num1 = 0 + xCoord;
int num2 = 5 + yCoord;
int num3 = 13 + xCoord;
int num4 = 10 + yCoord;
System.out.println("ctx.moveTo(" + num1 + "," + num2 + ");"
+ "\nctx.lineTo(" + num3 + "," + num4 + ")\n"
+ "Mid Point: " + num3 + "," + num4);
System.out.print("Again?: ");
more = scan.next();
while (more.equals("y"))
{
System.out.println("Enter a direction (above, left, right): ");
String nextDir = scan.next();
if (nextCube.equals("right"))
{
xCoord += 13;
yCoord += 5;
}
else if (nextDir.equals("left"))
{
xCoord -= 13;
yCoord += 5;
}
else if (nextDir.equals("above"))
{
yCoord -= 18;
}
System.out.println("ctx.moveTo(" + num1 + "," + num2 + ");"
+ "\nctx.lineTo(" + num3 + "," + num4 + ")\n"
+ "Mid Point: " + num3 + "," + num4);
System.out.print("Again?: ");
more = scan.next();
}
我發現,要求朝的XCOORD和YCOORD變量的變化移動方向,但「民」的變量並沒有隨着新的值更新後。我知道在我的思維過程中一定有什麼問題會影響Java中依賴變量的工作方式,但我不知道是什麼。請幫助!
你有使用調試器了嗎? –
當你說「因變量」時,這聽起來像你認爲,當xCoord變化時,不知何故num1動態更新? – Bill
不知道爲什麼所有的downvotes。這是一個基於關於變量聲明如何工作的基本誤解的初學者問題,但它包含了回答它所需的所有元素。 –