四则运算

package p;

import java.awt.Button;
import java.awt.Choice;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Test
{
	
    static  TextField tf1;
    static  TextField tf2;
    static  TextField tf3;
    static  TextField tf4;
	static  Choice choice;
	static Label label;
	static Label label2;
    int right; 
    int fault;
   
  
		public static void main(String[] args)
	{
		Frame frame = new Frame("四则运算");
		frame.setSize(300, 200);
		frame.setLocation(300, 200);
		frame.addWindowListener(new Listener1());
		tf1 = new TextField(8);
		choice = new Choice();
		choice.addItem("+");
		choice.addItem("-");
		choice.addItem("*");
		choice.addItem("/");
		tf2 = new TextField(8);
		 label = new Label("=");
		tf3 = new TextField(8);
		Button button = new Button("计算");
		label2 = new Label("正确率");
        tf4 = new TextField(8);
		frame.add(tf1);
		frame.add(choice);
		frame.add(tf2);
		frame.add(label);
		frame.add(tf3);
		frame.add(button);
	    frame.add(label2);
	    frame.add(tf4);
		frame.setLayout(new FlowLayout());
		button.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				String s1 = tf1.getText();
				String s2 = tf2.getText();
				
				String ch = choice.getSelectedItem();
				double d1 = Double.parseDouble(s1);
				double d2 = Double.parseDouble(s2);
				double d = 0;
				
				if (ch.equals("+"))
				{
					d = d1 + d2;
				} else if (ch.equals("-"))
				{
					d = d1 - d2;
				} else if (ch.equals("*"))
				{
					d = d1 * d2;
				} else
				{
					d = d1 / d2;
				}
				tf3.setText(d + "");
				
			}
			  @SuppressWarnings("unused")
			int Add(int right,int fault)
			    {
			    	int answer = 0;
			    	double d,d1 = 0,d2=0;
			    	d=d1+d2;
			    
			    	scanf("d%", answer);
			    	if(d==answer)
			    	{
			    		right++;
			    		zql(1);
			        }
			    	else
			    	{
			    		fault++;
			    		zql(0);
			    	}
			    	return 0;
			    	
			    }
	   });
		frame.setVisible(true);
	}
		
		
	static void zql(int rightOrfault)
	{
		 if (rightOrfault==1)
		    System.out.print("回答正确");
		else if(rightOrfault==0)
		    System.out.print("回答错误");
		    
	}
	 int Num(int max,int min) 
	   {
		int randNum;
		randNum=rand()%(max-min+1)+min;
		return randNum;
	    }
  
	private static void scanf(String string, int answer) {
		// TODO Auto-generated method stub
		
	}


	private int rand() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
}

想要计算出结果需要做一个监听

package p;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Listener1 extends WindowAdapter
{

	@Override
	public void windowClosing(WindowEvent arg0)
	{
		System.exit(0);
	}

}

 

刚刚自己翻了一点错误  只能把需求分析都写到后面了

需求分析 : 做一个10以内数字的加减乘除 计算出结果并统计正确率。

设计思路:首先要设计出一个大的架构,然后再去构划细节。

总结:因为能力有限所以很多都没有能够实现出来  统计正确率,产生随机数都没能实现。

注:因为代码这两天Java老师有讲一个类似的例题  所以有可能会有和别人相似的地方,还请老师见谅   毕竟能力太有限了。

原文地址:https://www.cnblogs.com/L0610/p/4848776.html