在java中,如何再循环一段代码?

时间:2015-10-22 16:27:50

标签: java

我试图在java中制作基于文本的游戏,我希望能够随时打印玩家统计数据。游戏的工作方式是你有编号的选项来做出选择,你输入你想要的数字。但是,如果您键入99,则会打印出您的统计信息。但在那之后,你实际上无法做出选择。它只打印出统计数据,然后在代码中继续。我如何让它重新开始,以便玩家可以做出选择并询问统计数据?

以下是代码:

        if(one = true){//I want to start here again if you type in 99.
            int weaponchoice;
            weaponchoice = scan.nextInt();
            if(weaponchoice == 1){
                strength = 3;
                agility = 2;
                stealth = 0;
                weaponslot[0] = "Fusion Heated Shotgun";
                System.out.println(">New Weapon equiped.< ");
                System.out.println(">strength + 2, agility + 1, stealth - 1.< ");
                System.out.println("");
                System.out.println(">The weapon chache locks up.<");
                System.out.println("''What should I do now?'', you think to yourself.''");
                System.out.println("");
                System.out.println("To go to the Hangar, press 1");
                System.out.println("To leave the ship, leave the military, and go home, press 2.");
                System.out.println("To explore the ship, press 3");
                one = false;
                two = true;
            }
            if(weaponchoice == 2){
                accuracy = 3;
                strength = 2;
                agility = 0;
                weaponslot[0] = "High Impact Ballistic Sniper Rifle";
                System.out.println(">New Weapon equipped<");
                System.out.println(">Accuracy + 2, Strength + 1, Agility - 1<");
                System.out.println("");
                System.out.println(">The weapon cache locks up<");
                System.out.println("''What should I do now?'', you think to yourself.''");
                System.out.println("");
                System.out.println("To go to the Hangar, press 1");
                System.out.println("To leave the ship, leave the military, and go home, press 2.");
                System.out.println("To explore the ship, press 3");
                one = false;
                two = true;
            }
            if(weaponchoice == 3){
                stealth = 3;
                agility = 2;
                strength = 0;
                weaponslot[0] = "Silenced Lazer Pistol";
                System.out.println(">New weapon equipped.< ");
                System.out.println(">Stealth + 2, Agility + 1, Strength - 1<");
                System.out.println("''What should I do now?'', you think to yourself.''");
                System.out.println("To go to the Hangar, press 1");
                System.out.println("To leave the ship, leave the military, and go home, press 2.");
                System.out.println("To explore the ship, press 3");
                one = false;
                two = true;
            }
            if(weaponchoice == 4){
                agility = 2;
                accuracy = 2;
                stealth = 2;
                strength = 0;
                weaponslot[0] = "Multi Purpose Combat Knife";
                System.out.println(">You pick up the weapon.<");
                System.out.println(">agility + 1. Accuracy + 1. Stealth + 1. Strength - 1<");
                System.out.println("");
                System.out.println("''What should I do now?'', you think to yourself.''");
                System.out.println("");
                System.out.println("To go to the Hangar, press 1");
                System.out.println("To leave the ship, leave the military, and go home, press 2.");
                System.out.println("To explore the ship, press 3");
                one = false;
                two = true;
            }
            if(weaponchoice == 5){
                agility = 3;
                strength = 2;
                stealth = 0;
                weaponslot[0] = "Lazer Katana";
                System.out.println(">You pick up the weapon.<");
                System.out.println(">Agility +2. Stength +1. Stealth -1<");
                System.out.println("");
                System.out.println("''What should I do now?'', you think to yourself.''");
                System.out.println("");
                System.out.println("To go to the Hangar, press 1");
                System.out.println("To leave the ship, leave the military, and go home, press 2.");
                System.out.println("To explore the ship, press 3");
                one = false;
                two = true;
            }
            if(weaponchoice == 99){
                System.out.println("Health: " + health);
                System.out.println("Strength: " + strength);
                System.out.println("Agility: " + agility);
                System.out.println("Accuracy " + accuracy);
                System.out.println("Stealth " + stealth);
                //so my question is really how do I from here all the way back to the beginning of the code I've shown. 
            }           

1 个答案:

答案 0 :(得分:0)

使用while循环

while (true){//I want to start here again if you type in 99.
            int weaponchoice;
            weaponchoice = scan.nextInt();
            if(weaponchoice == 1){
                strength = 3;
                agility = 2;
                stealth = 0;
                weaponslot[0] = "Fusion Heated Shotgun";
                System.out.println(">New Weapon equiped.< ");
                System.out.println(">strength + 2, agility + 1, stealth - 1.< ");
                System.out.println("");
                System.out.println(">The weapon chache locks up.<");
                System.out.println("''What should I do now?'', you think to yourself.''");
                System.out.println("");
                System.out.println("To go to the Hangar, press 1");
                System.out.println("To leave the ship, leave the military, and go home, press 2.");
                System.out.println("To explore the ship, press 3");
                break;
            }
            if(weaponchoice == 2){
                accuracy = 3;
                strength = 2;
                agility = 0;
                weaponslot[0] = "High Impact Ballistic Sniper Rifle";
                System.out.println(">New Weapon equipped<");
                System.out.println(">Accuracy + 2, Strength + 1, Agility - 1<");
                System.out.println("");
                System.out.println(">The weapon cache locks up<");
                System.out.println("''What should I do now?'', you think to yourself.''");
                System.out.println("");
                System.out.println("To go to the Hangar, press 1");
                System.out.println("To leave the ship, leave the military, and go home, press 2.");
                System.out.println("To explore the ship, press 3");
                break;
            }
            if(weaponchoice == 3){
                stealth = 3;
                agility = 2;
                strength = 0;
                weaponslot[0] = "Silenced Lazer Pistol";
                System.out.println(">New weapon equipped.< ");
                System.out.println(">Stealth + 2, Agility + 1, Strength - 1<");
                System.out.println("''What should I do now?'', you think to yourself.''");
                System.out.println("To go to the Hangar, press 1");
                System.out.println("To leave the ship, leave the military, and go home, press 2.");
                System.out.println("To explore the ship, press 3");
                break;
            }
            if(weaponchoice == 4){
                agility = 2;
                accuracy = 2;
                stealth = 2;
                strength = 0;
                weaponslot[0] = "Multi Purpose Combat Knife";
                System.out.println(">You pick up the weapon.<");
                System.out.println(">agility + 1. Accuracy + 1. Stealth + 1. Strength - 1<");
                System.out.println("");
                System.out.println("''What should I do now?'', you think to yourself.''");
                System.out.println("");
                System.out.println("To go to the Hangar, press 1");
                System.out.println("To leave the ship, leave the military, and go home, press 2.");
                System.out.println("To explore the ship, press 3");
                break;
            }
            if(weaponchoice == 5){
                agility = 3;
                strength = 2;
                stealth = 0;
                weaponslot[0] = "Lazer Katana";
                System.out.println(">You pick up the weapon.<");
                System.out.println(">Agility +2. Stength +1. Stealth -1<");
                System.out.println("");
                System.out.println("''What should I do now?'', you think to yourself.''");
                System.out.println("");
                System.out.println("To go to the Hangar, press 1");
                System.out.println("To leave the ship, leave the military, and go home, press 2.");
                System.out.println("To explore the ship, press 3");
                break;
            }
            if(weaponchoice == 99){
                System.out.println("Health: " + health);
                System.out.println("Strength: " + strength);
                System.out.println("Agility: " + agility);
                System.out.println("Accuracy " + accuracy);
                System.out.println("Stealth " + stealth);
                //so my question is really how do I from here all the way back to the beginning of the code I've shown. 
            } 
        }