| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 685 人关注过本帖
标题:这是个小数化分数的swt代码,不知道怎么地?算0.555老是死
只看楼主 加入收藏
JerryChen
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2006-8-14
收藏
 问题点数:0 回复次数:1 
这是个小数化分数的swt代码,不知道怎么地?算0.555老是死

package cn.anyp.jerrychen51;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class xshfs {

private static Text text2;
private static Text text1;
/**
* Launch the application
* @param args
*/
public static double a,b,c=1,i;
//求最大公约数函数
/*static double zdgys(double c,double d)
{double o=1,p=1;
while(c/o!=d/p)
{while(c/o>d/p)
o++;
while(c/o<d/p)
p++;
}
while(c/o==d/p)
break;
return (c/o);
}*/

//用辗转相除法求最大公约数函数
static double zzxc(double a,double b)
{double c;
while(a%b!=0)
{c=a%b;
a=b;
b=c;
zzxc(a,b);
}
return b;
}
//对实数取整函数
static double quzheng(double d)
{ double dw;
int iw;
iw=(int)d;
dw=iw;
return dw;
}
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell dtfShell = new Shell();
dtfShell.setSize(500, 375);
dtfShell.setText("小数转化分数");
//

dtfShell.open();

final Button button1 = new Button(dtfShell, SWT.NONE);
button1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
a=Double.parseDouble(text1.getText());
b=quzheng(a);
while(a-b!=0)
{a*=10;
c*=10;
b=quzheng(a);
}
i=zzxc(a,c);
a/=i;
c/=i;

text2.setText(String.valueOf(a)+"/"+String.valueOf(c));
// label3.setText(String.valueOf(a)+"/"+String.valueOf(c));
c=1; //c要归位,不然会出现第二次计算错误。
}
});
button1.setText("确定");
button1.setBounds(87, 255, 74, 33);

final Button button2 = new Button(dtfShell, SWT.NONE);
button2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
dtfShell.close();
}
});

button2.setText("退出");
button2.setBounds(307, 257, 74, 28);

text1 = new Text(dtfShell, SWT.BORDER);
text1.setBounds(87, 61, 105, 25);

final Label label2 = new Label(dtfShell, SWT.NONE);
label2.setText("分数值");
label2.setBounds(40, 154, 36, 13);

text2 = new Text(dtfShell, SWT.BORDER);
text2.setBounds(87, 151, 105, 25);

final Label label1 = new Label(dtfShell, SWT.NONE);
label1.setText("小数值");
label1.setBounds(40, 64, 41, 13);

final Label label3 = new Label(dtfShell, SWT.NONE);
label3.setBounds(250, 154, 77, 25);

final Button button3 = new Button(dtfShell, SWT.NONE);
button3.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
text1.setText("");
text2.setText("");
}
});
button3.setText("清空");
button3.setBounds(204, 257, 60, 28);
dtfShell.layout();
while (!dtfShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

}
这是个小数化分数的swt代码,不知道怎么地,算其他的都好着呢,但算0.555就死机了。甚者算0.5555都可以运行,不知道是怎么回事,另外,算0.3333也死机。Java真的有这么慢吗?

搜索更多相关主题的帖子: 小数 swt 代码 化分 
2007-04-19 22:17
事过境迁
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2006-8-2
收藏
得分:0 
怎么我这里没有你用的 包
2007-04-21 13:14
快速回复:这是个小数化分数的swt代码,不知道怎么地?算0.555老是死
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014088 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved