本文目录一览:
java中if 循环的问题。
package pk;
import java.util.*;
public class Guess {
int playerScore = 0;// 玩家初始积分
int pcScore = 0;// 电脑初始积分
Scanner input = new Scanner(System.in);
/**
* 选择玩家角色
*
* @param a
* @return
*/
public String name(int a) {
String playName = "";
switch (a) {
case 1:
playName = "雪糕";
break;
case 2:
playName = "冰糕";
break;
case 3:
playName = "棒冰";
break;
}
return playName;
}
/**
* 选择玩家出拳
*
* @param b
* @return
*/
public String guess(int b) {
String guess = "";
switch (b) {
case 1:
guess = "剪刀";
break;
case 2:
guess = "石头";
break;
case 3:
guess = "布";
break;
}
return guess;
}
/**
* 游戏开始
*/
public void play() {
System.out.println("**********欢迎来猜拳游戏**************\n\n");
System.out.println("*******1:剪刀 2:石头 3:布*******");
System.out.println("**********3局定胜负**********\n");
//
System.out.println("你选择角色:1:雪糕 2:冰糕 3:棒冰");// 玩家选人
int playerName = input.nextInt();
System.out.println("你选择:" + name(playerName));
int pcName = (int) (Math.random() * 3 + 1);
System.out.println("电脑选择:" + name(pcName) + "\n");// 电脑选人
//
//
int i = 0;// 初始次数
do {
i++;
;
System.out.println("请选择你要出的拳:");// 玩家出拳
int playerGuess = input.nextInt();
System.out.println("你出的拳:" + guess(playerGuess));
int pcGuess = (int) (Math.random() * 3 + 1);// 电脑出拳
System.out.println("电脑的拳:" + guess(pcGuess) + "\n");
//
if (playerGuess == pcGuess) {
System.out.println("平局!!");
} else if ((playerGuess == 1 pcGuess == 3)
|| (playerGuess == 2 pcGuess == 1)
|| (playerGuess == 3 pcGuess == 2)) {
System.out.println(name(playerName) + "你赢了!\n");
playerScore = playerScore + 1;
pcScore = pcScore - 1;
} else {
System.out.println(name(playerName) + "你输了!\n");
playerScore = playerScore - 1;
pcScore = pcScore + 1;
}
//
while (i == 3) {
System.out.println(name(playerName) + "是否继续(y/n)?");// 是否继续
String in = input.next();
if (in.equals("y")) {
i = 0;
} else {
i++;
}
}
} while (i = 3);
//
System.out.println(name(playerName) + "的积分" + playerScore);
System.out.println(name(pcName) + "的积分" + pcScore);
}
}
/*----------测试类-------------*/
package pk;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
Guess py=new Guess();
py.play();
}
}
这个我很久很久以前很久以前很久写的! 一个猜拳游戏
怎么在if条件成立的时候马上进入下一轮循环
给你看一个例子;
for(j=2;j=100;j++)
{
if(j%2==0)
continue;//到这里直接进入下一轮循环;
coutj"是奇数"endl;
}
java 中 while里有个if 想跳过本次循环继续,需要再if里写什么?
写continue;
continue是表示结束本次循环,继续下一次循环。
break表示结束整个循环,继续执行循环后面的代码。