我如何去下面的标签?

所以我有这个代码,我希望当用户键入A来激活while(攻击)时,它低于此代码,因此它不能继续攻击; 我放置了一个名为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 labelfunction。 当您有内部循环并且需要breakcontinue外部循环时使用标签,如下例所示:

 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