在java中,如何再次循环一段代码?
我正在尝试用java制作一个基于文本的游戏,我希望能够随时打印玩家的统计数据。游戏的运作方式是,你有编号的选项来做出选择,你可以输入你想要的选项的编号。然而,如果你输入99,它会打印出你的统计数据。但在那之后,你实际上无法做出选择。它只是打印出统计数据,然后在代码中继续。我如何让它重新开始循环,让玩家做出选择并要求统计数据 代码如下:在java中,如何再次循环一段代码?,java,Java,我正在尝试用java制作一个基于文本的游戏,我希望能够随时打印玩家的统计数据。游戏的运作方式是,你有编号的选项来做出选择,你可以输入你想要的选项的编号。然而,如果你输入99,它会打印出你的统计数据。但在那之后,你实际上无法做出选择。它只是打印出统计数据,然后在代码中继续。我如何让它重新开始循环,让玩家做出选择并要求统计数据 代码如下: if(one = true){//I want to start here again if you type in 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.
}
if(one=true){//如果您键入99,我想再次从这里开始。
国际武器选择;
weaponchoice=scan.nextInt();
如果(武器选择==1){
强度=3;
敏捷度=2;
隐形=0;
武器槽[0]=“聚变加热猎枪”;
System.out.println(“>新装备的武器。<”);
System.out.println(“>力量+2,敏捷+1,隐形-1.<”;
System.out.println(“”);
System.out.println(“>武器缓存锁定。新武器装备精度+2,强度+1,敏捷性-1武器缓存锁定新装备的武器。<”;
系统输出打印(“>隐形+2,敏捷+1,强度-1你拿起武器。敏捷+1。准确度+1。隐形+1。强度-1你拿起武器。敏捷+2。强度+1。隐形-1在循环时使用
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.
}
}
while(true){//如果您键入99,我想再次从这里开始。
国际武器选择;
weaponchoice=scan.nextInt();
如果(武器选择==1){
强度=3;
敏捷度=2;
隐形=0;
武器槽[0]=“聚变加热猎枪”;
System.out.println(“>新装备的武器。<”);
System.out.println(“>力量+2,敏捷+1,隐形-1.<”;
System.out.println(“”);
System.out.println(“>武器缓存锁定。新武器装备精度+2,强度+1,敏捷性-1武器缓存锁定新装备的武器。<”;
System.out.println(“>隐身+2,敏捷+1,力量-1你拿起武器。敏捷+1。准确度+1。隐身+1。力量-1你拿起武器。敏捷+2。力量+1。隐身-1我如何让它重新开始循环,以便玩家可以做出选择并询问统计数据?你正在回答这个问题。试着对循环和数据做一些研究while循环。您应该双重(或三重)检查这一行:如果(one=true)
。将该代码放入while循环中-while(true){}或有助于循环的条件。它将继续重复。。。