標題:
[隨堂練習] 字串轉數值 and try catch 用法
[打印本頁]
作者:
周政輝
時間:
2016-8-27 11:29
標題:
[隨堂練習] 字串轉數值 and try catch 用法
定義兩個字串
一個為數值字串 另一個為文字字串
並將兩個做數值相加
利用try catch finally 作呈現
本帖隱藏的內容需要回復才可以瀏覽
作者:
蔡庭豪
時間:
2016-8-27 11:30
package site.istak.org.tw;
public class Main {
public static void main(String args[]){
int all = 0;
String num1 ="250", num2= "number" ;
try{
all = Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(all);
}
catch(Exception x){
num2 = "100";
all = Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(all);
}
finally{
all+=200;
System.out.println(all);
}
}
}
複製代碼
作者:
張健勳
時間:
2016-8-27 11:31
package site.istak.org.tw;
public class Main {
public static void main(String args[])
{
String num1 = "94";
String num2 = "JohnCena";
int total = 0;
try
{
total = Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(total);
}catch(Exception ex)
{
num2 = "0";
total = Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(total);
}finally
{
total += 100;
System.out.println(total);
}
}
}
複製代碼
作者:
吳承勳
時間:
2016-8-27 11:32
package site.istak.org.tw;
public class Main {
public static void main(String args[])
{
String num1 = "100";
String num2 = "a";
int total = 0;
try
{
total = Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(total);
}
catch(Exception ex)
{ num2 = "0";
total = Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(total);
}
finally
{
total += 100;
System.out.println(total);
}
}
}
複製代碼
作者:
王彥甯
時間:
2016-8-27 11:32
package site.istak.org.tw;
public class Main {
public static void main(String args[]){
String num1 = "100";
String num2 = "a";
int total=0;
try{
total=Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(total);
}
catch(Exception ex){
num2="10";
total=Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(total);
}
finally{
total+=100;
System.out.println(total);
}
}
}
作者:
謝瀞儀
時間:
2016-8-27 11:41
package istak.org;
public class Main
{
public static void main(String args[])
{
String num1="100";
String num2="A";
int total=0;
try
{
total=Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(total);
}
catch(Exception ex)
{
num2="30";
total=Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(total);
}
finally
{
total+=100;
System.out.println(total);
}
}
}
複製代碼
作者:
蔡季樺
時間:
2016-8-27 11:59
package site.istak.org.tw;
public class Main {
public static void main(String args[])
{
String num1="100";
String num2="a";
int total=0;
try
{
total=Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(Integer.parseInt(num1)+Integer.parseInt(num2));
}catch(Exception ex)
{
num2="0";
total=Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(Integer.parseInt(num1)+Integer.parseInt(num2));
}finally{
total+=100;
System.out.println(total);
}
}
}
複製代碼
作者:
王彥甯
時間:
2016-8-27 12:00
package site.istak.org.tw;
public class Main {
public static void main(String args[]){
String num1 = "100";
String num2 = "a";
int total=0;
try{
total=Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(total);
}
catch(Exception ex){
num2="0";
total=Integer.parseInt(num1)+Integer.parseInt(num2);
System.out.println(total);
}
finally{
total+=100;
System.out.println(Math.pow(total, 2));
}
}
}
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2