java核心技术 第三章代码栗子


栗子 lotteryOdds: 这个在注释里面已经写的很明白了就是计算

代码:

import java.util.Scanner;


public class LotteryOdds {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//计算n*(n-1)*(n-2)*....(n-k+1)/k*(k-1)*...1
		
		Scanner in = new Scanner(System.in);
		
	    System.out.println("How many numbers do ypu want to draw?");
	    int n = in.nextInt();
	    
	    System.out.println("What is the Higher number you can draw?");
	    int k = in.nextInt();
	    
	    int lotterodds = 1;
	    for(int i = 1; i <= k ; i++)
	    {
	    	lotterodds = lotterodds*(n-i+1)/i;
	    }
	    System.out.println("your odds are 1 in "+ lotterodds +".Good Luck!");
	    
	}

}

下面一个栗子是也是计算退休金的,只是给出年率,每年存多少钱,然后最后获得多少钱。。。

代码:

import java.util.Scanner;


public class RetireTest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
       //首先确定输入
		Scanner in = new Scanner(System.in);
		
		//呢一共要存多少钱养老
		System.out.println("How much money do you want to contribute every year?");
		double payment = in.nextDouble();
		
		//年率是多少
		System.out.println("Interest rate in %");
		double intrestRate = in.nextDouble();
		
	     double balance = 0;
	     int year = 0;
	     String input; //定义一个字符串
	     //下面主要是用do while 循环来解决问题 的
	     do
	     {
	    	 balance += payment ;
	    	 double interest = intrestRate*balance/100;
	    	 balance += interest;
	    	 
	    	 year++;
	    	 
	    	 //输出N年后的一共多少钱printf 和println 的区别
	    	 System.out.printf("After year %d, your balance is %.2f", year, balance);
	    	 
	    	 System.out.println("Ready to Retire(Y/N)?");
	    	 input = in.next();//输入一个字符 	 
	    	 
	     }while(input.equals("N"));
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangxiaoming

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值