Java语言程序设计(基础篇) 第四章 数学函数、字符和字符串

第四章 数学函数、字符和字符串

4.2 常用数学函数

  方法分三类:三角函数方法(trigonometric method)、指数函数方法(exponent method)和服务方法(service method)

4.4 String类型

  String类型不是基本类型,而是引用类型(reference type)。

  

4.5.3 使用字符串修改彩票程序
 package com.chapter4;

 import java.util.Scanner;

 public class LotteryUsingString {
/**
* 使用字符串修改彩票程序
*/ public static void main(String[] args) { String lottery=""+(int)(Math.random()*10)+(int)(Math.random()*10); Scanner input=new Scanner(System.in);
System.out.println("输入您的彩票号码(两位数): ");
String guess=input.nextLine(); char lotteryDigit1=lottery.charAt(0);
char lotteryDigit2=lottery.charAt(1); char guessDigit1=guess.charAt(0);
char guessDigit2=guess.charAt(1); System.out.println("中奖号码为: "+lottery); if(guess.equals(lottery)){
System.out.println("恭喜您获得10000美元");
}else if(guessDigit1==lotteryDigit2 && guessDigit2==lotteryDigit1 ){
System.out.println("恭喜您获得3000美元");
}else if(guessDigit1==lotteryDigit1
|| guessDigit1==lotteryDigit2
|| guessDigit2==lotteryDigit1
|| guessDigit2==lotteryDigit2){
System.out.println("恭喜您获得1000美元");
}else{
System.out.println("不好意思,您没有中奖!");
}
} }
4.6 格式化控制输出
 package com.chapter4;

 public class FormatDemo {
/**
* 使用printf来显示一个表格的程序
*/ public static void main(String[] args) { System.out.printf("%-10s%-10s%-10s%-10s%-10s\n", "度(Degrees)", "弧度(Radians)", "正弦(Sine)", "余弦(Cosine)",
"正切(Tangent)"); int degrees = 30;// 度
double radians = Math.toRadians(degrees);
System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n", degrees, radians, Math.sin(radians), Math.cos(radians),
Math.tan(radians)); degrees = 60;
radians = Math.toRadians(degrees);
System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n", degrees, radians, Math.sin(radians), Math.cos(radians),
Math.tan(radians));
} }
上一篇:java的数学函数总结


下一篇:BZOJ4144: [AMPPZ2014]Petrol(最短路 最小生成树)