java学习笔记3.24

类与方法:

类是模板,而对象是具体化的事物,比如狗是一个类,而小明的狗,小丽的狗则是对象,这些狗都具有的同样的是属性(成员变量),而能完成的行为是方法。具体用代码举例:

这是一个人和电脑猜拳的游戏

人出拳

package homework;//如果不导入的话需要将文件都放在一个包内

import java.util.Scanner;
public class Person {
public String name;//成员变量,声明时会自动赋值,而局部变量则不会,在方法中,若有同名变量局部变量优先级高于成员变量,若要指定成员变量加前缀this.
public int score;
Scanner input=new Scanner(System.in);
//提示人出拳并返回一个值
public int punches(){
int perPunches;
boolean isOneMore;
do{
isOneMore=false;
System.out.println("请出拳:1、剪刀,2、石头,3布");
perPunches=input.nextInt();
switch(perPunches){
case 1:
System.out.println("你出了剪刀");
break;
case 2:
System.out.println("你出了石头");
break;
case 3:
System.out.println("你出了布");
break;
default:
isOneMore=true;
System.out.println("输入错误,请重新输入");
}
}while(isOneMore);
return perPunches;
}
}

//电脑出拳

package homework;


public class Computer {
public String name;
public int score;
public int punches(){
int computerpunches=(int)((Math.random())*9)%3+1;
switch(computerpunches){
case 1:
System.out.println("电脑出了剪刀");
break;
case 2:
System.out.println("电脑出了石头");
break;
case 3:
System.out.println("电脑出了布");
}
return computerpunches;
}
}

//游戏部分

package homework;


import java.util.Scanner;


public class personPlayGameWithComputer {
Scanner input=new Scanner(System.in);
Computer robot=new Computer();
Person firstPerson=new Person();
//欢迎界面和输入玩家名字
public void welcome(){
System.out.println("***************************");
System.out.println("\t\t欢迎来玩猜拳大战");
System.out.println("***************************");
System.out.println("请输入你的名字");
firstPerson.name=input.next();
}
//选择对手
public void choosePersonage(){
boolean isMistake=false;
do{
System.out.println("请选择你要对战的对手:1、张飞,2、吕布,3、秦琼");
switch(input.nextInt()){
case 1:
robot.name="张飞";
System.out.println("你选择了张飞做你的对手");
break;
case 2:
robot.name="吕布";
System.out.println("你选择了吕布做你的对手");
break;
case 3:
robot.name="秦琼";
System.out.println("你选择了秦琼做你的对手");
break;
default:
System.out.println("输入错误,请重新选择");
}
}while(isMistake);
}
//判断谁得分
public void getResult(){
int person=firstPerson.punches();
int computer=robot.punches();
if((person==1&&computer==2)||(person==2&&computer==3)||(person==3&&computer==1)){
System.out.println(robot.name+"胜利,"+robot.name+"分数加一");
robot.score+=1;
}else if((person==1&&computer==3)||(person==3&&computer==2)||(person==2&&computer==1)){
System.out.println(firstPerson.name+"胜利,"+firstPerson.name+"分数加一");
firstPerson.score+=1;
}else
System.out.println("平局");
}
public void end(){
System.out.println(firstPerson.name+"成绩为:"+firstPerson.score);
System.out.println(robot.name+"成绩为:"+robot.score);
if(firstPerson.score>robot.score)
System.out.println("你真厉害,我"+robot.name+"不是你的对手");
else if(firstPerson.score<robot.score)
System.out.println("哈哈哈,见识到我的厉害了吧");
else
System.out.println("算你运气好,竟然能和我打成平手");
}
}


//开始游戏

package homework;


import java.util.Scanner;


public class StaryGame {


public static void main(String[] args) {
Scanner input=new Scanner(System.in);
personPlayGameWithComputer A=new personPlayGameWithComputer();
A.welcome();
A.choosePersonage();
System.out.println("**************************");
System.out.println("\t\t游戏开始");
System.out.println("**************************");
String isOver=null;
do{
A.getResult();
System.out.println("是否继续(Y/N):");
isOver=input.next();
}while(isOver.equals("Y"));
A.end();
input.close();
}
}

main方法是所有方法的入口,main方法一个类只能有一个。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值