|
論壇說明 |
歡迎您來到『史萊姆論壇』 ^___^ 您目前正以訪客的身份瀏覽本論壇,訪客所擁有的權限將受到限制,您可以瀏覽本論壇大部份的版區與文章,但您將無法參與任何討論或是使用私人訊息與其他會員交流。若您希望擁有完整的使用權限,請註冊成為我們的一份子,註冊的程序十分簡單、快速,而且最重要的是--註冊是完全免費的! 請點擊這裡:『註冊成為我們的一份子!』 |
|
主題工具 | 顯示模式 |
2006-02-18, 05:22 AM | #1 |
榮譽會員
|
編寫一個程序。要求能夠輸入3個整數
編寫一個程序。要求能夠輸入3個整數,並輸出以這三個數為係數的一元二次方程的根。
import java.applet.Applet; import java.awt.*; public class ggg extends Applet { Label prompt1,prompt2,prompt3; TextField inputa,inputb,inputc; fc f; public void init() { prompt1=new Label("Input a:"); inputa=new TextField(5); prompt2=new Label("Input b:"); inputb=new TextField(5); prompt3=new Label("Input c:"); inputc=new TextField(5); add(prompt1); add(inputa); add(prompt2); add(inputb); add(prompt3); add(inputc); f=new fc(0,0,0); } public void paint(Graphics g) { if(!f.caculate()) g.drawString("The datas of "+f.geta()+"xx"+f.getb()+"x"+f.getc()+"=0 are incorrect. ",50,100); else if(f.rectifyroot()) g.drawString("The two keies of "+f.geta()+"xx"+f.getb()+"x"+f.getc()+"=0 are "+Double.toString(f.getresult1())+" and "+Double.toString(f.getresult2()),50,100); else g.drawString("The two keies are virtual keies.",50,100); } public boolean action(Event e,Object o) { if(e.target==inputa | e.target==inputb | e.target==inputc) { f.setinputa(Integer.parseInt(inputa.getText())); f.setinputb(Integer.parseInt(inputb.getText())); f.setinputc(Integer.parseInt(inputc.getText())); } f.caculate(); repaint(); return true; } } class fc { int m_a; int m_b; int m_c; double m_sum1; double m_sum2; double m_temp; fc(int a,int b,int c) { m_a=a;m_b=b;m_c=c; } public String geta() { return Integer.toString(m_a); } public String getb() { return Integer.toString(m_b); } public String getc() { return Integer.toString(m_c); } public boolean setinputa(int newa) { m_a=(int)newa; return true; } public boolean setinputb(int newb) { m_b=(int)newb; return true; } public boolean setinputc(int newc) { m_c=(int)newc; return true; } public boolean rectifyroot() { if(m_temp>=0) return true; else return false; } public boolean caculate() { m_temp=m_b*m_b-4*m_a*m_c; if(m_a!=0) { m_sum1=(-m_b+StrictMath.sqrt(m_temp))/(2*m_a); m_sum2=(-m_b-StrictMath.sqrt(m_temp))/(2*m_a); } else if(m_b!=0) m_sum2=m_sum1=-m_c/m_b; else return false; return true; } public double getresult1() { return m_sum1; } public double getresult2() { return m_sum2; } } |
__________________ |
|
送花文章: 3,
|