Java 如何使代码有条件地循环回其开始?
我是Java的初学者。我在大学第一门课程的第二周,已经爱上了编程。我一直在尝试使用我在课堂和实验室学到的一些东西来创建一个小游戏,但我遇到了一些问题 代码如下:Java 如何使代码有条件地循环回其开始?,java,loops,if-statement,while-loop,Java,Loops,If Statement,While Loop,我是Java的初学者。我在大学第一门课程的第二周,已经爱上了编程。我一直在尝试使用我在课堂和实验室学到的一些东西来创建一个小游戏,但我遇到了一些问题 代码如下: public class MathGame { public static void main(String args[]){ System.out.println("~~~~Welcome to ____!~~~~~"); System.out.println("Press 'S' to continue");
public class MathGame {
public static void main(String args[]){
System.out.println("~~~~Welcome to ____!~~~~~");
System.out.println("Press 'S' to continue");
char startGame;
char correctStart = 'S';
startGame = TextIO.getChar();
if (startGame == correctStart){
System.out.println("Both you and the computer will start with 20 lives"); //Game explanation
System.out.println("Use different move options to reduce your opponents life");
System.out.println("First one down to zero loses!");
System.out.println("OK here we go:");
int lifeTotal1 = 10;
int lifeTotal2 = 10;
int turnNum = 0;
turnNum ++;
System.out.println("Start of turn: " + turnNum);
System.out.println("Your life total is " + lifeTotal1 + " your opponents life total is " + lifeTotal2); //Starts turn
System.out.println("Select a move:");
System.out.println("Press 1 to do 3 damage");
System.out.println("Press 2 to do a random amount of damage");
System.out.println("Press 3 to heal 2 damage");
int userAttack1;
userAttack1 = TextIO.getInt();
if(userAttack1 == 1){ //User attack #1 selected
lifeTotal2 -=3;
}
if(userAttack1 == 2){ //User attack #2 selected
double random1 = Math.random();
if (random1 > 0 && random1 <= .2){
lifeTotal2 -= 1;
}
if (random1 > .2 && random1 <= .4){
lifeTotal2 -= 2;
}
if (random1 > .4 && random1 <= .6){
lifeTotal2 -= 3;
}
if (random1 > .6 && random1 <= .8){
lifeTotal2 -= 4;
}
if (random1 > .8 && random1 <= 1){
lifeTotal2 -=5;
}
}
if (userAttack1 == 3){
lifeTotal1 += 2;
}
System.out.println("End of turn");
System.out.println("Your current health is " + lifeTotal1);
System.out.println("Your opponents current health is " + lifeTotal2);
if (lifeTotal1 <= 0){
System.out.println("Game over, you lose");
if (lifeTotal2 <= 0){
System.out.println("Game over, you win");
}
}else{
公共类数学游戏{
公共静态void main(字符串参数[]){
System.out.println(“~~~~~欢迎来到”~~~~~”;
System.out.println(“按S键继续”);
char startGame;
char correctStart='S';
startGame=TextIO.getChar();
如果(startName==correctStart){
System.out.println(“你和计算机都将从20条生命开始”);//游戏说明
println(“使用不同的移动选项来减少对手的生命”);
System.out.println(“第一个降到零的丢失!”);
System.out.println(“好了,我们开始吧:”);
int-lifeTotal1=10;
int-lifeTotal2=10;
int-turnNum=0;
turnNum++;
System.out.println(“回合开始:+turnNum”);
System.out.println(“你的生命总数为“+lifeTotal1+”你的对手的生命总数为“+lifeTotal2”);//开始回合
System.out.println(“选择移动:”);
System.out.println(“按1键造成3点损坏”);
System.out.println(“按2进行随机数量的损坏”);
System.out.println(“按3键治疗2点伤害”);
int userAttack1;
userAttack1=TextIO.getInt();
如果选择了(userAttack1==1){//User attack#1
寿命总数2-=3;
}
如果选择了(userAttack1==2){//User attack#2
double random1=Math.random();
如果(random1>0&&random1.2&&random1.4&&random1.6&&random1.8&&random1假设您从1迭代到10,并且在某个迭代中需要再次启动循环,那么您可以这样做
for(int i=1 ;i<=10; i++){
if(condition) {
// This will make sure that the loop starts from the beginning.
i=0;
}
}
for(int i=1;iwhile循环有助于迭代,直到满足某个条件。这将循环,直到其中一个总寿命达到0
//Game set up code before your loop
while (lifeTotal1 > 0 && lifeTotal2 > 0) {
// Any code in here will repeat until one life total hits 0
}
//Code here to handle the game being over
Awhile loop
是循环代码的常用方法。天哪,这个循环将是无限的,你有什么建议?@Andremoniy:我只是说这个条件是为了解释。在真实的程序中,会有真实的条件,那么我应该在哪里把它添加到我的代码中呢?有没有办法让循环取决于玩家的总生命比整数还多?哦,非常感谢你的help@b1boss:我只能给出一个提示。您需要解决整个程序。我希望您了解我为其编写的示例程序背后的逻辑you@b1boss没问题。接受我的回答就是我所需要的全部感谢!