java学习代码02 范围:方法、方法递归、面向对象、static和this关键字、多态

1.取出大于输入值的最小质数,以7为例,两种方法。

public class MethorWork{
	public static void main(String[] args){
		panDuanZuiXiaoZhiShu(7);
	}

    public static long factional(long x){
		long a = x;
		long sum = 1;
		for (long i = 1;i<=a ;i++ ){
			sum = sum * i;
		}
		return sum;
	}

	public static void zhiShu(int x){
		int a = x;
		for (int i =a+1 ;i<35000 ;i++ ){
			for (int k =2;k<i ;k++ ){
				if (i%k==0){
					break;
				}else if (k==i-1){
					System.out.println(i);
				}
			}
		}
	}
	public static void panDuanZuiXiaoZhiShu(int b){
		while (!isZhiShu(++b)){
		}
		System.out.println(b);		
	}

	public static boolean isZhiShu(int a){
		for (int i = 2;i<a ;i++ ){
			if (a%i==0){
				return false;
			}
		}
		return true;
	}

}

2.计算1-n的和,用循环

/*使用循环计算1-n的和*/
import java.util.Scanner;
public class HomeWork01{
	public static void main(String[] args){
	System.out.println("你好,这是计算1-n和的计算器,请输入n的值:");
	Scanner s = new Scanner(System.in);
	int n = s.nextInt();
	int sum = 0;
	for (int i = 1;i<=n ;i++ ){
		sum +=i;
	}
	System.out.println("结果是"+sum);
	}
}

3.计算1-n的和,用递归

/*使用递归方式计算1-n的和*/
public class HomeWork02{
	public static void main(String[] args){
		System.out.println(sum(3));
	}
	public static int sum(int n){
		if (n==1){
			return 1;
		}
		return n+sum(n-1);
	}
}
  1. 使用递归计算n的阶乘
//使用递归方式计算n的阶乘
public class HomeWork03{
	public static void main(String[] args){
		System.out.println(sum(3));
	}
	public static int sum(int n){
		if (n==1){
			return 1;
		}
		return n*sum(n-1);
	}
}

5.编写程序,模拟用户的登录功能。程序开始运行时现在DOS命令窗口中初始介面,题型用户输入用户名和密码。用户名为admin,密码为123时登陆成功打印欢迎信息,当用户输入的用户名和密码不正确打印错误提示信息并退出系统。

import java.util.Scanner;
public class HomeWork04{
	public static void main(String[] args){
		boolean x= biJiaoYong(chuShiHua());
		boolean y= biJiaoMi(miMa());
		if (x&&y){
			System.out.println("欢迎使用Windows系统!");
		}else{
			System.out.println("用户名或密码错误");
		}
	}

	public static String chuShiHua(){
		System.out.println("初始化");
		System.out.println("请输入用户名:");
		Scanner s =new Scanner(System.in);
		String a = s.next();
		return a;
	}

	public static String miMa(){
		System.out.println("请输入密码");
		Scanner s = new Scanner(System.in);
		String b = s.next();
		return b;
	}

	public static boolean biJiaoYong(String a){
		if (a.equals("admin")){
			return true;
		}
		return false;
	}
	
	public static boolean biJiaoMi(String a){
		if (a.equals("123")){
			return true;
		}
		return false;
	}
}

6.使用方法重载比较大小

public class HomeWork05{
	public static void main(String[] args){
		System.out.println(biJiao(3,4));
		System.out.println(biJiao(3,4,5));		
	}

	public static int biJiao(int a,int b){
		if (a==b){
			return 0;
		}else if (a>b){
				return a;
		}
		return b;
	}

	public static int biJiao(int a,int b,int c){
		if (a==b&&b==c){
			return 0;
		}else if (a>b&&a>c){
			return a;
		}else if (b>c&&b>a){
			return b;
		}
		return c;
	}
}

7.定义丈夫类,定义妻子类,要求能写出丈夫的妻子,妻子的丈夫

public class HomeWork{
	public static void main(String[] args){
		Man x = new Man("210411199805013521","张三","1999-10-11",null);
		Woman y = new Woman("210411199805013500","李四","1999-10-12",null);
		x.woman = y;
		y.man = x;
		System.out.println(x.idCard);
		System.out.println(x.name);
		System.out.println(x.birth);
		System.out.println(x.woman.name);
		System.out.println(y.idCard);
		System.out.println(y.name);
		System.out.println(y.birth);
		System.out.println(y.man.name);
	}
}
class Man{
	String idCard;
	String name;
	String birth;
	Woman woman;
	public Man(){

	}
	public Man(String a,String b,String c,Woman d){
		idCard = a;
		name = b;
		birth = c;
		woman = d;
	}
}

class Woman{
	String idCard;
	String name;
	String birth;
	Man man;
	public Woman(){

	}
	public Woman(String a,String b,String c,Man d){
		idCard = a;
		name = b;
		birth = c;
		man = d;
	}
}	

8.定义Book类,两个属性,一个名称一个页数,页数不能少于200,少于200的赋值直接赋默认值200,要求能够输出“名称的页数为…”

public class HomeWork02{
	public static void main(String[] args){
		Book b1 = new Book("高三数学人教版",250);
		b1.detail();
		b1.setPageNum(150);
		b1.detail();
		b1.setTitle("高三物理人教版");
		b1.detail();
	}
}

class Book{
	private String title;
	private int pageNum;
	//构造方法 有参数 无参数
	public Book(){

	}
	public Book(String a,int i){
		title = a;
		if (i<=200){
			pageNum = 200;
			System.out.println("页数不能少于200,已经赋予默认值200");
			return;
		}
		pageNum = i;
	}
	//set和get方法
	public void setTitle(String a){
		title = a;
	}
	public String getTiltle(){
		return title;
	}
	public void setPageNum(int b){
		if (b<=200){
			pageNum = 200;
			System.out.println("页数不能少于200,已经赋予默认值200");
			return;
		}
		pageNum = b;
	}
	//打印输出方法
	public void detail(){
		System.out.println("本书的名字是" + title + "本书的页数是" + pageNum);
	}
}

9.存取款

public class HomeWork03{
	public static void main(String[] args){
		Account ja = new Account("1000",2000,0.0123);
		Customer j = new Customer("Jame Smith",ja);
		j.getAccount().withdraw(100);
		j.getAccount().deposit(960);
		j.getAccount().deposit(2000);
	}
}

class Customer{
	private String name;
	private Account act;
	//无参数有参数的构造方法
	public Customer(){

	}
	public Customer(String name,Account act){
		this.name = name;
		this.act= act;
	}
	//set方法
	public void setName(String name){
		this.name = name;
	}
	public void setAct(Account act){
		this.act = act;
	}
    //get方法
	public String getName(){
		return name;
	}
	public Account getAccount(){
		return act;
	}
}

class Account{
	private String id;
	private int balance;
	private double annualInterestRate;
	//无参数的构造方法
	public Account(){

	}
	//有参数的构造方法
	public Account(String id,int balance,double annualInterestRate){
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterestRate;
	}
	//set方法
	public void setId(String id){
		this.id = id;
	}
	public void setBalance(int balance){
		this.balance = balance;
	}
	public void setAualInterestRate(double annualInterestRate){
		this.annualInterestRate = annualInterestRate;
	}
	//get方法
	public String getId(){
		return id;
	}
	public int getBalance(){
		return balance;
	}
	public double getAnnualInterestRate(){
		return annualInterestRate;
	}
	//存款方法
	public void withdraw(int withdraw){
		if (withdraw<=0){
			System.out.println("不能存入少于1元的数额");
			return;
		}
		balance = balance + withdraw;
		System.out.println("成功存入" + withdraw + ",账户余额" + balance);
	}
	//取款方法
	public void deposit(int deposit){
		if (deposit>balance){
			System.out.println("余额不足,无法取款!");
			return;
		}
		balance = balance - deposit;
		System.out.println("成功取出" + deposit + ",账户余额" + balance);
	}
}

10.多态的使用,拓展性强

public class HomeWork04{
	public static void main(String[] args){
		Master zhangSan = new Master();
		Pet zangAo = new Dog();
		Pet meiDuan = new Cat();
		Pet jinGang = new Yingwu();
		zhangSan.feed(zangAo);
		zhangSan.feed(meiDuan);
		zhangSan.feed(jinGang);
	}
}

class Master{
	public void feed(Pet a){
		a.eat();
	}
}

class Pet{
	public void eat(){
	}
}

class Dog extends Pet{
	public void eat(){
		System.out.println("狗在吃");
	}
}

class Cat extends Pet{
	public void eat(){
		System.out.println("猫在吃");
	}
}

class Yingwu extends Pet{
	public void eat(){
		System.out.println("鹦鹉在吃");
	}
}

11.从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入 为0时结束程序

import java.util.Scanner;
public class Whileexample{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		int zhengShu = 0;
		int fuShu = 0;
		for (; ; ){
			while (true){
			System.out.println("输入一个整数");
			int z = scanner.nextInt();
			if (z > 0){
				zhengShu++;
			}else if (z < 0){
				fuShu++;
			}else
				break;
			}
		System.out.println("正整数的个数为" + zhengShu);
		System.out.println("负整数的个数为" + fuShu);
		return;
	    }
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值