示例:人机猜拳(请各位大佬看下)
请各位大佬帮我看下,怎样缩短语句。
在这谢谢啦!
这是管理类:
import java.util.Scanner;
public class startGame {
Scanner input=new Scanner(System.in);
int count=0;
int computerCount=0;
int personCount=0;
String computer;
public void startbefore(){
System.out.println("\t\t******************");
System.out.println("\t\t** 猜拳,开始 **");
System.out.println("\t\t******************\n\n");
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
System.out.println("请选择对方角色 (刘备1 孙权2 曹操3):");
computer=input.next();
System.out.println("您选择了 "+computer+"对战");
do{
System.out.println("要开始吗? (y/n)");
String answer=input.next();
String nide="null";
System.out.println("请选择出拳:1.剪刀 2.石头 3.布 (输入相应数字):");
int num=input.nextInt();
switch(num){
case 1:
nide="剪刀";
break;
case 2:
nide="石头";
break;
case 3:
nide="布";
break;
}
System.out.println("你的出拳:"+nide);
String chuquan="null";
int num2=(int)(Math.random()*3);
while(num2<=0){
num2=(int)(Math.random()*3);
}
switch(num2){
case 1:
chuquan="剪刀";
break;
case 2:
chuquan="石头";
break;
case 3:
chuquan="布";
break;
}
System.out.println("刘备出拳:"+chuquan);
if(nide.compareToIgnoreCase(chuquan)>0){
System.out.println("您赢了!真棒!");
personCount++;
}else if(nide.compareToIgnoreCase(chuquan)<0){
System.out.println("您输了,下次加油!");
computerCount++;
}else{
System.out.println("平手,不错哟!");
}
count++;
}
System.out.println("是否开始下一轮 (y/n):");
String answer1=input.next();
while("y".equals(answer1)){
String ni="null";
System.out.println("请选择出拳:1.剪刀 2.石头 3.布 (输入相应数字):");
int num1=input.nextInt();
switch(num1){
case 1:
ni="剪刀";
break;
case 2:
ni="石头";
break;
case 3:
ni="布";
break;
}
System.out.println("你的出拳:"+ni);
String chuquan="null";
int num2=(int)(Math.random()*3);
while(num2<=0){
num2=(int)(Math.random()*3);
}
switch(num2){
case 1:
chuquan="剪刀";
break;
case 2:
chuquan="石头";
break;
case 3:
chuquan="布";
break;
}
System.out.println("刘备出拳:"+chuquan);
if(ni.compareToIgnoreCase(chuquan)>0){
System.out.println("您赢了!真棒!");
personCount++;
}else if(ni.compareToIgnoreCase(chuquan)<0){
System.out.println("您输了,下次加油!");
computerCount++;
}else{
System.out.println("平手,不错哟!");
}
System.out.println("是否开始下一轮 (y/n):");
answer1=input.next();
count++;
}
System.out.println("要开始下一轮吗? (y/n)");
String answer=input.next();
}
while("y".equals(answer1);
public void showResult(){
String person="王潇璐";
System.out.println("-----------------------------------------");
System.out.println(computer+"vs"+person);
System.out.println("对战次数:"+count);
if(computerCount>personCount){
System.out.println("结果:您输了,下次加油哟!");
}else if(computerCount==personCount){
System.out.println("结果:平手,下次干掉它!");
}else{
System.out.println("您赢了,真棒!不要骄傲哟!");
}
}
}
这是调试类:
public class Test {
public static void main(String[] args) {
startGame center=new startGame();
center.startbefore();
center.showResult();
}
}