我是第一次引用【do...while】循环,代码如下~希望大家指出我的错误或者漏洞~
import java.util.Random;
import java.util.Scanner;public class shaizi { /**
 * @param args
 * 
 */
public static void main(String[] args) {
System.out.println("请输入任意字符并回车开始游戏");
Scanner scan = new Scanner(System.in);
String a; 
Random w = new Random();
int computer = w.nextInt(6) + 1;
int player = w.nextInt(6) + 1;
int number = 0;
int c=0;
int p=0;
a = scan.next();
do {
System.out.println("电脑掷出了" + player + "点");
a = scan.next();
System.out.println("玩家掷出了 " + computer + "点");
number++;
if (player > computer) {
System.out.println("对不起,您输了,快给钱!!");
c++; } else if (player == computer) {
System.out.println("点数相等,双方各拿出20元给程序设计者");
} else {
System.out.println("恭喜啊~好不容易赢一次!!来,给你20块钱!");
p++;
}
System.out.println("是否查看结果?(y/n)");
a = scan.next();
if (a.equals("y")) //equals:等于。
{
System.out.println("总共玩了" + number + ",电脑赢了" + c
+ ",您赢了" + p + ".");
}
System.out.println("是否继续?(y/n)");
a = scan.next();
} while (a.equals("y"));
System.out.println("是否退出?(y/n)");
a = scan.next();
if (a.equals("y")) {
System.out.println("您已成功退出");
} else {
System.out.println("是否继续(y/n)");
a = scan.next();
}
while (scan.equals("y"));
}
}

解决方案 »

  1.   

    int computer;
    int player;
    int number = 0;
    int c=0;
    int p=0;
    a = scan.next();
    do {
    player = w.nextInt(6) + 1; System.out.println("电脑掷出了" + player + "点");
    a = scan.next();
    computer = w.nextInt(6) + 1; System.out.println("玩家掷出了 " + computer + "点");
    number++;
    if (player > computer) {
    System.out.println("对不起,您输了,快给钱!!");
    c++; }