你是要思路还是整个代码文件?思路的话,其实挺简单的。
首先,写一个Chciken类,里面有一个比赛方法,带2个参数,然后再方法中判断这2个参数,然后返回一个数值类型。赢了就返回1,输了就返回0.
然后,写一个world类,里面用个main方法,然后先定义一个数值变量,就是你赢的次数,默认给0.然后用那个变量 += 方法,如果方法返回1,则该变量会加个1
最后,打印出那个变量即可。
如果需要代码,请再回复。
别的先不说,define后面怎么能用逗号隔开呢?这不就变成逗号表达式了吗?还有既然是C++干嘛要用stdio.h和define呢?用iostream和const多好?C和C++风格混用很容易出事儿的。。。。
把你的问题代码发出来啊
你能给代码吗!
wrold.java
public class world {
public static void main(String[] args) {
Chicken cluck = new Chicken();
cluck.playgame();
}
}
--------------------------------Chicken .java
import java.util.Random;
import java.util.Scanner;
public class Chicken {
public int compchoice;
private final static String MSG = "Enter 0, 1 or 2 for paper, scissors, rock";
private final String[] ary = {"Paper", "Scissors", "Rock"};
public void playgame() {
int count = 0;
Scanner scanner = new Scanner(System.in);
Random randomGenerator = new Random();
int winCount = 0;
while(count++ < 4){
System.out.println(MSG);
int input = scanner.nextInt();
compchoice = randomGenerator.nextInt(3);
System.out.println("My choice was" + compchoice);
if(input == compchoice){
System.out.println("Draw");
continue;
}
System.out.print(ary[input] + " blunts " + ary[compchoice] + ":");
String result = null;
if(input == 0){
if(compchoice == 1){
result = "I win";
}else{
result = "You win";
winCount++;
}
}else if(input == 1){
if(compchoice == 0){
result = "You win";
winCount++;
}else{
result = "I win";
}
}else if(input == 2){
if(compchoice == 0){
result = "You win";
winCount++;
}else{
result = "I win";
}
}
System.out.println(result);
}
System.out.println("Number of times you won: " + winCount);
}
}
--------------------testing result
Enter 0, 1 or 2 for paper, scissors, rock
1
My choice was2
Scissors blunts Rock:I win
Enter 0, 1 or 2 for paper, scissors, rock
2
My choice was2
Draw
Enter 0, 1 or 2 for paper, scissors, rock
0
My choice was0
Draw
Enter 0, 1 or 2 for paper, scissors, rock
1
My choice was0
Scissors blunts Paper:You win
Number of times you won: 1