所以我有这个代码,我想当用户键入A来激活在此代码下面的while(attack)时,它就不能继续攻击;我在while(attack)上方放置了一个名为Attack:的标签,但它无法识别,因为稍后会对其进行声明。
String M2 = JOptionPane.showInputDialog(Name2 + "'s Turn. Here are your options\n 1.Invade \n 2.Buy \n 3.End Turn \n 4.Check Money Balance \n 5.Check Soldier Count \n 6.Citizen's Hapinness \n 7.Owned Islands \n 8.Check Rules", "Type the Number of the action you want to take place"); if (M2.equals("1")) { String Inv=JOptionPane.showInputDialog(null, "Open up the map and Check the island that you are in! If you dont remember the islands name type B to go back and then go into Owned Islands and come back! Then see the attack option you have and choose where you want to attack.Type A to Attack"); if(Inv.equalsIgnoreCase("A")){ Attack=false; Attack=true; continue attack; }else{ continue P2Menu2; } }
这是播放器键入A时必须开始的代码。谢谢您的时间.. :)
//Attack Phase attack: while (Attack) { Random r = new Random(); int R = r.nextInt(6 - 1) + 1; int R2 = r.nextInt(6 - 1) + 1; int R3 = r.nextInt(6 - 1) + 1; int R4 = r.nextInt(6 - 1) + 1; int R5 = r.nextInt(6 - 1) + 1; int R6 = r.nextInt(6 - 1) + 1; int totalr[] = {R, R2, R3, R4, R5, R6};
标签不能像这样工作。在Java中,没有任何goto label功能。当您有内部循环且需要break或continue有外部循环时,将使用标签,如以下示例所示:
goto label
break
continue
outterloop: for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { // this would break the inner loop and go to the next outter loop iteration // break; // this would break the outter loop, thus exiting both loops // break outterloop; // this would jump to the next inner loop iteration // continue; // this would jump to the next outter loop iteration, exiting the inner loop // continue outterloop; } }
您需要的是在不使用标签的情况下改进代码结构以实现所需的功能。
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html