JavaOOP定义一个人类编写并测试其对应的方法

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 + "]";
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值