大四学校上课笔记 day1
程序员文章站
2024-02-28 12:11:34
...
-----------对象与类的区别
-----------静态特征(属性),动态特征(方法)
-----------变量与常量
---变量定义:内存中一块存储空间的表示
----变量三要素:变量类型,变量名,变量值
-----------计算机存储方式
----硬盘:永久存储
----内存:瞬时存储
开辟空间--------变量
数据的类型-----变量类型
给空间起名----变量名
存数据----------变量值
------------访问修饰符 public private protected 默认(不写)
------------math.random() 0~1之间的数左闭优开
math.random()*3 0~3之间的数(不包括3)
-----------快捷键
alt+/ 自动生成(syso输出)
做出的小游戏
computerPlayer.java
package day1;
public class computerPlayer {
int hp=3000;
String name;
public int computerAttack() {
System.out.println("请输入技能");
int choice=(int)(Math.random()*4+1);
int result=0;
switch(choice)
{
case 1:
result=(int)(Math.random()*100+200);
break;
case 2:
result=(int)(Math.random()*200+300);
break;
case 3:
result=(int)(Math.random()*100+500);
break;
case 4:
result=(int)(Math.random()*200+600);
break;
default :
break;
}
return result;
}
}
Demo.java
package day1;
public class Demo {
public static void main(String[] args) {
personPlayer p = new personPlayer();
computerPlayer q = new computerPlayer();
Game g = new Game();
g.init();
}
}
personPlayer.java
package day1;
import java.util.Scanner;
import javax.print.DocFlavor.INPUT_STREAM;
public class personPlayer {
Scanner input = new Scanner(System.in);
// 血量
int hp = 3000;
// 昵称
String name;
// 攻击方法
public int personAttack() {
System.out.println("请输入技能");
String choice = input.next();
int result = 0;
switch (choice) {
case "Q":
case "q":
result = (int) (Math.random() * 100 + 200);
break;
case "W":
case "w":
result = (int) (Math.random() * 200 + 300);
break;
case "E":
case "e":
result = (int) (Math.random() * 100 + 500);
break;
case "R":
case "r":
result = (int) (Math.random() * 200 + 600);
break;
default:
break;
}
return result;
}
}
Game.java
package day1;
import java.util.Scanner;
public class Game {
personPlayer p = new personPlayer();
computerPlayer c = new computerPlayer();
Scanner input = new Scanner(System.in);
// 初始化界面
public void init() {
System.out.println("LowB版人机对战");
System.out.println("\tAre you ready?");
System.out.println("如有雷同,纯属巧合");
System.out.println("是否进入游戏 True or False");
boolean istart = input.nextBoolean();
if (istart) {
System.out.println("请选择英雄 1、亚索 2、提莫 3、蛤蟆 4、盲僧 5、大头");
String[] array = { "亚索 ", "提莫 ", "蛤蟆 ", "盲僧 ", "大头" };
int index = input.nextInt();
p.name = array[index - 1];
System.out.println(p.name);
String[] arr = { "女警 ", "奥巴马 ", "牛头 ", "盖伦 ", "寒冰" };
int index2 = (int) (Math.random() * arr.length);
c.name = arr[index2];
System.out.println(p.name + "-----VS-----" + c.name);
System.out.println("\t开始对战!");
while(true){
attack();
}
} else {
System.out.println("有眼无珠,后悔一生!");
}
}
public void attack() {
int personAttack = p.personAttack();
int computerAttack = c.computerAttack();
check(personAttack,computerAttack);
}
public void check(int personResult,int computerResult){
if(p.hp-computerResult<0){
System.out.println("Defeat!");
System.exit(0);
}else if(c.hp-personResult<0){
System.out.println("victory!");
System.exit(0);
}else{
p.hp=p.hp-computerResult;
c.hp=c.hp-personResult;
if(computerResult<300){
System.out.println("对方给了你一个Q,干了你"+computerResult+"点血量!剩余"+p.hp);
}else if(computerResult<500){
System.out.println("对方给了你一个W,干了你"+computerResult+"点血量!剩余"+p.hp);
}else if(computerResult<600){
System.out.println("对方给了你一个E,干了你"+computerResult+"点血量!剩余"+p.hp);
}else if(computerResult<800){
System.out.println("对方想干死你,给了你一个R,干了你"+computerResult+"点血量!剩余"+p.hp);
}
if(personResult<300){
System.out.println("你给对方一个Q,干了对方"+personResult+"点血量!剩余"+c.hp);
}else if(personResult<500){
System.out.println("你给了对方一个W,干了对方"+personResult+"点血量!剩余"+c.hp);
}else if(personResult<600){
System.out.println("你给了对方一个E,干了对方"+personResult+"点血量!剩余"+c.hp);
}else if(personResult<800){
System.out.println("你给了对方一个R,干了对方"+personResult+"点血量!剩余"+c.hp);
}
}
}
}
推荐阅读