📄 rsc.java
字号:
//package rsc;
import javax.swing.*;
import java.io.*;
import java.util.*;
/**
File Name: RSC.java
Author: 黄增博 #B02251132
Date: March 20 2004
Description: This program would like to program the computer to play this game
against a human. Each will make a choice and the computer
will work out who won.
**/
public class RSC {
public static void main(String[] args)throws IOException{
String player=null;
String computerstring=null;
String winner=null;
int you=1000;
int computer=1000;
BufferedReader stdn = new BufferedReader(new InputStreamReader(System.in));
String r;
int seed;
boolean isDigit;
System.out.println("Enter the seed:");
do{
isDigit=true;
r=stdn.readLine();//input the string
for(int i=0;i<r.length();i++)//check the input is the integer
{
char checkDigit;
checkDigit=r.charAt(i);
if(!Character.isDigit(checkDigit))
isDigit=false;
}
if(isDigit==false)
System.out.println("The seed should be an integer!Input again!");
}while(isDigit==false);//if the input is not an integer,input it again
seed=Integer.parseInt(r);
System.out.println("The rules: \n rock beats scissors;\n scissors beat cloth;\n cloth beats rock; ");
System.out.println("Please input your choice!"+'\n'+"r is rock,s is scissors,c is cloth,q is exit");
System.out.println("Computer : Your turn");
Random temp=new Random(seed);//give the random number from seed
while(!(r=stdn.readLine()).equals("q")){//input the r,s,c,q ,when input q ,exit
if(r.equals("r"))
you=2;
else if(r.equals("s"))
you=1;
else if(r.equals("c"))
you=0;
//check if the input is r,s,c,q
else System.out.println("Wrong input,please enter 'r','s','c','q'"+'\n'+"Computer:Your turn");
seed=temp.nextInt();//give the next random number
seed=seed%3; //make the number to the computer
computer=Math.abs(seed);
if (computer==2)//make the computer's output
computerstring="rock";
else if (computer==1)
computerstring="scissors";
else if (computer==0)
computerstring="cloth";
//check the winner
if (((you==0)&&(computer==2))||((you==1)&&(computer==0))||((you==2)&&(computer==1)))
winner="I got you!";
else if (computer==you)
winner="Draw!";
else if (((computer==0)&&(you==2))||((computer==1)&&(you==0))||((computer==2)&&(you==1)))
winner="You lose!";
if(r.equals("s")||r.equals("r")||r.equals("c")||r.equals("q"))//check the input is s,r,c,q
//output the conclution
System.out.print("Computer: "+computerstring+','+winner+'\n'+"Computer:Your turn"+'\n'+"User: ");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -