最近,当我遇到一些变量问题时,我试图自动编写javascript代码。
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变量会发生变化,但是" num"变量没有随新值一起更新。我知道我的思维过程一定有什么问题,因为依赖变量在Java中是如何工作的,但我不知道是什么。求救!
答案 0 :(得分:1)
也许您需要在循环中更新“num”变量,例如:
while (more.equals("y"))
{
...
// Add below codes
num1 = 0 + xCoord;
num2 = 5 + yCoord;
num3 = 13 + xCoord;
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();
}