Java初学Text3

1:使用封装的思想实现对User, username,password,age,sex,和age和性别做限制

年龄在0-130是合理的,超过这个范围给出默认值20

String , 比较  equals()

package javaHome3;

public class User {
	private String username;
	private String password;
	private String sex;
	private int age=20;
	public User(){}
	public User(String username,String password,String sex,int age){}
	/**
	 * @return the age
	 */
	public int getAge(int age) {
		if(age>0&&age<130){
			this.age=age;
		}
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
	/**
	 * @return the password
	 */
	public String getPassword(String password) {
		this.password = password;
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * @return the sex
	 */
	public String getSex(String sex) {
		if(sex.equals("男")||sex.equals("女")){
			this.sex=sex;
		}
		return sex;
	}
	/**
	 * @param sex the sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}
	/**
	 * @return the username
	 */
	public String getUsername(String username) {
		this.username=username;
		return username;
	}
	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	public void show(){
		System.out.println("用户名是:"+username+";密码是:"+password
				+";年龄是:"+age+";性别是:"+sex+"。");
	}
}



package javaHome3;

import java.util.Scanner;

public class DemoUser {
	public static void main(String[] args) {
		System.out.println("请输入用户名:");
		Scanner n=new Scanner(System.in);
		String username=n.next();
		System.out.println("请输入密码:");
		String password=n.next();
		System.out.println("请输入性别:");
		String sex=n.next();
		System.out.println("请输入年龄(0-130是合理,超出默认20):");
		int age=n.nextInt();
		User us=new User(username,password,sex,age);
		us.getUsername(username);
		us.getPassword(password);
		us.getSex(sex);
		us.getAge(age);
		us.show();
	}

}

运行结果:

 

package javaWeek3;

import java.util.Scanner;

public class Voter {
	static int count=90;
	static int[] voter=new int[100];
	static void vote(){
		for (int i = 0; i < 999; i++) {
			voter[i]=i+91;
			Scanner sc=new Scanner(System.in);
			int x=sc.nextInt();
			if(count<100){
				if(x==1){				
				count++;
				System.out.println(voter[i]+"号"+"投了一票!"+"当前票数是"+count+"。");
				}else{
					System.out.println(voter[i]+"号"+"放弃投票"+"当前票数是"+count+"。");
				}
				if(count==100){
					System.out.println("票数满100,停止投票!");
					break;
				}
				
			}
		}
		
	}
	public static void main(String[] args) {
//		Scanner sc=new Scanner(System.in);
//		int x=sc.nextInt();
		System.out.println("输入1投票,输入其他放弃投票:");
		Voter.vote();
	}
}

 运行结果:

 

package javaWeek3;

import java.util.Scanner;

public class Penguin extends Pet {
	private int lovely=60;
	public Penguin(){}
	public Penguin(String name,String type,int lovely,int health,String area,String sex){}
	
	/**
	 * @return the lovely
	 */
	public int getLovely() {
		
		return lovely;
	}
	/**
	 * @param lovely the lovely to set
	 */
	public void setLovely(int lovely) {
		this.lovely = lovely;
		if(this.lovely<0||this.lovely>100){
			System.out.println("健康值应在1~100之间");
		}else{this.lovely=60;}
	};
	public void showp(){
		Scanner n=new Scanner(System.in);
		System.out.println("请输入亲密度:");
		this.lovely=n.nextInt();

		
	}
	public void showp2(){
		System.out.println("我的亲密度是:"+lovely);
	}



}

package javaWeek3;

import java.util.Scanner;

public class DemoPen {
	public static void main(String[] args) {
		for (int i = 0; i<5; i++) {
		System.out.println("欢迎来到宠物店:");
		Scanner n=new Scanner(System.in);
		System.out.println("请输入要领养宠物的名字:");
		String name=n.next();//输入名字
		System.out.println("请输入要领养宠物的类型(狗狗,企鹅):");
		String type=n.next();
		System.out.println("请输入性别(GG,MM):");
		String sex=n.next();
		System.out.println("请输入健康值(1~100之间,默认值为60)");
		int health=n.nextInt();		
		Dog d=new Dog();
		Penguin p=new Penguin();
			d.setName(name);
			d.setType(type);
			d.setSex(sex);
			d.setHealth(health);
			if(type.equals("狗狗")){
				d.showd();
				System.out.println("下面是宠物的自白:");	
				d.showd2();
				d.show0();
			}else if(type.equals("企鹅")){
					p.setName(name);
					p.setType(type);
					p.setSex(sex);
					p.setHealth(health);
					p.showp();
					System.out.println("下面是宠物的自白:");	
					p.showp2();
					p.show0();
				}else if(name.equals("结束")){
					System.out.println("谢谢惠顾!");
					break;
				}
			
		}
		
		
		
	}

}

 

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值