设计出物品这种类
类名:Item
物品有如下属性:
名字 name 类型是字符串String
价格 price 类型是整型 int
创建(实例化)3件具体物品
名称 价格
血瓶 50
草鞋 300
长剑 350
class Item {
String name;
int price;
public void Item(String name, int price) {
this.name = name;
this.price = price;
}
}
public class Demo {
public static void main(String args[]) {
Item blood = new Item();
blood.name = "血瓶";
blood.price = 50;
System.out.println("物品一 名称:" + blood.name + "、价格:" + blood.price);
Item sandal = new Item();
sandal.name = "草鞋";
sandal.price = 300;
System.out.println("物品二 名称:" + sandal.name + "、价格:" + sandal.price);
Item sword = new Item();
sword.name = "长剑";
sword.price = 350;
System.out.println("物品二 名称:" + sword.name + "、价格:" + sword.price);
}
}
为英雄类Hero设计几个新的方法:
1. 超神 legendary(),无参数,无返回类型
2. 获取当前的血量 getHp(), 无参数,有float类型的返回值
3. 回血 recovery(float blood), 有参数 float类型参数 blood表示本次恢复的血量,无返回类型
class Hero {
float Hp;
public void legendary() {
}
public float getHp() {
return this.Hp = Hp;
}
public void recovery(float blood) {
System.out.println("本次恢复血量:" + blood);
}
}
public class Demo {
public static void main(String args[]) {
}
}
public class Demo {
int i = 1; //属性名是i
public void method1(int i) { //参数也是i
System.out.println(i);
}
public static void main(String[] args) {
new Demo().method1(5);
//结果打印出来是 1还是5?
}
}
public class HelloWorld {
public void method1(final int j) {
j = 5; //这个能否执行?
}
}
使用Scanner从控制台获取两个数字,然后计算这两个数字的和
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a = s.nextInt();
System.out.println("第一个整数:" + a);
int b = s.nextInt();
System.out.println("第二个整数:" + b);
System.out.println("两个数的和为:" + (a+b));
}
}
使用Scanner收集你的身高体重,并计算出你的BMI值是多少
BMI的计算公式是 体重(kg) / (身高*身高)
比如邱阳波的体重是72kg, 身高是1.69,那么这位同学的BMI就是
72 / (1.69*1.69) = ?
import java.util.Scanner;
class BMI {
float height;
float weight;
public BMI(float height, float weight) {
this.height = height;
this.weight = weight;
}
public float Calculation() {
return weight / (height * height);
}
public void Obesity(float bmi) {
if (bmi < 18.5) {
System.out.println("您属于体重过轻,要多吃肉肉哦");
} else if (18.5 <= bmi && bmi < 24) {
System.out.println("您体脂率属于正常,请继续保持哦");
} else if (24 <= bmi && bmi < 27) {
System.out.println("您属于轻度肥胖,稍微运动一下就完美了哦");
} else if (30 <= bmi && bmi < 35) {
System.out.println("您属于中度肥胖,加油锻炼下告别肥胖");
} else if (35 <= bmi) {
System.out.println("您属于重度肥胖,加油动起来吧");
} else {
System.out.println("您是外星人吗?系统竟无法判断体脂率");
}
}
}
public class Demo {
public static void main(String[] args) {
System.out.println("请输出您的身高(m):");
Scanner height = new Scanner(System.in);
float heightA = height.nextFloat();
System.out.println("请输出您的体重(kg):");
Scanner weight = new Scanner(System.in);
float weightA = weight.nextFloat();
BMI bmi = new BMI(heightA, weightA);
System.out.print("您当前的BMI指数为:");
float a = bmi.Calculation();
bmi.Obesity(a);
}
}
通过Scanner输入一个1-7之间的整数,使用三元操作符判断是工作日还是周末?
import java.util.Scanner;
// 逻辑或运算符:||
//三目运算符 z = (x > y) ? x : y;
public class Demo {
public static void main(String[] args) {
System.out.println("今天是星期几?");
Scanner day = new Scanner(System.in);
int days = day.nextInt();
if(days>7 || days<1){
throw new IllegalArgumentException("数字非法");
}
String z = (days>5) ? "今天是周末" : "今天是工作日";
System.out.println(z);
}
}
判断某一年是否为闰年
通过Scanner 输入一个年份,然后判断该年是否是闰年
闰年判断标准(满足任何一个)
1. 如果能够被4整除,但是不能被100整除
2. 能够被400整除
import java.util.Scanner;
//Scanner prin = new Scanner(System.in);
public class Demo {
public static void main(String[] args) {
while (true) {
System.out.println("请输入一个年份");
Scanner prints = new Scanner(System.in);
int year = prints.nextInt();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println("是闰年");
break;
} else {
System.out.println("不是闰年");
}
}
}
}
遗忘的知识点:%是求模运算符;/是除运算符;二者都是双目运算符。它们之间的区别就是:%是求余运算,即2%10=2,10%2=0,10%3=1。/是普通的除号,即10/2=5
通过Scanner 输入月份,然后使用switch 判断季节
import java.util.Scanner;
//Scanner prints = new Scanner(System.in);
public class Demo {
public static void main(String[] args) {
System.out.println("请输入一个年份");
Scanner prints = new Scanner(System.in);
int season = prints.nextInt();
switch(season){
case 1: case 2: case 3:
System.out.println("春天");
break;
case 4: case 5: case 6:
System.out.println("夏天");
break;
case 7: case 8: case 9:
System.out.println("秋天");
break;
case 10: case 11: case 12:
System.out.println("冬天");
break;
default:
System.out.println("非法");
}
}
}
天朝有一个乞丐姓洪,去天桥要钱
第一天要了1块钱
第二天要了2块钱
第三天要了4块钱
第四天要了8块钱
以此类推
问题: 洪乞丐干10天,收入是多少?
public class Demo {
public static void main(String[] args) {
int money = 0;
int all = 0;
for (int i = 1; i <= 10; i++) {
if (i >= 2) {
money = money * 2;
} else
money++;
all=money+money;
System.out.println("第"+i+"天"+money+"元");
}
System.out.println("总:"+all);
}
}
打印 1-100 之间的数,如果这个数,要么是3,要么5的倍数,就忽略掉
public class Demo {
public static void main(String[] args) {
for (int i =1; i <= 100; i++){
if(i%3==0 || i%5==0){
}else{
System.out.println(i);
}
}
}
}
设计一个类Support (辅助英雄)继承Hero,提供一个heal(治疗)方法
对Support的heal方法进行重载
heal()
heal(Hero h) //为指定的英雄加血
heal(Hero h, int hp) //为指定的英雄加了hp的血
class Hero {
String name;
public Hero(String name){
this.name = name;
}
}
class Support extends Hero {
public Support(String name) {
super(name);
}
public void heal() {
}
public void heal(String name) {
}
public void heal(String name, int hp) {
System.out.println("为" + name + "加了" + hp + "血");
}
public void heal(Hero name) {
}
}
public class Demo {
public static void main(String[] args) {
Support per = new Support("啊啊");
per.heal("啊啊",50) ;
per.heal() ;
}
}