Java原书第8版(第二章知识)

package Chapter1;


import java.text.SimpleDateFormat;


/*
 * 基础篇第二章
 * 
 * 
 */
public class Q2 {
public static void main(String[] args) {


double radius;// 半径
double area;// 面积
// Assign a radius获取半径
Scanner input1 = new Scanner(System.in);
System.out.print("please input the value of radius:");
radius = input1.nextDouble();// readLine()读一行(回车键为结束标志);next()读一个字符串(以空格为结束标志)
// Computer area
area = (Math.PI) * radius * radius;
// Display the result
System.out.println("The circle area of radius " + radius + "is:\n"
+ area);
// 通过半径求圆的面积


Scanner input2 = new Scanner(System.in);
System.out.print("Enter three numbers:");
double number1 = input2.nextDouble();
double number2 = input2.nextDouble();
int number3 = input2.nextInt();
System.out.println("These number's average is\n"
+ (number1 + number2 + number3) / 3);
// 连续读取三个数字求平均数


int i, j, k;// 变量一起申明


final double PI = 3.1415926;// 定义π,按照习惯,常量用大写字母表示,例:PI


Scanner input3 = new Scanner(System.in);
System.out.print("input the seconds");
long seconds = input3.nextLong();
long hours = seconds / 3600;
long minutes = (seconds - hours * 3600) / 60;
long sec = seconds % 60;
System.out.println(hours + "hous" + minutes + "minutes" + sec
+ "seconds");
// 输入秒数换算小时分钟秒


System.out.println(0xFFFF);// 输出16进制数
System.out.println(076);// 输出8进制数


System.out.println("1.0/3.0 is:" + 1.0 / 3.0);
System.out.println("1.0F/3.0F is" + 1.0F / 3.0F);
// 浮点数直接带小数点,默认为double类型,因为double比float精确,用float必须加F
// double和float 都是浮点数,浮点数是以科学计数法存储的


Scanner input4 = new Scanner(System.in);
System.out.println("Enter fahrenheit :");
double Fahrenheit = input4.nextDouble();
double celsius = (5.0 / 9) * (Fahrenheit - 32);
System.out.println("Fahrenheit " + Fahrenheit + " is " + celsius
+ " celsius");
// 温度转换


long totalMilliSeconds = System.currentTimeMillis();
long totalSeconds = totalMilliSeconds / 1000;
long currentSeconds = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currentMinutes = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currentHours = totalHours % 24;
System.out.println("CurrentTime:   " + currentHours + ":"
+ currentMinutes + ":" + currentSeconds + "  GMT");
Date date = new Date(totalMilliSeconds);
SimpleDateFormat form = new SimpleDateFormat("yyyy年MM月dd日——hh:mm:ss");
System.out.println(form.format(date));
// 获取系统时间
// 简捷运算符 //+= -= /= %= *= //++a a++ --a a--


double x = 1.0;
double y = 5.0;
// double z=x--+(++y);//z=7
double z = x + (x++) + (++x) + y;// z=10
System.out.println("x=" + x + "\ty=" + y + "\tz=" + z);


// 拓宽类型可以隐式完成,,但是缩窄类型必须显式完成
System.out.println((int) 1.7);// 1
System.out.println((double) 1 / 2);// 0.5
System.out.println(1 / 2);// 0
double d = 4.5;
int i1 = (int) d;// i=4,d=4.5 int强转时直接去掉小数部分
byte b = (byte) i1;// b=4
System.out.println(i1 + "\n" + d + "\n" + b);

// 保留营业税后两位小数
Scanner input5 = new Scanner(System.in);
System.out.println("input the purchaseAmount:");
double purchaseAmount = input5.nextDouble();
double tax = (int) ((purchaseAmount * 0.06) * 100) / 100.0;
System.out.println("tax is:" + tax);


// 用Math.pow(a,b)来计算a的b次方
System.out.println(Math.pow(4, 0.5));// 2.0
System.out.println(Math.pow(12, 2));// 144.0


Scanner input6 = new Scanner(System.in);
System.out.println("input loan amount(eg:12000):");
double loanAmount = input6.nextDouble();
System.out.println("input annual rate(eg:8.25):");
double annualRate = input6.nextDouble();
double monthRate = annualRate / 1200;
System.out.println("input loan yeras(eg:5):");
int loanYear = input6.nextInt();
double monthPayment = (loanAmount * monthRate)
/ (1 - 1 / (Math.pow(1 + monthRate, 12 * loanYear)));
double totalPayment = monthPayment * loanYear * 12;
System.out.println("monthPayment:" + (int) (monthPayment * 100) / 100.0
+ "\t totalPayment:" + (int) (totalPayment * 100) / 100.0);


// 'A'表示一个字符 "A"表示一个字符串


JOptionPane.showMessageDialog(null, "\u6b22\u8FCE \u03b2\u03b3",
"\u6b22\u8FCE welcome", JOptionPane.INFORMATION_MESSAGE);
char ch = 'A';
System.out.println(++ch);


/*
* 转义字符:\b 退格键\u0008 \t Tab键(制表符) \n 换行符号 \f 换页 \r 回车键 \\ 反斜杠 \u005c \'
* 单引号 \u0027 \" 双引号 \u0022
*/


System.out.println("他说:\"这东西好吃\"");// 输出双引号


// 将一个整数转换为char型数据时,只用到该数据低16位
// 将浮点型转换为char类型时,先取整int,再转为char
// 如果转换结果适用于目标变量,可以使用隐式转换,不然就要显式转换
char ch2 = (char) 0xAB0041;
System.out.println(ch2);// 输出A 低16位是0041,化为10进制是65,就是A的统一码 a是97
char ch3 = (char) 65.25;
System.out.println(ch3);// 输出A 65.25取整是65
byte b1 = 'a';
int a = 'a';
byte b2 = (byte) '\uFFF4';// uFFF4不适用于一个字节范围
int i2 = '2' + '3';// 字符2的统一码是50 字符3的统一码是51
System.out.println("i =:" + i2);// 输出101


Scanner input7 = new Scanner(System.in);
System.out.println("please input an amount:");
double amount = input7.nextDouble();
int remainingAmount = (int) (amount * 100);
int oneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
int quarterDollars = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
int dimesDollars = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
int nickelDollars = remainingAmount / 5;
remainingAmount = remainingAmount % 5;
int pennisDollars = remainingAmount;
System.out
.println("your amount " + amount + "consist of \n" + "\t"
+ oneDollars + "dollars\n" + "\t" + quarterDollars
+ "quarters\n" + "\t" + dimesDollars + "dimes\n" + "\t"
+ nickelDollars + "nickels\n" + "\t" + pennisDollars
+ "pennis");


// String是java中预定义的类,不是基本类型,而是引用类型
String message = "Welcome" + "to" + "java";
message += "and java is fun";
int i11 = 1, j1 = 2;
System.out.println("i+j=" + i11 + j1);// 输出i+j=12
System.out.println("i+j=" + (i11 + j1));// 输出i+j=3


// 从键盘读取字符串
Scanner input8 = new Scanner(System.in);
System.out.println("Enter three strings:");
// for example: welcome to java
String str1 = input8.next();// welcome
String str2 = input8.next();// to
String str3 = input8.next();// java
System.out.println("str1= " + str1 + "\tstr2= " + str2 + "\tstr3= "
+ str3);
// next()是以' ' '\t' '\f' '\r' '\n'结束的
// nextLine()是读取整行


// 从输入对话框获取输入
String input9 = JOptionPane.showInputDialog("Enter a input");
String input10 = JOptionPane.showInputDialog(null, "Enter a input ",
"input dialog demo", JOptionPane.QUESTION_MESSAGE);
// 将字符串转换为数字
int intValue = Integer.parseInt("123");
double doubleValue = Double.parseDouble("12.34");


// 程序接收年利率a、年数b、贷款总额c,然后再显示月支付额和总支付额d
String loanAmountStr = JOptionPane
.showInputDialog("Enter the loanAmount:");
double loanAmount1 = Double.parseDouble(loanAmountStr);
String annualRateStr = JOptionPane
.showInputDialog("Enter the annualRate:");
double annualRate1 = Double.parseDouble(annualRateStr);
double monthRate1 = annualRate1 / 1200;
String loanYearStr = JOptionPane.showInputDialog("Enter the loanYear:");
int loanYear1 = Integer.parseInt(loanYearStr);
double monthPayment1 = (loanAmount1 * monthRate1)
/ (1 - 1 / (Math.pow(1 + monthRate1, 12 * loanYear1)));
double totalPayment1 = monthPayment1 * loanYear1 * 12;
monthPayment1 = (int) (monthPayment1 * 100) / 100.0;
totalPayment1 = (int) (totalPayment1 * 100) / 100.0;
String output = "The monthly payment is :" + monthPayment1
+ "\nThe total payment is :" + totalPayment1;
JOptionPane.showMessageDialog(null, output);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值