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

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

4.2 常用数学函数

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

4.4 String类型

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

  

 4.5.3 使用字符串修改彩票程序
 1 package com.chapter4;
 2 
 3 import java.util.Scanner;
 4 
 5 public class LotteryUsingString {
 6     /**
 7      * 使用字符串修改彩票程序
 8      */
 9 
10     public static void main(String[] args) {
11         
12         String lottery=""+(int)(Math.random()*10)+(int)(Math.random()*10);
13         
14         Scanner input=new Scanner(System.in);
15         System.out.println("输入您的彩票号码(两位数): ");
16         String guess=input.nextLine();
17         
18         char lotteryDigit1=lottery.charAt(0);
19         char lotteryDigit2=lottery.charAt(1);
20         
21         char guessDigit1=guess.charAt(0);
22         char guessDigit2=guess.charAt(1);
23         
24         System.out.println("中奖号码为: "+lottery);
25         
26         if(guess.equals(lottery)){
27             System.out.println("恭喜您获得10000美元");
28         }else if(guessDigit1==lotteryDigit2 && guessDigit2==lotteryDigit1 ){
29             System.out.println("恭喜您获得3000美元");
30         }else if(guessDigit1==lotteryDigit1 
31                 || guessDigit1==lotteryDigit2 
32                 || guessDigit2==lotteryDigit1 
33                 || guessDigit2==lotteryDigit2){
34             System.out.println("恭喜您获得1000美元");
35         }else{
36             System.out.println("不好意思,您没有中奖!");
37         }
38     }
39 
40 }

 

4.6 格式化控制输出
 1 package com.chapter4;
 2 
 3 public class FormatDemo {
 4     /**
 5      * 使用printf来显示一个表格的程序
 6      */
 7 
 8     public static void main(String[] args) {
 9 
10         System.out.printf("%-10s%-10s%-10s%-10s%-10s\n", "度(Degrees)", "弧度(Radians)", "正弦(Sine)", "余弦(Cosine)",
11                 "正切(Tangent)");
12 
13         int degrees = 30;//
14         double radians = Math.toRadians(degrees);
15         System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n", degrees, radians, Math.sin(radians), Math.cos(radians),
16                 Math.tan(radians));
17 
18         degrees = 60;
19         radians = Math.toRadians(degrees);
20         System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n", degrees, radians, Math.sin(radians), Math.cos(radians),
21                 Math.tan(radians));
22     }
23 
24 }

 

posted @ 2016-12-18 20:50  Young_Yang_Yang  阅读(349)  评论(0编辑  收藏  举报