史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   編寫一個程序。要求能夠輸入3個整數 (http://forum.slime.com.tw/thread168521.html)

psac 2006-02-18 05:22 AM

編寫一個程序。要求能夠輸入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;
}
}


所有時間均為台北時間。現在的時間是 01:15 PM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1