Re: [問題] java的問題
※ 引述《roger70349.bbs@ptt.cc (roger70349)》之銘言:
> 請各位大大能否幫我看一下下面的程式那裡有問題
> 寫一個applet程式,可計算M~N的總和(其中M,N為使用者自行輸入)
> import javax.swing.JOptionPane;
> public class MtoN
> {
> int sum=0;
> int m,n;
> public MtoN (int x)
> { n=x;
> for (int i=m;i<=n;i++)
> {
> sum=sum+i;
> }
> }
> public static void main(String args[])
> {
> String s = JOptionPane.showInputDialog("請輸入n值:");
> String s1= JOptionPane.showinputDialog("請輸入m值");
> int n=Integer.parseInt(s);
> int m=Integer.parseInt(s1);
> MtoN a=new Sum1toN(n);
> MtoN a=new Sum1toN(m);
> JOptionPane.showMessageDialog(null,a.n"1到"+a.m+"加總=" + a.sum,"加總
> ",JOptionPane.PLAIN_MESSAGE );
> }
> }
> 還有有題是請輸入一個十六進位整數, 將其轉換為
> (1) 二進位 (2)八進位 (3)十進位 輸出
這不算是Applet的程式,你比較一下下面的程式哪裡有差別,因為有很多地方要改
import javax.swing.JOptionPane;
public class MtoN
{
int sum=0;
int m,n;
public MtoN(int m,int n)
{
for (int i=m;i<=n;i++)
{
sum=sum+i;
}
}
public static void main(String args[])
{
String s = JOptionPane.showInputDialog("請輸入m值:");
String s1= JOptionPane.showInputDialog("請輸入n值");
int m=Integer.parseInt(s);
int n=Integer.parseInt(s1);
MtoN a=new MtoN(m,n);
JOptionPane.showMessageDialog(null,m+"到"+n+"的加總="+a.sum,"加總",
JOptionPane.PLAIN_MESSAGE );
}
}
--
┌─────◆KKCITY◆─────┐ ◢ ◤ 找歌最方便 KKBOX 歌詞搜尋!!
│ bbs.kkcity.com.tw │ \^_^ / ★http://www.kkbox.com.tw★
└──《From:220.138.47.102 》──┘ ◤ 唱片公司授權,音樂盡情下載
--
java 近期熱門文章
PTT數位生活區 即時熱門文章