我如何去下面的标签? [英] How do i go to a label below?

查看:100
本文介绍了我如何去下面的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个代码,我希望当用户键入A来激活低于此代码的while(攻击),因此它不能继续攻击;我放置了一个名为attack的标签:上面的(攻击),但它不识别它,因为它稍后被声明。

So i have this code and i want when the user types A to activate the while(attack) which has is below this code so it doesnt work with continue attack; I placed a label called attack: above while(attack) but it does not recognize it because it gets declared later on.

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.感谢您的时间.. :)

This is the code that has to start when the player types A. Thank you for your time.. :)

 //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中,没有转到标签功能。当你有内部循环时需要使用标签,你需要 break 继续一个外部循环,如下例所示:

Labels don't work like this. In Java, there is no goto label functionality. Labels are used when you have inner loops and you need to break or continue an outer loop, like in the following example:

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; 
    }
}

您需要的是改善代码结构以实现你需要什么而不需要标签。

What you need is to improve your code structure to achieve what you want without the need of labels.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

这篇关于我如何去下面的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆