有關 exception |
尚未結案
|
chenglovebun
一般會員 發表:15 回覆:16 積分:6 註冊:2004-10-02 發送簡訊給我 |
A Pay Check Program
Write a class PayCheck that uses dialog boxes to compute the total gross pay of an hourly wage worker. The program should use input dialog boxes to get the number of hours worked and the hourly pay rate from the user. The program should use a message dialog to display the total gross pay. The pay calculation should assume the worker earns time and a half for overtime (for hours over 40).
以上是題目
我寫的程式碼 /* PayCheck.java Author : cheng-yuh,wu
design a payroll system */ import javax.swing.JOptionPane; public class PayCheck
{
public static void main (String [] args)
{
String numStr, result ,st ;
int again;
double mon,num;
double a=40.0;
do
{
numStr = JOptionPane.showInputDialog ("Enter number of hours you work: ");
st = JOptionPane.showInputDialog ("Enter your rate per hour : ");
num = Double.parseDouble(numStr);
mon = Double.parseDouble(st);
if (num <=a) {
result = "your pay check is " num*mon " dollars";
}
else{
result = "your pay check is " (mon*a (num-a)*1.5*mon) " dollars";}
JOptionPane.showMessageDialog (null, result); again = JOptionPane.showConfirmDialog (null, "try again?");
}
while (again == JOptionPane.YES_OPTION);
}
}
請各位高手提供意見
如果我想要驗證輸入的是不是數字也就是說只能輸入數字該如何做呢 (exception)
|
neoart
版主 發表:22 回覆:582 積分:425 註冊:2003-05-09 發送簡訊給我 |
參考拙作:
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=50354
末尾第二段有略述
你可以用:
try{ ... double dValue= Double.parseDouble(jtext.getText()); ... }catch(java.lang.NumberFormatException nfe){ //alert一下,user您打錯了. } 或是:如果你可以取得那個輸入物件的Text Filed的話,直接把事件攔截下來處理 .... Jtext.addKeyListener(new myKeyHandler()); ... ... class myKeyHandler extends KeyAdapter{ public void keyTyped(java.awt.event.KeyEvent e){ if(e.getKeyChar()=='1'.....){ //看看一下keyChar是不是介於0~9或是-,".',是的話,不用處理 }else{ e.consume();//事件取消(大事化小,小事化無) } } }或是實作InputVerifier.verify的方法,請參考 http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/InputVerifier.html |
chenglovebun
一般會員 發表:15 回覆:16 積分:6 註冊:2004-10-02 發送簡訊給我 |
|
chenglovebun
一般會員 發表:15 回覆:16 積分:6 註冊:2004-10-02 發送簡訊給我 |
/* PayCheck.java Author : cheng-yuh,wu
design a payroll system */
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.JOptionPane;
import java.io.IOException;
public class PayCheck1
{
public static void main (String [] args)
{
String numStr, result ,st ;
int again;
double mon,num;
double a=40.0;
do
{
numStr = JOptionPane.showInputDialog ("Enter number of hours you work: ");
st = JOptionPane.showInputDialog ("Enter your rate per hour : ");
num = Double.parseDouble(numStr);
mon = Double.parseDouble(st);
try{
double doubleValue= Double.parseDouble(Jtext.getText());
}
catch(java.lang.NumberFormatException nfe){
System.out.println("wrong number");
}
if (num <=a) {
result = "your pay check is " num*mon " dollars";
}
else{
result = "your pay check is " (mon*a (num-a)*1.5*mon) " dollars";}
JOptionPane.showMessageDialog (null, result); again = JOptionPane.showConfirmDialog (null, "try again?");
}
while (again == JOptionPane.YES_OPTION);
}
}
可否幫我看看try catch 那裡錯了
|
neoart
版主 發表:22 回覆:582 積分:425 註冊:2003-05-09 發送簡訊給我 |
請把你的:
..... ..... num = Double.parseDouble(numStr); mon = Double.parseDouble(st); try{ double doubleValue= Double.parseDouble(Jtext.getText()); }catch(java.lang.NumberFormatException nfe){ System.out.println("wrong number"); } ... ... ------------------------------------------------ 改成: ...... ...... dobule num = 0; double mon = 0; try{ num = Double.parseDouble(numStr); mon = Double.parseDouble(st); }catch(java.lang.NumberFormatException nfe){ System.out.println("wrong number"); } ..... |
chenglovebun
一般會員 發表:15 回覆:16 積分:6 註冊:2004-10-02 發送簡訊給我 |
|
chenglovebun
一般會員 發表:15 回覆:16 積分:6 註冊:2004-10-02 發送簡訊給我 |
|
neoart
版主 發表:22 回覆:582 積分:425 註冊:2003-05-09 發送簡訊給我 |
try{ .... .... }catch(java.lang.NumberFormatException nfe){ //System.out.println("wrong number"); JOptionPane.showMessageDialog( null,"Your Message","Message Title",JOptionPane.WARNING_MESSAGE ); return; //or...return the flag of fail,notify system to re-do this process } |
chenglovebun
一般會員 發表:15 回覆:16 積分:6 註冊:2004-10-02 發送簡訊給我 |
|
chenglovebun
一般會員 發表:15 回覆:16 積分:6 註冊:2004-10-02 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |