简明现代魔法 -> Java编程语言 -> SWT之路:计算某数的负N次方

SWT之路:计算某数的负N次方

2009-11-13

有个同事来问,1.1的-5次方是多少?

于是自己写个程序来计算下。

1.1的-5次方也就是1.1的5次方的倒数,java程序如下:

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.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class pow {

	protected Shell shell;
	private Text text;
	private Text text_1;
	double dishu;
	double zhishu;
	static Label resultShow; 

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			pow window = new pow();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("计算某数N次方的倒数");
		{
			Group group = new Group(shell, SWT.NONE);
			group.setText("输入底数和指数");
			group.setBounds(10, 10, 422, 120);
			{
				text = new Text(group, SWT.BORDER);
				text.setBounds(133, 65, 70, 18);
			}
			{
				text_1 = new Text(group, SWT.BORDER);
				text_1.setBounds(209, 47, 70, 18);
			}
			{
				Button button = new Button(group, SWT.NONE);
				button.setBounds(340, 88, 72, 22);
				button.setText("计算");
				button.addSelectionListener(new SelectionAdapter() {    
                    public void widgetSelected(SelectionEvent e) {    
                        // 读取输入    
                        dishu = Double.parseDouble(text.getText());  
                        zhishu = Double.parseDouble(text_1.getText());  
                        double rate = getResult(zhishu, dishu);  
                        System.out.println(rate + "");  
                        resultShow.setText(rate + "");  
                    }    
                });
			}
		}
		{
			Group group = new Group(shell, SWT.NONE);
			group.setText("结果的倒数为");
			group.setBounds(10, 136, 422, 120);
			{
				resultShow = new Label(group, SWT.NONE);
				resultShow.setAlignment(SWT.CENTER);
				resultShow.setBounds(10, 49, 402, 61);
				//resultShow.setText("New Label");
			}
		}

	}
	
	public double getResult(double n, double j) {
		double result = 1;
		for(int i=1; i<=n; i++)
            result *= j;
		double export = 1/result;
		return export;
	}
}

运行结果

其实也就是写了以下一个方法:

public double getResult(double n, double j) {
		double result = 1;
		for(int i=1; i<=n; i++)
            result *= j;
		double export = 1/result;
		return export;
	}

j 作为底数, n 个 j 相乘,n 即指数,得出的结果保存到变量 result,再用1除以 result 即可。

随机文章推荐
网站分类


注:如需转载本文,请注明出处(原文链接),谢谢。更多精彩内容,请进入简明现代魔法首页。

进入新博客
喜欢本文,就分享它吧
给我留言
您的名字:
您的邮件:
您的网站:


 

copyright © 2009 简明现代魔法    学习、分享、进步

power by Gonn 感谢所有关心和支持本站的朋友们