java问题,高手请进!

2024-12-05 00:18:07
推荐回答(3个)
回答(1):

import java.io.*;
import java.util.*;
import javax.xml.transform.dom.*;

import org.w3c.dom.Node;

public class X {

public static void main(String[] args) {
int userNum=0;
int aiNum=0;
Scanner s=new Scanner(System.in);
Map map=new HashMap();
map.put(0, "剪刀");
map.put(1, "石头");
map.put(2, "布");
List list=new ArrayList();
System.out.println("5局3胜制:0=剪刀 1=石头 2=布");
String info="";
for(int i=0;i<5;i++){
if(userNum>=3 || aiNum>=3) break;
System.out.println("请输入拳标志:");
int user=s.nextInt();
if(!map.containsKey(user)) {System.out.println("没有这个拳名");i--;continue;}
int ai=(int)(Math.random()*100)%3;
if(user==0 && ai==1){
info="用户:"+map.get(user)+" 电脑:"+map.get(ai)+" 电脑胜";
aiNum++;
}
else if(user==0 && ai==2){
info="用户:"+map.get(user)+" 电脑:"+map.get(ai)+" 用户胜";
userNum++;
}
else if(user==1 && ai==2){
info="用户:"+map.get(user)+" 电脑:"+map.get(ai)+" 电脑胜";
aiNum++;
}
else if(user==2 && ai==0){
info="用户:"+map.get(user)+" 电脑:"+map.get(ai)+" 电脑胜";
aiNum++;
}
else if(user==2 && ai==1){
info="用户:"+map.get(user)+" 电脑:"+map.get(ai)+" 用户胜";
userNum++;
}
else if(user==1 && ai==0){
info="用户:"+map.get(user)+" 电脑:"+map.get(ai)+" 用户胜";
userNum++;
}
else{
info="用户:"+map.get(user)+" 电脑:"+map.get(ai)+" 平局";
i--;
}
System.out.println("本局[ "+info+" ]");
list.add(info);

}
System.out.println("游戏结束");
System.out.println("----------------------------------");
for(String string:list){
System.out.println(string);
}
if(userNum>aiNum)
System.out.println("最终用户以"+userNum+"比"+aiNum+"战胜电脑");
else
System.out.println("最终用户以"+userNum+"比"+aiNum+"败北电脑");
System.out.println("----------------------------------");
s.close();
}

}

回答(2):

看看,你能不能理解,动态的哦。记着在查看结果的时候拉动一下窗口这样才能看见全部结果。
package testpackage;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Caiquan extends Applet implements ActionListener{

Label result=new Label();
Button b1=new Button("shi tou"),b2=new Button("jiandao "),b3=new Button("bu");
int data[][]={
{0,1,0},
{0,0,1},
{1,0,0}};
/** 石头 剪刀 布
*
* 石头 0 1 0
*
* 剪刀 0 0 1
*
* 布 1 0 0
*
* */

int user=0,computer=0;
int getcomputer()
{
return (int)(Math.random()*3);
}
public void init()
{
add(result);
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
int computerres=getcomputer();
if(e.getSource()==b1)
{
user+=data[0][computerres];
computer+=data[computerres][0];
}
if(e.getSource()==b2)
{
user+=data[1][computerres];
computer+=data[computerres][1];
}
if(e.getSource()==b3)
{
user+=data[2][computerres];
computer+=data[computerres][2];
}
result.setText("user win:"+user+"\tcomputer win:"+computer+"\t result(user win):"+(double)user/(double)(user+computer));
}
}

回答(3):

方法类:
package test;

public class CaiQuan {
private int computerFist=-10;
private int result = -1;
public CaiQuan() {
super();
this.computerFist = ((int) (Math.random()*1000))%3;;
}
public String getComputerFist()
{
String s = "石头";
if(computerFist!=0)s=computerFist==1?"剪刀":"布";
return s;
}
public String getResult(int yourFist)
{
if(yourFist>2||yourFist<0)return "输入有误";
String y = "";
String r = "";
switch(yourFist)
{
case 0:y="石头";
if(computerFist==0){r="和电脑出得一样";result=1;}
else if(computerFist==1){r="赢了";result=2;}
else if(computerFist==2){r="输了";result=0;}
break;
case 1:y="剪刀";
if(computerFist==1){r="和电脑出得一样";result=1;}
else if(computerFist==2){r="赢了";result=2;}
else if(computerFist==0){r="输了";result=0;}
break;
case 2:y="布";
if(computerFist==2){r="和电脑出得一样";result=1;}
else if(computerFist==0){r="赢了";result=2;}
else if(computerFist==1){r="输了";result=0;}
break;
}
return "你出的是:"+y+",电脑出的是:"+getComputerFist()+"。你"+r;
}
public int getResult() {
return result;
}
}

测试类:
package test;
import java.util.Scanner;
public class TestCaiQuan {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int win = 0;
int lose = 0;
int draw = 0;
int error = 0;
int times = 10;
int num = times;
while(num>0)
{
CaiQuan cq = new CaiQuan();
System.out.println("请输入您要出的拳(0 石头)(1 剪刀)(2 布)");
System.out.println(cq.getResult(input.nextInt()));
if(cq.getResult()==0)lose+=1;
else if(cq.getResult()==1)draw+=1;
else if(cq.getResult()==2)win+=1;
else error+=1;
num--;
}
System.out.println("您共玩了"+times+"次,赢"+win+"次,输"+lose+"次,平"+draw+"次,错误输入"+error+"次");
System.out.println("您的胜率是:"+Double.valueOf(win)/times*100+"%");
}

}