0% found this document useful (0 votes)
65 views4 pages

Java筆記

1. The document provides code examples for common Java programming tasks like input/output, string manipulation, arrays, exceptions handling, math operations, and file input. 2. Examples include using Scanner for user input, splitting strings, array declaration and usage, ArrayLists, rounding numbers, try-catch blocks, and reading from text files. 3. Key methods and classes demonstrated include Scanner, String, arrays, ArrayList, BigDecimal, Math, and FileInputStream.

Uploaded by

翰翔
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views4 pages

Java筆記

1. The document provides code examples for common Java programming tasks like input/output, string manipulation, arrays, exceptions handling, math operations, and file input. 2. Examples include using Scanner for user input, splitting strings, array declaration and usage, ArrayLists, rounding numbers, try-catch blocks, and reading from text files. 3. Key methods and classes demonstrated include Scanner, String, arrays, ArrayList, BigDecimal, Math, and FileInputStream.

Uploaded by

翰翔
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

加入工作區 cd C:/Users/hhjj55013/desktop/workspace (cd+目標 file)


2. 編譯 javac hw1.java
UTF-8 編譯 javac -encoding utf-8 hw1.java
3. 執行 java hw1

Scanner 輸入
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);// 使用輸入函式庫
String inp;
String[] str= new String[5];
inp = scanner.nextLine();//輸入句子
String[] sinp = inp.split(" ");//分割
for (int i = 0; i < sinp.length; i++){//分配
str[i]=sinp[i];
}
if(str[0].equals("edit")){ }//應用
String 應用
str[0].equals("search")//分大小寫
str[0].equalsIgnoreCase("edit")//不分大小寫
str[0]==null//空字串
str[j]=str[j].toLowerCase();//轉小寫
int m = Integer.parseInt(x); //Srting 轉 int
char y=x1.charAt(1); //Srting 轉 char,( )內放位置轉成空白
str = str.replace('%',' '); //Srting 內'%'轉成空白
str = str.replaceAll("\\s+","");//Srting 內任何空白字元轉成空白
str = str.replaceFirst("\\d","");//Srting 內第一個出現數字轉成空白
陣列宣告
public int [] now_quantity={0,0,0,0};
private int[] charm=new int[4];
String[] str= new String[2];
String[][] input=new String[2][2];
Arraylist 應用
import java.util.ArrayList;
ArrayList<fish> fishes = new ArrayList<fish>();//戰鬥區宣告
fishes.add(new fish(y,0,start_weight[y],true,0,0)); //添加新成員
wizard m=wizards.get(player); //取得資料
wizard n=wizards.get(1);//敵人
wizard n=wizards.get(Math.abs(player-1));//敵人
m.getblood();
數學計算
import java.math.BigDecimal;
import java.math.RoundingMode;
double val1 = 4312.186462;
System.out.println("Double value: "+val1);
BigDecimal bd = new BigDecimal(val1).setScale(2,RoundingMode.HALF_UP);
double val2 = bd.doubleValue();
System.out.println("Rounded Double value: "+val2);
Double value: 4312.186462
Rounded Double value: 4312.19
double d = 7435.9876;
double roundDbl = Math.round(d*100.0)/100.0;
System.out.println("Rounded Double value: "+roundDbl);
Rounded Double value: 7435.99
Math.abs(player) //返回絕對值
Math.pow(player) //返回次方
return (int)0; //強制轉換
Math.ceil(5.4); //無條件進位
Math.floor(5.4); //無條件捨去
try catch 錯誤預防
try {
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("輸入錯誤,請重新輸入!");
} catch (Exception e){
System.out.println("輸入錯誤,請重新輸入!");
} catch (NumberFormatException e){
System.out.println("status.txt error NE");
}
BigDecimal
import java.math.BigDecimal;
BigDecimal r = new BigDecimal(-3.3456789);
BigDecimal i1 = r.setScale(3,RoundingMode.UP);
BigDecimal i2 = r.setScale(3,RoundingMode.DOWN);
BigDecimal i3 = r.setScale(3,RoundingMode.CEILING);
BigDecimal i4 = r.setScale(3,RoundingMode.FLOOR);
BigDecimal i5 = r.setScale(3,RoundingMode.HALF_UP);
BigDecimal i6 = r.setScale(3,RoundingMode.HALF_DOWN);
BigDecimal i7 = r.setScale(3,RoundingMode.HALF_EVEN);
double val2 = bd.doubleValue();
int val2 = bd.intValue();
要從 BigDecimal 轉回來!!
1. UP:往該數絕對值大的方向進位
2. DOWN:往該數絕對值小的方向捨去
3. CEILING:無條件進位
4. FLOOR:無條件捨去
5. HALF_UP:四捨五入(不會有誤差)
6. HALF_DOWN:五捨去,六以上進位
r.setScale(3,RoundingMode.HALF_UP)
7. 這就代表四捨五入到小數點後第三位
file 輸入
import java.io.FileInputStream;
import java.io.FileNotFoundException;
String[] str= new String[2];
try {
Scanner doc = new Scanner(new FileInputStream("event.txt"));
while(doc.hasNextLine()){//偵測是否有下一行
String inp=doc.nextLine();//讀取下一行
String[] sinp = inp.split(" ");//分割處理
for (int j = 0; j < sinp.length; j++){
str[j]=sinp[j];
}
//應用
} catch (FileNotFoundException e) {
System.out.println("File not found");
}catch(NumberFormatException e){
System.out.println("txt error : "+str[0]+" "+str[1]);
}
(不包含空白字元、空白鍵、Tab)

換行為止(包含空白字元、空白鍵、Tab)

You might also like