出租车
package week1;/*** 完成出租车计费系统* @author zhaominghe**/import java.util.Scanner;public class Task5 {public static void main(String[] args) {Scanner input = new Scanner(System.in);double sumFee, basicFee, lowSpeedFee, requireFee, emptyFee;double miles;double lowSpeedTime, beginTime, endTime, totalTime;int require, hourRequire, lowSpeed, kmReturn;double unitprice = 2.3;System.out.println("请输入行驶总里程:");miles = input.nextDouble();//System.out.println("请输入开始乘坐出租车时间(24小时制):");//beginTime = input.nextDouble();//System.out.println("请输入结束乘坐出租车时间(24小时制):");//endTime = input.nextDouble();//System.out.println("请输入总乘车时间:(分钟制)");//totalTime = input.nextDouble();if(miles <= 3) {basicFee = 14 ;}else {basicFee = 14 + 2.3*(miles-3);}System.out.println("是否有低速行驶? 回答1/0");lowSpeed = input.nextInt();if(lowSpeed == 1) {System.out.println("请输入低速时间:");lowSpeedTime = input.nextDouble();lowSpeedFee = unitprice * Math.ceil(lowSpeedTime/5) / 2;}else {lowSpeedFee = 0;}System.out.println("是否预约叫车? 回答1/0");require = input.nextInt();if(require == 1) {System.out.println("是否在四小时内预约? 回答1/0");hourRequire = input.nextInt();if(hourRequire == 1) {requireFee = 5;}else {requireFee = 6;}}else {requireFee = 0;}System.out.println("是否往返2公里范围内");//miles >= 15 时候kmReturn = input.nextInt();if(kmReturn == 1) {emptyFee = 0;}if(miles>15) {emptyFee = (miles-15) * unitprice /2;}else {emptyFee = 0;}sumFee = Math.ceil(basicFee) + Math.ceil(lowSpeedFee) + Math.ceil(requireFee) + Math.ceil(emptyFee);System.out.println("基本费用是:"+Math.ceil(basicFee));System.out.println("低速行驶费用是:"+Math.ceil(lowSpeedFee));System.out.println("预约叫车费用是:"+Math.ceil(requireFee));System.out.println("空驶费用是:"+Math.ceil(emptyFee));System.out.println("总费用是:"+sumFee);}}
遇到的问题
- 数据类型问题:
- 注意 float 和 double 使用上的区别
- 强制转换的问题
- 判断条件问题
- 字符串不能进行判断,需要调用方法
退出多重循环!
字符串进行比较
Java的虚拟机在处理String时会在内存中开辟出一块单独的区域,用来存储字符串对象,这块内存区域被称为字符串缓冲池。当使用String a = “abc”;这样的语句进行定义一个引用的时候,首先会在字符串缓冲池中查找是否已经相同的对象,如果存在,那么就直接将这个对象的引用返回给a,如果不存在,则需要新建一个值为”abc”的对象,再将新的引用返回a。而String a = newString(“abc”);这样的语句明确告诉JVM想要产生一个新的String对象,并且值为”abc”,于是就在堆内存中的某一个小角落开辟了一个新的String对象。
用 “==” 比较
比较引用的变量地址是否相等,且相同的字符串在内存中只会存储一份
用 equals() 比较
- 比较内容是否相等
- equals()方法最重要的一点就是编程人员能够根据自己的需求去重写该方法,按照自己定义的规则判断两个对象是否相等
- 在String类中equals()方法重写为将字符串转化为字符数组,比较两个字符数组间的每个字符是否相同,所以当String对象调用equals()方法时,其比较的是两个字符串的内容是否相同,而不是指向字符串的地址
package com.de.test;public class StringA {public static void main(String[] args){String s1 = "hello";String s2 = "hello";String s3 = new String("hello");String s4 = new String("hello");System.out.println("s1:" + s1);System.out.println("s2:" + s2);System.out.println("s3:" + s3);System.out.println("s4:" + s4);System.out.println("----------比较内容是否相等---------------");System.out.println(s1.equals(s2));System.out.println(s2.equals(s3));System.out.println(s3.equals(s4));System.out.println("----------比较引用地址是否相等---------------");System.out.println(s1 == s2);System.out.println(s2 == s3);System.out.println(s3 == s4);}}结果:s1:hellos2:hellos3:hellos4:hello----------比较内容是否相等---------------truetruetrue----------比较引用地址是否相等---------------truefalsefalse
随机数的生成方式
new Random()
需要借助 java.util.Random 类来产生一个随机数发生器,构造函数有两个
Random()以当前时间为默认种子Random(long seed)以指定的种子值进行; 种子就是产生随机数的第一次使用值,机制是通过一个函数,将这个种子的值转化为随机数空间中的某一个点上,并且产生的随机数均匀的散布在空间中。以后产生的随机数都与前一个随机数有关。
Random random = new Random();num = random.nextInt(1000); 表示输出>0,<1000的整数。
随机字符串生成方法
for (int i = 0; i <= 100; i++){String sources = "0123456789"; // 加上一些字母,就可以生成pc站的验证码了Random rand = new Random();StringBuffer flag = new StringBuffer();for (int j = 0; j < 6; j++){flag.append(sources.charAt(rand.nextInt(9)) + "");}System.out.println(flag.toString());}
Math.random()
调用 java.lang.Math.random(); 生成 [0.0, 1.0) 的伪随机数(是以当前系统时间的相关数字作为种子数,按照特定复杂算法生成的,其实它生成的大量随机数是线性均匀分布的,黑客是完全可能通过返回的大量随机数结果破解种子数的,所以它并不是真正的随机,叫伪随机数。)
产生0.0到10.0之间的双精度浮点数 Math.random() * 10
产生0到10之间的整数 Math.round(Math.random() * 10)
来源 日后来看
currentTimeMillis()
数据类型转换 String.valueOf() 方法使用
数字的基本数据类型转换为 String
(1)String.valueOf(boolean b) : 将 boolean 变量 b 转换成字符串
(2)String.valueOf(char c) : 将 char 变量 c 转换成字符串
(3)String.valueOf(char[] data) : 将 char 数组 data 转换成字符串
(4)String.valueOf(char[] data, int offset, int count) : 将 char 数组 data 中 由 data[offset] 开始取 count 个元素 转换成字符串
(5)String.valueOf(double d) : 将 double 变量 d 转换成字符串
(6)String.valueOf(float f) : 将 float 变量 f 转换成字符串
(7)String.valueOf(int i) : 将 int 变量 i 转换成字符串
(8)String.valueOf(long l) : 将 long 变量 l 转换成字符串
(9)String.valueOf(Object obj) : 将 obj 对象转换成 字符串, 等于 obj.toString()
用法如下:
int i = 10;
String str = String.valueOf(i);
这时候 str 就会是 “10”
String 转换为数字的基本数据类型
要将 String 转换成基本数据型态转 ,大多需要使用基本数据型态的包装类别 ;比如说 String 转换成 byte ,可以使用 Byte.parseByte(String s) ,这一类的方法如果无法将 s 分析 则会丢出 NumberFormatException
(1)byte : Byte.parseByte(String s) : 将 s 转换成 byte
(2)Byte.parseByte(String s, int radix) : 以 radix 为基底 将 s 转换为 byte ,比如说 Byte.parseByte(“11”, 16) 会得到 17
(3)double : Double.parseDouble(String s) : 将 s 转换成 double
(4)float : Double.parseFloat(String s) : 将 s 转换成 float
(5)int : Integer.parseInt(String s) : 将 s 转换成 int
(6)long : Long.parseLong(String s)
