Java 8.10
scanner类
Next
import java.util.Scanner;
public class Mine {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("next方式接收: ");
if(scan.hasNext()) {
String str1 = scan.next();
System.out.println("输入的数据为:" + str1);
}
scan.close();
}
}
Nextline (读入一行 可读入空格)
import java.util.Scanner;
public class Mine {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("nextline 方式接收: ");
if(scan.hasNextLine()) {
String str1 = scan.nextLine();
System.out.println("输入的数据为:" + str1);
}
scan.close();
}
}
同理的 nextdouble 等
import java.util.Scanner;
public class Mine {
static <T> void pln(T x) {
System.out.println(x);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
pln("Please enter nubers: ");
double sum = 0.0;
int cnt = 0;
while (scan.hasNextDouble()) {
double x = scan.nextDouble();
cnt++;
sum += x;
}
pln("The tot sum of " + cnt + " numbers is: " + sum);
pln("The average value of " + cnt + " numbers is: " + (double)(sum/cnt));
}
}
try-catch
public class Mine{
static <T> void pln(T x) {
System.out.println(x);
}
public static void main(String[] args) {
try {
int[] a = new int[2];
pln("3rd value: " + a[3]);
} catch (ArrayIndexOutOfBoundsException e) {
pln("Exeception thrown :" + e);
}
pln("Out of the block.");
}
}
throw:
import java.util.Scanner;
public class Mine{
static <T> void pln(T x) {
System.out.println(x);
}
public static void check_num(int num) {
if(num < 0) {
throw new IllegalArgumentException("Number must be postive!");
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
pln("Please enter a number: ");
int num = scan.nextInt();
Mine.check_num(num);
}
}
try-with-resources 方法 类似python的 with-open 可以结束语句块的时候自动关闭资源
import java.io.*;
public class Mine{
static <T> void pln(T x) {
System.out.println(x);
}
public static void main(String[] args) {
int linecnt = 0;
String line;
try(BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
while ((line = br.readLine()) != null) {
linecnt++;
pln("Line " + linecnt + " => " + line);
}
} catch (IOException e) {
pln("IOException in try block =>" +e.getMessage());
}
}
}
自定义异常类:
模拟银行账户操作
import java.*;
import java.util.Scanner;
public class Mine{
static <T> void pln(T x) {
System.out.println(x);
}
public static void main(String[] args) {
AccountSimulating cAccountSimulating = new AccountSimulating("123124124124");
Scanner scan = new Scanner(System.in);
pln("请输入存款金额:");
double deposit = scan.nextDouble();
cAccountSimulating.deposit(deposit);
try {
pln("\nWithdrawing ¥100...");
cAccountSimulating.withdraw(100);
pln("\nWithdrawing ¥600...");
cAccountSimulating.withdraw(600);
} catch (AccountException e) {
pln("Sorry, but you are short ¥" + e.get_amount());
e.printStackTrace();
}
}
}
class AccountSimulating{//模拟银行账户操作
private double rest_money;
private String card_id;
public AccountSimulating(String card_id) {
this.card_id = card_id;
}
public void deposit(double money) {//存
rest_money += money;
}
public void withdraw(double money) throws AccountException {//取
if(money <= rest_money) {
rest_money -= money;
}
else {
double delta = money - rest_money;
throw new AccountException(delta);
}
}
public double get_restmoney() {//返回余额
return rest_money;
}
public String get_cardid() {//返回卡号
return card_id;
}
}
class AccountException extends Exception{
private double amount;
public AccountException(double amount) {
this.amount = amount;
}
public double get_amount() {
return amount;
}
}