package practice;
import java.util.Scanner;
public class TestPeople {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
People p = new People();
// 自我介绍
p.ShowPersonalInfo();
// 返回最大值
System.out.println("\n");
int number[] = new int[3];
for (int i = 0; i < number.length; i++) {
System.out.print("请输入第" + (i + 1) + "个数:");
number[i] = sc.nextInt();
}
System.out.println(number.length + "个数中的最大值为:" + p.FindTheMaximum(number));
// 目标数累加
System.out.print("\n请输入一个目标数:");
if (sc.hasNextInt()) {
int target = sc.nextInt();
System.out.println("从1累加至" + target + "的和为:" + p.TargetNumberAccumulation(target));
} else {
System.out.println("提示:请输入数字!");
}
// 烹饪方法
System.out.print("\n请输入第一道食材:");
String foodMaterial01 = sc.next();
System.out.print("请输入第二道食材:");
String foodMaterial02 = sc.next();
System.out.println(p.Cooking(foodMaterial01, foodMaterial02));
// 重写toString()
System.out.println("\n" + p.toString());
sc.close();
}
}
class People {
private String name = "李荣浩";
private int age = 34;
private char gender = '男';
// 自我介绍
public void ShowPersonalInfo() {
System.out.println("自我介绍");
System.out.print("姓名:" + name);
System.out.print(" 年龄:" + age);
System.out.print(" 性别:" + gender);
}
// 返回最大值
public int FindTheMaximum(int number[]) {
int max = 0;
for (int i = 0; i < number.length; i++) {
if (max < number[i]) {
max = number[i];
}
}
return max;
}
// 目标数累加
public int TargetNumberAccumulation(int target) {
int sum = 0;
for (int i = 1; i <= target; i++) {
sum += i;
}
return sum;
}
// 烹饪方法
public String Cooking(String dishes01, String dishes02) {
return "提示:" + dishes01 + "和" + dishes02 + "能做出一份" + dishes01 + "炒" + dishes02 + "!";
}
// 重写toString()
@Override
public String toString() {
return "People [name=" + name + ", age=" + age + ", gender=" + gender + "]";
}
}
JavaOOP定义一个人类编写并测试其对应的方法
最新推荐文章于 2022-09-03 09:39:28 发布