刀石头布游戏 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
相关文档:
类的初始化和对象初始化是 JVM 管理的类型生命周期中非常重要的两个环节,Google 了一遍网络,有关类装载机制的文章倒是不少,然而类初始化和对象初始化的文章并不多,特别是从字节码和 JVM 层次来分析的文章更是鲜有所见。
本文主要对类和对象初始化全过程进行分析,通过一个实际问题引入,将源代码转换成 JVM 字节码后, ......
public boolean writeXML(String content, String filename)
{
String savepath;
FileOutputStream fout;
// log.info("content:"+content+ ......
今天和大家一起学习Java的设计模式。本人的水平不是很高,这系列文章只是自己学习的过程,并希望能同大家分享经验。
先说下我对工厂模式的理解:当我们需要某个对象时,最直接的办法是看到这个对象就拿过来。但是当对象非常多的时候,找起来就很不方便。这时就需要一个中介来帮助我们取得想要的东西,这个中介就是工厂(fa ......
java文件过滤器的使用代码如下:
测试代码:package file;
import java.io.File;
public class fileFilter {
public static void main(String[] args) {
File file = new File("d:\\");//设置文件路径
for (File fileList : file.listFiles(new file.MyFileFilter())) {
......
今天观看了蓝山老师java背后的秘密相关视频,发现要想写出高性能的程序,一定要对其运行原理以及其运行环境有相当程度的了解。那ClassLoader是相当关键的一个部分。
先说下java程序运行的基本流程,先将java文件编译为class文件,然后通过ClassLoader(类加载器),加载到Runtime Data Area(类似于内存)中 ......