刀石头布游戏 java版
import java.util.Scanner;
public class Game{
void welcome(){
println("欢迎来到剪刀石头布游戏");
}
Choice getUserChoice(){
println("请选择\t[1]剪刀\t[2]石头\t[3]布");
Scanner sc= new Scanner(System.in);
int userChoice=Integer.parseInt(sc.nextLine());
while(userChoice>3||userChoice<1){
println("您输入的不合法,请重新输入");
userChoice=Integer.parseInt(sc.nextLine());
}
return new Choice(userChoice);
}
Choice getComputerChoice(){
return new Choice((int)(3*Math.random())+1);
}
void showResult(Choice userChoice,Choice computerChoice){
int re=userChoice.compareTo(computerChoice);
if(re==0){
println("平了!您好,您输入的是:"+userChoice.getText());
}else if(re==1){
println("恭喜您获胜了!您输入的是:"+userChoice.getText()+"\t电脑输入的是:"+computerChoice.getText());
}
else {
println("抱歉您输了!您输入的是:"+userChoice.getText()+"\t电脑输入的是:"+computerChoice.getText());
}
}
void println(String s){
System.out.println(s);
}
void start(){
welcome();
Choice userChoice=getUserChoice();
Choice computerChoice=getComputerChoice();
showResult(userChoice,computerChoice);
}
public static void main(String[] args){
new Game().start();
}
}
class Choice{
String s[]={"剪刀","石头","布"};
int value;
Choice(int a){
value=a;
}
String getText(){
return s[value-1];
}
// 0为 平 1为 赢 -1为 输
int compareTo(Choice c){
if(value==c.value){
&nb
相关文档:
用Java编写应用时,有时需要在程序中调用另一个现成的可执行程序或系统命令,这时可以通过组合使用Java提供的Runtime类和Process类的方法实现。下面是一种比较典型的程序模式:
...
Process process = Runtime.getRuntime().exec(".\\p.exe");
process.waitfor( );
...
在上面的程序中,第一行的“.\\p.exe& ......
/**
* 创建缩略图片
*
* @param orgpath
* @param filename
* @return
* @description: 描述
*/
//此方法对于ssh项目并且针对 上传功能时,非常有用
public static Boolean createAbbreviateImg(String orgpath, String filename) {
Boolea ......
public boolean writeXML(String content, String filename)
{
String savepath;
FileOutputStream fout;
// log.info("content:"+content+ ......
transient是Java语言的关键字,用来表示一个域不是该对象串行化的一部分。当一个对象被串行化的时候,transient型变量的值不包括在串行化的表示中,然而非transient型的变量是被包括进去的!
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
......
今天和大家一起学习Java的设计模式。本人的水平不是很高,这系列文章只是自己学习的过程,并希望能同大家分享经验。
先说下我对工厂模式的理解:当我们需要某个对象时,最直接的办法是看到这个对象就拿过来。但是当对象非常多的时候,找起来就很不方便。这时就需要一个中介来帮助我们取得想要的东西,这个中介就是工厂(fa ......