Javacode:
package test01;
import java.util.Scanner;
public class Retirement2 {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.print("How muchmoney will you contribute every year?");
double payment=in.nextDouble();
System.out.print("Interestrate in%:");
double interestRate=in.nextDouble();
double balance=0;
int year=0;
String input;
do{
balance+=payment;
double interest=balance*interestRate/100;
balance+=interest;
year++;
System.out.printf("After year%d,your balance is%,2f%n,year,balance");
System.out.print("Ready toretire?(Y/N)");
input=in.next();
}
while (input.equals("N"));
}
}
还是昨天的体会,书不是全部都对的。Scanner没有导的包。
Number02:
Package test01;
Import java.util.scanner
Public class Retirment2{
Public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.print(“How much money will youcontribute every year?”);
Double payment=in.nextDouble();
System.out.print(“Interest rate in%”);
Double interestRate=in.nextDouble();
Double balance=0.0;
Int year=0;
String input;
Do{
Balance+=payment;
Doubleinterest=balance*interestRate/100;
Balance+=interest;
Year++;
System.out.printf(“After year %d,your balanceis 5,.2f%n”,year,balance);
System.out.print(“Ready to retire?(Y/N)”);
Input=in.next();
}
While(input.equals(“N”));
}
}
Number03:
Package test01;
Import java.util.scanner;
Public class Retirement2{
Public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.print(“How much money will you contribute every year?”);
Doublepayment=in.nextDouble();
System.out.print(“Interest rate in%:”);
DoubleinterestRate=int.nextDouble();
Doublebalance=0;
Intyear=0;
String input;
Do{
Balance+=payment;
Doubleinterest=balance*interestRate/100;
Balance+=interest;
Year++;
System.out.printf(“After year %d,your balance is %,.2f$n”,year,balance);
System.out.print(“Ready to retire?(Y/N)”;
Input=in.next()
}
While (input.equals(“N”))
}
}
Number03:
Package test01;
Import java.util.scanner;
Public class Retireent2{
Public static void main (String[] args){
Scnner in=new Scnner(System.in);
System.out.print(“How much money will youcontribute every year?”);
Doublepayment=in.nextDouble();
System.out.print(“Interest rate in %:”);
DoubleintersetRate=in.nextDouble();
Doublebalance=0;
Intyear=0;
String input;
Do{
Balance+=payment;
Doubleinterest=balance*intersetRate/100;
Balance+=interest;
Year++;
System.out.printf(“Afteryear %d,your balance is %,.2f”,year,balance);
System.out.print(“Ready toretire?(Y/N)”);
Input=in.next();
}
While (input.equals(“N”));
}
}
Java code:
package test01;
public class dowhile {
public static void main(String[] args){
for (int i=10;i>0;i--){
System.out.println("Countingdown..."+i);
}
}
}
执行结果:
Number 02:
Package test01;
Public class dowhile {
Public static void main (String[] args){
For(int i=10;i>0;i--){
System.out.print(“Counting down”+i);
}
}
}
大数值:
在java中如果基本的整数和浮点数精度不能满足需求,就可以使用java.math包中的两个类bigInteger和bigdecimal。这两个类可以处理任意长度的数值。Biginteger类实现了任意精度整数的运算,bigDecimal实现的任意精度的浮点数的运算。用valusOf方法可以将普通数值转化为大数值,使用方法为:
Biginteger a=BigInteger.valusOf(100);
要注意的事,在bigInteger和BigDecimal两个类中,不能用我们熟悉的运算符来进行加减乘除等运算。它的运算分别用addsubtract multiply divide pow 代表加减乘除求幂运算。因为java中没有支持运算符重载的功能。
Int k=int.nextInt()用于读取一个整数k
大数值运算
返回一个大整数和另一个大整数的和,差,积,商及余数的返回方法:
BigOmteger add(BigInteger other)
BigInteger subtract(BigInteger other)
BigInteger multiply(BigInteger other0
BigInteger divide(BigInteger other0
BigInteger mod(BigInteger other0
判断两个在整数是不是相等,并将结果返回一个值,相等返回0,小于返回负数,大于返回正数
Int compareTo(BigInteger other)
返回值等于X的大整数:static BigIntger valusOf(longx)
返回一个大数值和另一个大数值的浮点和,差,积,商及余数的返回方法:
BigDecimal add(BigDecimal other)
BigDecimal subtract(BigDecimal other)
BigDecimal multiply(BigDecimal other)
BigDecimal divide(BigDecimal other,RoundingMode mode)
判断两个浮点数大数值是否相等,并将结果返回一个值,相等返回0,小于返回负数,大于返回正数。
Int compareTo(BigDecimal other)
返回值为X或x/10(上标scale)的一个大小数:
Static BigDecimal valueOf(long x)
Static BigDecimal valueOf(long x,int scale)
数组:数组是一种数据结构,用于存储同类型值的集合。在声明数组变量时,需要指出数组,数组元素后跟[]和数组的名字。声明数组的两种方式:
Int [] a int a[]
一个完整的数组声明要有声明并初始化如:
Int []a;
Int []a=new int[100]//也可以将两条语句写在一起。
如:int[]smallPrimes={2,3,5,7,11,13}
定义一个匿名数组的方法:newint[]{17,19,23,29,31,37}
在java中数组长度可以为空,注意数组长度为0,不代表null,创建长度的0的数组的方法:new elementType[0]
数组拷贝:在java中允许将一个数组变量拷贝到另一个数组变量中。这两个变量将引用同一数组。用法如:int[] luckyNumbers=smallPrimes;
luckyNumbers[5]=12;//smallPrimes[5]也等于12
拷贝的方法:System.arraycopy(from,fromIndex,totoindex.count);
数组to必须有足够的空间来存放拷贝的元素:
如:int[]smallPrimes={2,3,5,7,11,13}
Int[]luckyNumbers={1001,1002,1003,1004,1005,1006,1007}
System.arraycopy(smallPrimes,2,luckyNumbers,3,4)
这句话的意思是从sumallPrimes中的第2个开始拷贝,拷贝到luckyNumber的数组元素3的位置,拷贝4个。
Java中的[]运算符被预定义为检查数组边界,而且没有指针运算,不能通过a+1来得到数组的下一个元素。