发表于: 2017-08-01 16:31:02
2 987
今天完成:
import java.math.BigInteger;
import java.util.Scanner;
public class BigIntegerTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("How many numbers do you need to draw?");
int k = in.nextInt();
System.out.println("What is the highest number you can draw?");
int n = in.nextInt();
BigInteger lotteryOdds = BigInteger.valueOf(1);
for (int i = 1; i <= k; i++) {
lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i - 1)).divide(BigInteger.valueOf(i));
}
System.out.println("Your odds are 1 in " + lotteryOdds + ".Good luck!");
}
}
Console:(输出正常)
How many numbers do you need to draw?
60
What is the highest number you can draw?
99
Your odds are 1 in 839905957702732081833776208.Good luck!
Process finished with exit code 0
public class CompoundTnterest {
public static void main(String[] args) {
final double STARTRATE = 10 ;
final int NRATES = 6 ;
final int NYEARS = 10 ;
//set interest rates to 10...15%
double[] interestRate = new double[NRATES];
for(int j = 0 ; j < interestRate.length ; j++)
interestRate[j] = (STARTRATE+j)/100.0;
double [][] balances = new double[NYEARS][NRATES];
//set initial balances to 10000
for(int j = 0 ; j < balances[0].length ; j++)
balances[0][j] = 10000 ;
for(int i = 1 ; i < balances.length ; i++) {
for(int j = 0 ; j < balances[i].length ; j++) {
//get last year's balances from previous row
double oldBalance = balances[i-1][j];
//compute interest
double interest =oldBalance* interestRate[j];
//compute this year's balances
balances[i][j] = oldBalance+interest;
}
}
//print one row of interest rates
for(int j = 0 ; j < interestRate.length ; j++)
System.out.printf("%9.0f%%",100*interestRate[j]);
System.out.println();
//print balance table
for(double[] row: balances){
//print table row
for(double b:row)
System.out.printf("%10.2f",b);
System.out.println();
}
}
}
Console:
10% 11% 12% 13% 14% 15%
10000.00 10000.00 10000.00 10000.00 10000.00 10000.00
11000.00 11100.00 11200.00 11300.00 11400.00 11500.00
12100.00 12321.00 12544.00 12769.00 12996.00 13225.00
13310.00 13676.31 14049.28 14428.97 14815.44 15208.75
14641.00 15180.70 15735.19 16304.74 16889.60 17490.06
16105.10 16850.58 17623.42 18424.35 19254.15 20113.57
17715.61 18704.15 19738.23 20819.52 21949.73 23130.61
19487.17 20761.60 22106.81 23526.05 25022.69 26600.20
21435.89 23045.38 24759.63 26584.44 28525.86 30590.23
23579.48 25580.37 27730.79 30040.42 32519.49 35178.76
Process finished with exit code 0
Junit:
junit是一个用Java语言编写的单元测试框架。促进了先测试再编码,提高了程序员的工作效率和代码的稳定性。但它仅适合纯粹的单元测试,对于集成测试应使用TestNG。
一个单元可是method,class,package,or 子系统
package com.thrice.junit;
public class AddTest {
public int sum(int var1,int var2){
System.out.println("相加的值是:"+var1+"+"+var2);
return var1+var2;
}
}
package com.thrice.junit;
import static org.junit.Assert.*;
import org.junit.Test;
public class AddText_1 {
AddTest addtest = new AddTest();
int sum = addtest.sum(2, 5);
int testsum = 7;
@Test
public void testsum() {
System.out.println("@Test sum():" + sum + "=" + testsum);
assertEquals(sum, testsum);
}
}
明日计划:
继续学Spring,junit。
马上大四了,这个月要准备电子设计大赛,然后就是毕业设计,实习。。。事情慢慢就很多了,所以可能学Java的时间要少些了,但自己会尽量坚持下去的。
遇到的问题:
注释掉插入数据部分后,输出显示任然不正确,程序和xml文件可能都有问题。
收获:
基本的整数和浮点数精度不能够满足需求时,可以使用java.math包中的两个很有用的类:BigInteger和BigDecimal.这两个类可处理包含任意长度数字序列的数值。
BigInteger类实现了任意精度的整数运算 BigDecimal实现了任意精度的浮点数运算
BigInteger a = BigInteger.valueOf(100);//使用静态的valueOf方法可以将普通的数值转换为大数值
但不能使用熟悉的算术运算符如(+ ,—)处理大数值。需要使用大数值类中的add和multiply方法
评论