java学习笔记(二)Console input and output

           Chaper 2      Console input and output

 

2.1 Screen Output

2.1.1 System.out println():打印语句话后 有个回车符

      

 

2.1.2 System.out.print(); 打印末尾不回车

         

二者的区别也可以等效成:            

                    

 

2.1.3 System.out.printf();主要用于打印浮点数的时候应用

                 

解释:%6.2f 会被price代替,但是在表示浮点数的时候,需要限制他的位数,此时是6;(.2)表示,小数点后有2位,然后f表示的是floating-point number

The format specifier  %6.2f says to output a floating-point number in a field (number of spaces) of width  6 (room for six characters) and to show exactly two digits after the decimal point.

由于规定了6位有效数字长度, 因此少的位数需要用空格占位。

Because  "19.80" has only  five characters, a blank character is added to obtain the six- character string  " 19.80" 

                      

例子:

 

 

2.1.4 NumberFormat类

导入:import java.text.NumberFormat; 才可以使用

                        

该类的对象可以 使用其中的一个方法format。用来获得一个浮点数,并且返回一个string值的数字, 使用默认或者自定义的货币货币种类;This object moneyFormatter has a method named  format that takes a floating-point number as an argument and returns a  String value representing that number in the local currency (the default currency) 例如:

                  

                   

 

2.1.5 import class and package

packages(包)is liararies; 包中包含很多类,只要引用后可以直接使用其中的类和封装的方法。

java包含一些默认的包,会自动的进行引入。不需要import特殊的进行说明

---------------------------------------------------------------------------------------------------------------------------

2.2 console input using the scanner class

2.2.1 scanner class

                   

.next()和.nextLine()函数都接收字符串,区别如下:

其他一些常见的函数:

例子:

                   

---------------------------------------------------------------------------------------------------------------------------

空字符串 (empty String)

"Hello" 包含5个字符,空字符串表示的是含有0个字符。

String s1 = ""; 表示的是空字符串

String s2 = " "; 表示的是一个字符,字符是空格

---------------------------------------------------------------------------------------------------------------------------

自定义--delimiter(分隔符)

Scanner keyboard2 = new Scanner(System.in);

keyboard2.useDelimiter("##"); (表示接收到##时,才完成一个字符串接收,一般next默认的是空格,nextLine 默认的是回车)

After this invocation of the  useDelimiter method,  "##" will be the only input delimiter for the input object  keyboard2 . Note that whitespace will no longer be a delimiter for keyboard input done with  keyboard2 .

用户输入:one two##three##

word1 = keyboard2.next();  word1 = one two
word2 = keyboard2.next();  word2 = three

---------------------------------------------------------------------------------------------------------------------------

2.3  file input

使用scanner读取指定文件的内容

 首先引入两个包:

import java.io.FileInputStream; (目的:连接java program 和文件所在的 disk --- 磁盘)

import java.io.FileNotFoundException; (目的: 如果打开的文件不存在时,需要用到该类)

            

读取完全部内容后,需要关闭文件。如果判断已经全部读取完毕呢?使用函数:fileIn,hasNextLine(), 该函数会返回一个boolean值,用来反馈是否还有文件内容可以被读取。同时关闭文件后,分配的资源会得到释放。

Unlike reading from the console, we might want to know if we have reached the end of the file. We can use  fileIn.hasNextLine() to determine if there is data to read. When we are done with the file, we can close it with  fileIn.close(), which will release any resources that have been allocated by Java in association with the file

---------------------------------------------------------------------------------------------------------------------------

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值