急!!用java编写一个石头剪子布的游戏。 电脑从1-3中选择一个任意数。 (用数字代表石头剪子布
发布网友
发布时间:2022-07-29 03:14
我来回答
共2个回答
热心网友
时间:2023-11-18 08:28
import java.util.Scanner;
public class Kyo
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int count_computer = 0;
int count_user = 0;
int count_draw = 0;
int which = 0;
String[] arr = { "石头", "布", "剪刀" };
while(true)
{
System.out.print("1=石头, 2=布 , 3=剪刀; 你出什么: ");
String line = scanner.nextLine().trim();
try
{
which = Integer.parseInt(line);
}
catch(NumberFormatException nfe)
{
continue;
}
if(which < 0 || which > 3)
{
continue;
}
if(which == 0)
{
scanner.close();
System.out.println("用户胜利的次数: " + count_user);
System.out.println("电脑胜利的次数: " + count_computer);
System.out.println("平局的次数: " + count_draw);
break;
}
int rand = (int) (Math.random() * 3) + 1;
System.out.println("电脑出的是: " + arr[rand - 1]);
System.out.println("你出的是: " + arr[which - 1]);
if(rand > which)
{
System.out.println("winner is 电脑");
count_computer++;
}
else if(rand < which)
{
System.out.println("你是winner");
count_user++;
}
else
{
System.out.println("平局draw game");
count_draw++;
}
}
}
}
热心网友
时间:2023-11-18 08:29
这么简单