JAVA初学笔记(5)

tech2022-09-30  73

笔记 学习网站:Java教程|菜鸟教程


文章目录

1 Java Scanner 类1.1 使用 next 方法1.2 使用 nextLine 方法1.3 next() 与 nextLine() 区别 2 Java 异常处理2.1 捕获异常2.2 多重捕获块2.3 finally关键字2.4 通用异常

1 Java Scanner 类

java.util.Scanner 是 Java5 的新特征,可以通过 Scanner 类来获取用户的输入。

下面是创建 Scanner 对象的基本语法:

Scanner s = new Scanner(System.in);

通过 Scanner 类的 next() 与 nextLine() 方法获取输入的字符串,在读取前一般需要使用 hasNext 与 hasNextLine 判断是否还有输入的数据:

1.1 使用 next 方法

import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 从键盘接收数据 // next方式接收字符串 System.out.println("next方式接收:"); // 判断是否还有输入 if (scan.hasNext()) { String str1 = scan.next(); System.out.println("输入的数据为:" + str1); } scan.close(); } }

1.2 使用 nextLine 方法

import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 从键盘接收数据 // nextLine方式接收字符串 System.out.println("nextLine方式接收:"); // 判断是否还有输入 if (scan.hasNextLine()) { String str2 = scan.nextLine(); System.out.println("输入的数据为:" + str2); } scan.close(); } }

1.3 next() 与 nextLine() 区别

next():

一定要读取到有效字符后才可以结束输入。对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。next() 不能得到带有空格的字符串。

nextLine():

以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。可以获得空白。

如果要输入 int 或 float 类型的数据,在 Scanner 类中也有支持,但是在输入之前最好先使用 hasNextXxx() 方法进行验证,再使用 nextXxx() 来读取:

import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 从键盘接收数据 int i = 0; float f = 0.0f; System.out.print("输入整数:"); if (scan.hasNextInt()) { // 判断输入的是否是整数 i = scan.nextInt(); // 接收整数 System.out.println("整数数据:" + i); } else { // 输入错误的信息 System.out.println("输入的不是整数!"); } System.out.print("输入小数:"); if (scan.hasNextFloat()) { // 判断输入的是否是小数 f = scan.nextFloat(); // 接收小数 System.out.println("小数数据:" + f); } else { // 输入错误的信息 System.out.println("输入的不是小数!"); } scan.close(); } }

以下实例我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果:

import java.util.Scanner; class ScannerDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double sum = 0; int m = 0; while (scan.hasNextDouble()) { double x = scan.nextDouble(); m = m + 1; sum = sum + x; } System.out.println(m + "个数的和为" + sum); System.out.println(m + "个数的平均值是" + (sum / m)); scan.close(); } }

例子

import java.util.Scanner; class ScannerDemo { public static void main(String []args) { Scanner input = new Scanner(System.in); System.out.println("请输入一个字符串(中间能加空格或符号)"); String a = input.nextLine(); System.out.println("请输入一个字符串(中间不能加空格或符号)"); String b = input.next(); System.out.println("请输入一个整数"); int c; c = input.nextInt(); System.out.println("请输入一个double类型的小数"); double d = input.nextDouble(); System.out.println("请输入一个float类型的小数"); float f = input.nextFloat(); } }

2 Java 异常处理

2.1 捕获异常

使用 try 和 catch 关键字可以捕获异常。try/catch 代码块放在异常可能发生的地方。

try/catch代码块中的代码称为保护代码,使用 try/catch 的语法如下:

try { // 程序代码 }catch(ExceptionName e1) { //Catch 块 }

Catch 语句包含要捕获异常类型的声明。当保护代码块中发生一个异常时,try 后面的 catch 块就会被检查。如果发生的异常包含在 catch 块中,异常会被传递到该 catch 块,这和传递一个参数到方法是一样。

例子

import java.io.*; public class ExcepTest{ public static void main(String args[]){ try{ int a[] = new int[2]; System.out.println("Access element three :" + a[3]); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Exception thrown :" + e); } System.out.println("Out of the block"); } }

2.2 多重捕获块

一个 try 代码块后面跟随多个 catch 代码块的情况就叫多重捕获。

try{ // 程序代码 }catch(异常类型1 异常的变量名1){ // 程序代码 }catch(异常类型2 异常的变量名2){ // 程序代码 }catch(异常类型2 异常的变量名2){ // 程序代码 }

上面的代码段包含了 3 个 catch块。可以在 try 语句后面添加任意数量的 catch 块。如果保护代码中发生异常,异常被抛给第一个 catch 块。如果抛出异常的数据类型与 ExceptionType1 匹配,它在这里就会被捕获。如果不匹配,它会被传递给第二个 catch 块。如此,直到异常被捕获或者通过所有的 catch 块。

2.3 finally关键字

finally 关键字用来创建在 try 代码块后面执行的代码块。

无论是否发生异常,finally 代码块中的代码总会被执行。

在 finally 代码块中,可以运行清理类型等收尾善后性质的语句。

finally 代码块出现在 catch 代码块最后,语法如下:

try{ // 程序代码 }catch(异常类型1 异常的变量名1){ // 程序代码 }catch(异常类型2 异常的变量名2){ // 程序代码 }finally{ // 程序代码 }

注意下面事项:

catch 不能独立于 try 存在。在 try/catch 后面添加 finally 块并非强制性要求的。try 代码后不能既没 catch 块也没 finally 块。try, catch, finally 块之间不能添加任何代码。

2.4 通用异常

在Java中定义了两种类型的异常和错误。

JVM(Java虚拟机) 异常:由 JVM 抛出的异常或错误。例如:NullPointerException 类,ArrayIndexOutOfBoundsException 类,ClassCastException 类。程序级异常:由程序或者API程序抛出的异常。例如 IllegalArgumentException 类,IllegalStateException 类。
最新回复(0)