題目來源: http://ac.jobdu.com/problem.php?pid=1010
我的代碼:
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.util.HashMap;
import
java.util.Map;
public
class
Main{
public
static
void
main (String[] args){
BufferedReader br =
new
BufferedReader(
new
InputStreamReader(System.in));
String ss =
null
;
String[] sa,sa1,sa2;
int
num1,num2;
try
{
while
((ss = br.readLine()) !=
null
){
sa = ss.split(
"\\+"
);
sa1 = sa[
0
].split(
" "
);
sa2 = sa[
1
].split(
" "
);
num1 = trans(sa1);
num2 = trans(sa2);
if
((num1 ==
0
) && (num2 ==
0
)){
break
;
}
System.out.println(num1 + num2);
}
}
catch
(IOException e){
e.printStackTrace();
}
}
public
static
int
trans(String[] sa){
Map<String,Integer> map=
new
HashMap<String, Integer>();
map.put(
"nine"
,
9
);
map.put(
"eight"
,
8
);
map.put(
"seven"
,
7
);
map.put(
"six"
,
6
);
map.put(
"five"
,
5
);
map.put(
"four"
,
4
);
map.put(
"three"
,
3
);
map.put(
"two"
,
2
);
map.put(
"one"
,
1
);
map.put(
"zero"
,
0
);
int
num =
0
;
int
i;
for
(i =
0
;i < sa.length; i ++){
sa[i] = sa[i].toLowerCase();
if
(map.get(sa[i]) ==
null
){
continue
;
}
switch
(map.get(sa[i])){
case
9
: num =
10
* num +
9
;
break
;
case
8
: num =
10
* num +
8
;
break
;
case
7
: num =
10
* num +
7
;
break
;
case
6
: num =
10
* num +
6
;
break
;
case
5
: num =
10
* num +
5
;
break
;
case
4
: num =
10
* num +
4
;
break
;
case
3
: num =
10
* num +
3
;
break
;
case
2
: num =
10
* num +
2
;
break
;
case
1
: num =
10
* num +
1
;
break
;
case
0
: num =
10
* num +
0
;
break
;
default
:
break
;
}
}
return
num;
}
};
注意:
1.String .split() 是java.lang函數
http://www.cnblogs.com/mingforyou/archive/2013/09/03/3299569.html
(1).如果用“.”作爲分隔的話,必須是如下寫法,String.split("\\."),這樣才能正確的分隔開,不能用String.split(".");
(2).如果用“|”作爲分隔的話,必須是如下寫法,String.split("\\|"),這樣才能正確的分隔開,不能用String.split("|");
“.”和“|”都是轉義字符,必須得加"\\";
(3).如果有多個分隔符,可以用|連接,如,“acount=? and uu =? or n=?”,把三個都分隔出來,可以用String.split("and|or");(4).對於一些用特殊符號分隔的必須用轉義字符,不然會拋出異常,或者得不到我們預期的結果
split的參數regex是一個 regular-expression的匹配模式而不是一個簡單的String,他對一些特殊的字符可能會出現你預想不到的結果
常見的 "|" -> "\\|" "*" -> "\\*" "+" -> "\\+" "\" ->"\\\"
2. switch-case語句
switch語句的判斷條件可以接受int,byte,char,short, enum不能接受其他類型
case 後面必須是常量表達式constant expressions
一旦case匹配,就會順序執行後面的程序代碼,而不管後面的case是否匹配,直到遇見break,利用這一特性可以讓好幾個case執行統一語句
switch在java7纔開始支持判斷字符串,針對switch不能支持字符串這個問題,可以使用map容器
map<int,string> (或 map<string,int> ),用字符串對應的索引值(鍵值)來進行匹配
順便普及一下枚舉類型:
java.lang.Enum.ordinal()方法返回此枚舉常量的序數(其枚舉聲明中的位置,其中初始常量分配的序數爲零)。
例子:
package com.yiibai;
import java.lang.*;
// enum showing Mobile prices
enum Mobile {
Samsung(400), Nokia(250),Motorola(325);
int price;
Mobile(int p) {
price = p;
}
int showPrice() {
return price;
}
}
public class EnumDemo {
public static void main(String args[]) {
System.out.println("CellPhone List:");
for(Mobile m : Mobile.values()) {
System.out.println(m + " costs " + m.showPrice() + " dollars");
}
Mobile ret = Mobile.Samsung;
System.out.println("The ordinal is = " + ret.ordinal());
System.out.println("MobileName = " + ret.name());
}
}
輸出:
CellPhone List:
Samsung costs 400 dollars
Nokia costs 250 dollars
Motorola costs 325 dollars
The ordinal is = 0
MobileName = Samsung
看完這個例子就比較瞭解枚舉了。
3.switch null http://www.ylzx8.cn/kaifayuyan/program/301058.html
Java中null表示一個不確定的對象,所以它是沒有類型的,null本身不是對象,也不是Objcet的實例
所以null更不屬於int,byte,char,short, enum中的任一類型,所以傳入null的話會報錯