java9次试验报告+8次作业

目录

 

9次试验报告

1变量交换

2输出图形

3 二维数组的转置

4文本字符统计

5温度单位转换

6策略模式的应用

7内部类静态的应用

 8删除tmp文件

9用户注册页面的实现

8次作业

1五角星

2对称对称五角星

3日历

4首字母大写

5LowCaseLetter

6普通计算器

7计算器加抛出异常

8计算器GUI


9次试验报告

1变量交换

public class job1 {
    public static void main(String[] args) {
        int a = 13,b=14,t;
        t = a;
        a = b;
        b = t;
        System.out.println("a="+a+","+"b="+b);
    }
}

2输出图形

import java.util.Scanner;

public class job2
{
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n;
        n = cin.nextInt();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n+(i-1);j++){
                if(j==n-i+1||j==n+i-1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
        for(int i=n-1;i>=1;i--){
            for(int j=1;j<=n+(i-1);j++){
                if(j==n-i+1||j==n+i-1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

3 二维数组的转置

public class job3 {
    public static void main(String[] args) {
        int[][] a = new int[4][2];
        int[][] b = new int[2][4];
        int k=0;
        for(int i=0;i<a.length;i++){
            for(int j=0;j<a[i].length;j++){
                a[i][j] =k++;
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("转置后:");
        for(int i=0;i<b.length;i++){
            for(int j=0;j<b[i].length;j++){
                b[i][j] = a[j][i];
                System.out.print(b[i][j]+" ");
            }
            System.out.println();
        }
    }
}

4文本字符统计

public class job4 {
    public static void main(String[] args) {
        String s = "没有如果llpp可能";
        String regex = "[\\u4e00-\\u9fa5]";
        int ans = 0;
        for (int i = 0; i < s.length();i++){
            if((""+s.charAt(i)).matches(regex))
                ans++;
        }
        System.out.println("该字符串中有"+ans+"个汉字");
    }
}

5温度单位转换

import java.util.Scanner;

public class job5 {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        System.out.println("请输入华氏度:");
        double F = cin.nextDouble();
        job5 jj = new job5();
        double C = jj.change(F);
        System.out.println("该摄氏度为:"+C);
    }
    double change(double F){
        double C = (F - 32)*5/9;
        return C;
    }
}

6策略模式的应用

public class job6 {
    public static void main(String[] args) {
        Context con;
        System.out.println("执行任务1");
        con = new Context(new kk1());
        con.ido();
        System.out.println("执行任务2");
        con = new Context(new kk2());
        con.ido();
    }
}
class Context{
    private inter p;
    public Context(inter p){
        this.p = p;
    }
    public void  ido(){
        p.work();
    }
}
interface inter{
    public void work();
}
class kk1 implements inter{
    public void work(){
        System.out.println("任务1开始执行");
    }
}
class kk2 implements inter{
    public void work(){
        System.out.println("任务2开始执行");
    }
}

7内部类静态的应用

public class job7 {
    public static class HH{
        private static int mx;
        private static int location;
        public HH(int mx,int location){
            this.mx = mx;
            this.location = location;
        }
        public static int getMx(){
            return mx;
        }
        public static int getLocation(){
            return location;
        }
        public static void work(int[] a){
            mx = Integer.MIN_VALUE;
            location = 0;
            for(int i=0;i<a.length;i++){
                if(mx<a[i]){
                    mx = a[i];
                    location = i+1;
                }
            }
        }
    }
    public static void main(String[] args) {
        int [] a = new int[100];
        for(int i=0;i<10;i++){
            a[i] = i;
        }
        HH.work(a);
        int mx = HH.getMx();
        int location = HH.getLocation();
        System.out.println("最大的数是"+mx);
        System.out.println("最大的数在第"+location+"个位置");
    }
}

 8删除tmp文件

import java.io.File;

public class job8 {
    private  static  void delTmpFile(File tempFile){
        String name = tempFile.getName();
        if(name.endsWith(".tmp")||name.endsWith(".TMP")){
            tempFile.delete();
            System.out.println("删除tmp文件");
        }
    }
    public static void delTmpFiles(File root){
        if(root.isDirectory()){
            File[] files = root.listFiles();
            for(File file : files){
                if(file.isDirectory()){
                    System.out.println("进入文件夹");
                    delTmpFile(file);
                }
                if(file.isFile()){
                    delTmpFile(file);
                }
            }
        }
        if(root.isFile()){
            delTmpFile(root);
        }
    }
    public static void main(String[] args) {
        File root =new File("F:\\tmp文件夹");
        delTmpFiles(root);
    }
}

9用户注册页面的实现

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class job9 extends JFrame {
    private JPanel p;
    private JLabel lbName,lblPwd,lbRePwd, lbAddress,lbIMsg;

    //声明文本框
    private JTextField txtName;
    //声明两个密码框
    private JPasswordField txtPwd,txtRePwd;
    //声明一个文本域
    private JTextArea txtAddress;
    private JButton btnReg,btnCancel;
    public job9() {
        super("注册新用户");
        //创建面板,面板布局为NULL
        p=new JPanel(null);
        //实例化5个标签
        lbName =new JLabel("用户名");
        lbName.setFont(new Font("楷体",Font.BOLD,15));
        lblPwd =new JLabel("密   码");
        lblPwd.setFont(new Font("楷体",Font.BOLD,15));
        lbRePwd =new JLabel("确认密码");
        lbRePwd.setFont(new Font("楷体",Font.BOLD,15));
        lbAddress =new JLabel("地址");
        lbAddress.setFont(new Font("楷体",Font.BOLD,15));
        //显示信息的标签
        lbIMsg = new JLabel();
        //设置标签的文字是红色
        lbIMsg.setForeground(Color.RED);
        //创建一个长度为20 的文本框
        txtName =new JTextField(20);
        //创建两个密码框长度为20
        txtPwd=new JPasswordField(20);
        txtRePwd = new JPasswordField(20);
        //设置密码框显示的字符为*
        txtPwd.setEchoChar('*');
        txtRePwd.setEchoChar('*');
        //创建一个文本域  20,2
        txtAddress = new JTextArea(20,2);
        //创建两个按钮
        btnReg =new JButton("注册");
        btnCancel = new JButton("清空");
        btnReg.setFont(new Font("楷体",Font.BOLD,15));
        btnCancel.setFont(new Font("楷体",Font.BOLD,15));
        //注册监听
        btnReg.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //设置信息标签为空 清楚原来的历史信息
                lbIMsg.setText("");
                //获取用户输入的用户名
                String strName = txtName.getText();
                if (strName==null||strName.equals("")) {

                    lbIMsg.setText("用户名不能为空");
                    return;
                }
                //获取用户名密码
                String strPwd = new String(txtPwd.getPassword());
                if (strPwd==null||strPwd.equals("")) {

                    lbIMsg.setText("密码不能为空");
                    return;
                }
                String strRePwd = new String(txtRePwd.getPassword());
                if (strRePwd==null||strRePwd.equals("")) {

                    lbIMsg.setText("确认密码不能为空");
                    return;
                }

                //判断确认密码是否跟密码相同
                if (!strRePwd.equals(strPwd)) {

                    lbIMsg.setText("确认密码跟密码不同");
                    return;
                }
                //获取用户地址
                String strAddress = new String(txtAddress.getText());
                if (strAddress==null||strAddress.equals("")) {

                    lbIMsg.setText("地址不能为空");
                    return;
                }
                lbIMsg.setText("注册成功");
            }
        });

        //取消按钮的事件处理
        btnCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //清空所有文本信息
                txtName.setText("");
                txtPwd.setText("");
                txtRePwd.setText("");
                txtAddress.setText("");
                //设置信息标签为空
                lbIMsg.setText("");
            }
        });
        lbName.setBounds(30,20,80,40);
        txtName.setBounds(95,25,120,25);
        lblPwd.setBounds(30,60,60,25);
        txtPwd.setBounds(95,60,120,25);
        lbRePwd.setBounds(30,80,80,40);
        txtRePwd.setBounds(95,90,120,25);
        lbAddress .setBounds(30,120,60,25);
        txtAddress.setBounds(95,120,120,25);
        lbIMsg.setBounds(60,185,180,25);
        btnReg.setBounds(50,200,80,40);
        btnCancel.setBounds(125,200,80,40);
        //添加所有组件
        p.add(lbName);
        p.add(txtName);
        p.add(txtPwd);
        p.add(lblPwd);
        p.add(txtRePwd);
        p.add(lbRePwd);
        p.add(txtAddress);
        p.add(lbAddress);
        p.add(lbIMsg);
        p.add(btnReg);
        p.add(btnCancel);
        this.add(p);
        this.setSize(350,300);
        this.setLocation(200, 100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new job9();
    }

}

8次作业

1五角星

import java.util.Scanner;

public class Test {
	int k = 0, p = 0, pp = 0;

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		String s;
		s = cin.nextLine();
		int touHigh = 6;
		int jianHigh = 20;
		int kuang = 50;
		for (int i = 1; i <= touHigh + jianHigh; i++) {
			for (int j = 1; j <= kuang; j++) {
				// 上三角
				if (i <= touHigh) {
					if (j >= (kuang / 2 + 1) + 1 - i && j <= (kuang / 2 + 1) - 1 + i) {
						System.out.print(s);
					} else {
						System.out.print(" ");
					}
				}
				// 上三角一下部分
				if (i > touHigh && i <= jianHigh) {
					if (j >= (kuang / 2 + 1) + 2 - i && j <= kuang - 3 * (i - touHigh)) {
						System.out.print(s);
					} else if (j < (kuang / 2 + 1) - 3 + i && j >= 3 * (i - touHigh)) {
						System.out.print(s);
					} else {
						System.out.print(" ");
					}
				}
			}
			System.out.println("");
		}
	
	}

}

2对称对称五角星

import java.util.Scanner;

public class Test {
	int k = 0, p = 0, pp = 0;

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		String s;
		s = cin.nextLine();
		int touHigh = 6;
		int jianHigh = 20;
		int kuang = 50;
		for (int i = 1; i <= touHigh + jianHigh; i++) {
			for (int j = 1; j <= kuang; j++) {
				// 上三角
				if (i <= touHigh) {
					if (j >= (kuang / 2 + 1) + 1 - i && j <= (kuang / 2 + 1) - 1 + i) {
						System.out.print(s);
					} else {
						System.out.print(" ");
					}
				}
				// 上三角一下部分
				if (i > touHigh && i <= jianHigh) {
					if (j >= (kuang / 2 + 1) + 2 - i && j <= kuang - 3 * (i - touHigh)) {
						System.out.print(s);
					} else if (j < (kuang / 2 + 1) - 3 + i && j >= 3 * (i - touHigh)) {
						System.out.print(s);
					} else {
						System.out.print(" ");
					}
				}
			}
			System.out.println("");
		}
		for (int i = touHigh + jianHigh; i >= 1; i--) {
			for (int j = 1; j <= kuang; j++) {
				// 上三角
				if (i <= touHigh) {
					if (j >= (kuang / 2 + 1) + 1 - i && j <= (kuang / 2 + 1) - 1 + i) {
						System.out.print(s);
					} else {
						System.out.print(" ");
					}
				}
				// 上三角一下部分
				if (i > touHigh && i <= jianHigh) {
					if (j >= (kuang / 2 + 1) + 2 - i && j <= kuang - 3 * (i - touHigh)) {
						System.out.print(s);
					} else if (j < (kuang / 2 + 1) - 3 + i && j >= 3 * (i - touHigh)) {
						System.out.print(s);
					} else {
						System.out.print(" ");
					}
				}
			}
			System.out.println("");
		}
	}

}

3日历

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

/**
 * 测试编写一个简单的日历
 * 
 * @author Administrator
 *
 */
public class rili {

	public static void main(String[] args) {
		myCalendar();
	}

	public static void myCalendar() {
		
		int maxDay = 0;	
		int firstDay = 0;	
		int currentDay = 0;		
		
		System.out.println("请输入一个日期:格式为:2030-03-10");	
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine();	//键盘输入日期格式的字符串

		try {
			
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = format.parse(str);	//将字符串转化为指定的日期格式
			
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(date);	//将日期转化为日历

			maxDay = calendar.getActualMaximum(Calendar.DATE);	//当前日期中当前月对应的最大天数

			currentDay = calendar.get(Calendar.DATE);	//当前日期中的当前天
			calendar.set(Calendar.DATE, 1); // 设置为当前月的第一天

			firstDay = calendar.get(Calendar.DAY_OF_WEEK);	//当前日期中当前月第一天对应的星期数

		} catch (ParseException e) {
			e.printStackTrace();
		}

		System.out.println("日\t一\t二\t三\t四\t五\t六\n");
		for (int j = 1; j < firstDay; j++) {
			System.out.print("\t");
		}
		for (int i = 1; i <= maxDay; i++) {
			if (i == currentDay) {
				System.out.print("#");
			}
			System.out.print(i + "\t");
			if ((i - (8 - firstDay)) % 7 == 0) {
				System.out.println("\n");
			}
		}
	}
}

4首字母大写

import java.util.Scanner;

public class daxiaoxie {
	public static void main(String[] args) {
		String s;
		Scanner cin = new Scanner(System.in);
		s = cin.nextLine();
		int f=0;
		for(int i=0;i<s.length();i++){
			if(i==0&&s.charAt(i)>='a'&&s.charAt(i)<='z'){
				System.out.print((char)(s.charAt(i)-32));
				continue;
			}
			if(s.charAt(i-1)==' '&&s.charAt(i)!=' '){
				if(s.charAt(i)>='a'&&s.charAt(i)<='z'){
					System.out.print((char)(s.charAt(i)-32));
					continue;
				}
			}
			System.out.print(s.charAt(i));
			
		}
	}
}

5LowCaseLetter

public class LetterOutVariation {
	public static void main(String[] args) {
		ziLowCaseLetter ss = new ziLowCaseLetter();
		ss.outLetter();
		System.out.println();
		ss.OutUpcaseletter();
	}
}
public class ziLowCaseLetter extends LowCsaeLetter{
	public void OutUpcaseletter(){
		char s = 'A';
		for(int i=0;i<26;i++) {
			System.out.print((char)(s+i)+" ");
		}
	}
}
public class LowCsaeLetter {
	public void outLetter() {
		char s = 'a';
		for(int i=0;i<26;i++) {
			System.out.print((char)(s+i)+" ");
		}
	}

}

6普通计算器

import java.util.Scanner;

public class Jisuanqi {
    public static void main(String[] args) {
        double a,b;
        char x;
        System.out.println("切记输入的数字与符号要留出空格要不然会报错的");
        while(true) {
            Scanner cin = new Scanner(System.in);
            a = cin.nextDouble();
            x = cin.next().charAt(0);
            b = cin.nextDouble();
            Usefuhao u = new Usefuhao();
            u.start(x, a, b);
        }
    }
}
 
class Usefuhao {
    public void start(char x,double a,double b){
        switch (x){
            case '+':
                jia j = new jia();
                System.out.println("a + b = "+j.yunxin(a,b));
                break;
            case '-':
                jian jan = new jian();
                jan.yunxin(a,b);
                System.out.println("a - b = "+jan.yunxin(a,b));
                break;
            case '*':
                chen c = new chen();
                System.out.println("a * b = "+c.yunxin(a,b));
                break;
            case '/':
                chu cu = new chu();
                System.out.println("a / b = "+cu.yunxin(a,b));
                break;
                default:
                    System.out.println("您输入的符号不在我的计算范围之内");
                    break;
        }
    }
}
 
 
abstract interface fuhao {
    public abstract double yunxin(double n,double b);
}
 
class jia implements fuhao {
    public double yunxin(double a,double b) {
        return a+b;
    }
}
class jian implements fuhao{
    public double yunxin(double a,double b){
        return  a-b;
    }
}
class chen implements fuhao{
    public double yunxin(double a,double b){
        return a*b;
    }
}
class chu implements fuhao{
    public double yunxin(double a,double b){
        return a/b;
    }
}

7计算器加抛出异常

import java.util.Scanner;

public class Jisuanqi {
    public static void main(String[] args) {
        double a,b;
        char x;
        System.out.println("切记输入的数字与符号要留出空格要不然会报错的");
        System.out.println("这次加的抛出异常是分母为0是会出现异常");
        while(true) {
            Scanner cin = new Scanner(System.in);
            a = cin.nextDouble();
            x = cin.next().charAt(0);
            b = cin.nextDouble();
            Usefuhao u = new Usefuhao();
            u.start(x, a, b);
        }
    }
}

class Usefuhao {
    public void start(char x,double a,double b){
        switch (x){
            case '+':
                jia j = new jia();
                System.out.println("a + b = "+j.yunxin(a,b));
                break;
            case '-':
                jian jan = new jian();
                jan.yunxin(a,b);
                System.out.println("a - b = "+jan.yunxin(a,b));
                break;
            case '*':
                chen c = new chen();
                System.out.println("a * b = "+c.yunxin(a,b));
                break;
            case '/':
                chu cu = new chu();
                try {
                    System.out.println("a / b = "+cu.yunxin(a,b));
                }catch (YiChangException e){
                    e.printStackTrace();
                }
                break;
                default:
                    System.out.println("您输入的符号不在我的计算范围之内");
                    break;
        }
    }
}

class YiChangException extends Exception{
    public YiChangException() {
    }
    public YiChangException(String message){
        super(message);
    }
}

abstract interface fuhao {
    public abstract double yunxin(double n,double b) throws YiChangException;
}

class jia implements fuhao {
    public double yunxin(double a,double b) {
        return a+b;
    }
}

class jian implements fuhao{
    public double yunxin(double a,double b){
        return  a-b;
    }
}

class chen implements fuhao{
    public double yunxin(double a,double b){
        return a*b;
    }
}

class chu implements fuhao{
    public double yunxin(double a,double b) throws YiChangException {
        if(b==0){
            throw new YiChangException("您输入的分母为0,不能进行除法运算");
        }else
        return a/b;
    }
}

8计算器GUI


/*
 *@小姚
 *写的不好,很多bug,很多限制,才学的,所以不懂,加了特别多的注释
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class Calculator extends JFrame {
	private final String[] FH = { "+", "-", "*", "/", ".", "=", "C", "sin", "cos", "tan", "^", "log","sqrt","<-" };
	CharSequence FHH[] = { "+", "-", "*", "/" };
	private JPanel btjp = new JPanel();// 创建面板容器
	private JTextField jt = new JTextField();// 创建文本框

	public Calculator(String title) {
		super(title);
		btjp.setLayout(new GridLayout(6, 4, 20, 5));// 定义面板,并设置为网格布局,5行4列,组件水平距离为20,垂直距离为5
		JButton[] JBtn_1 = new JButton[10];
		JButton[] JBtn_2 = new JButton[FH.length];
		for (int i = 0; i <= 9; i++)
			JBtn_1[i] = new JButton(String.valueOf(i));// 数字按键
		for (int i = 0; i <= 9; i++) {
			JBtn_1[i].setFont(new Font("宋体", Font.BOLD, 30));// 给数字按键调整字体大小
		}
		for (int i = 0; i < FH.length; i++)
			JBtn_2[i] = new JButton(String.valueOf(FH[i]));// 符号按键
		for (int i = 0; i < FH.length; i++) {
			//System.out.println(FH[i]);
			JBtn_2[i].setFont(new Font("宋体", Font.BOLD, 30));// 给符号按键调整字体大小
		}
		// 把这些按钮按照事先排好的顺序,按在面板容器
		btjp.add(JBtn_1[1]);
		btjp.add(JBtn_1[2]);
		btjp.add(JBtn_1[3]);
		btjp.add(JBtn_2[0]);
		btjp.add(JBtn_1[4]);
		btjp.add(JBtn_1[5]);
		btjp.add(JBtn_1[6]);
		btjp.add(JBtn_2[1]);
		btjp.add(JBtn_1[7]);
		btjp.add(JBtn_1[8]);
		btjp.add(JBtn_1[9]);
		btjp.add(JBtn_2[2]);
		btjp.add(JBtn_2[4]);
		btjp.add(JBtn_1[0]);
		btjp.add(JBtn_2[6]);
		btjp.add(JBtn_2[3]);
		for (int i = 5; i < FH.length; i++)
			btjp.add(JBtn_2[i]);
//		btjp.add(JBtn_2[5]);
//		btjp.add(JBtn_2[6]);
//		btjp.add(JBtn_2[7]);
//		btjp.add(JBtn_2[8]);
		// btjp.add(JBtn_3[0]);
		jt.setHorizontalAlignment(JTextField.RIGHT);// 文本框内容右侧对齐
		jt.setFont(new Font("宋体", Font.BOLD, 30));
		// JBtn_1[2].setFont(new Font("宋体",Font.BOLD,30));
		jt.setPreferredSize(new Dimension(200, 100));// 文本框为宽200,长100
		// 以下this是指主窗口JFrame
		this.add(jt, BorderLayout.NORTH);// 将文本框放在靠北的地方
		this.add(btjp, BorderLayout.CENTER);// 将面板容器放在中央
		this.setSize(600, 600);// 设置主窗口的大小
		this.setResizable(true);// 表示生成的窗体能够是否有用户改变大小,false表示不能,true代表能
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 使用 System exit 方法退出应用程序。
		this.setVisible(true);
		// etVisible(true)并不是告诉JVM让该控件可见,而是在内部调用repaint方法把各个控件画出来进行显示。
		// 如果在控件还没完全添加完其他控件就setVisible(true)那么在方法后面添加的控件都不能显示
		for (int i = 0; i <= 9; i++)
			JBtn_1[i].addActionListener(new number());// 为事件源增加监听器
		for (int i = 0; i < FH.length; i++)
			JBtn_2[i].addActionListener(new number());
	}

	private class number extends WindowAdapter implements ActionListener {// 重写监听器里面的内容
		public void actionPerformed(ActionEvent e) {// 这个是ActionListener这个接口里面的方法,监听器里的方法
			// TODO Auto-generated method stub
			// getActionCommand()依赖于按钮上的字符串
//			getSource得到的组件的名称,而getActionCommand得到的是标签。
//			如:Button bt=new Button("buttons");
//			用getSource得到的是bt 而用getActionCommand得到的是:buttons
			if (!e.getActionCommand().equals("=") && !e.getActionCommand().equals("C")&&!e.getActionCommand().equals("<-")) {
				jt.setText(jt.getText() + e.getActionCommand());// 在文本框中显示
			} //
			else if(e.getActionCommand().equals("<-")) {
				String Formula = jt.getText();
				int len = Formula.length();
				if(len!=0) {
				String s = String.valueOf(Formula.substring(0,len-1));
				jt.setText(s);
				}
			}
			else if (e.getActionCommand().equals("=")) {
				String Formula = jt.getText();
				if (Formula.contains(FHH[0])) {// java中contains方法是判断是否存在包含关系的,
					// 1.java中String元素下标从0开始,substring(a)是从第a个字符开始截取,包含第a个字符。可以看成数学中的[ ),表示左闭右开区间
					// 2.substring(a, b)表示截取下标从a开始到b结束的字符,包含第a个字符但是不包含第b个字符,可以看成[a,b)。
					// Double.parseDouble方法是把数字类型的字符串,转换成double类型
					// Double.valueOf方法是把数字类型的字符串,转换成Double类型
					double number_1 = Double.parseDouble(Formula.substring(0, Formula.indexOf('+')));
					double number_2 = Double.parseDouble(Formula.substring(Formula.indexOf('+') + 1));
					jt.setText(String.valueOf(number_1 + number_2));
				}
				else if (Formula.contains(FHH[2])) {
					double number_1 = Double.parseDouble(Formula.substring(0, Formula.indexOf('*')));
					double number_2 = Double.parseDouble(Formula.substring(Formula.indexOf('*') + 1));
					jt.setText(String.valueOf(number_1 * number_2));
				}
				else if (Formula.contains(FHH[3])) {
					double number_1 = Double.parseDouble(Formula.substring(0, Formula.indexOf('/')));
					double number_2 = Double.parseDouble(Formula.substring(Formula.indexOf('/') + 1));
					try {
						check(number_1, number_2);
					} catch (YiChangException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					jt.setText(String.valueOf(number_1 / number_2));
				}
				else if (Formula.contains("sin")) {
					double number = Double.parseDouble(Formula.substring(Formula.indexOf('n') + 1));
					double a = Math.toRadians(number);
					jt.setText(String.valueOf(Math.sin(a)));
				}
				else if (Formula.contains("cos")) {
					double number = Double.parseDouble(Formula.substring(Formula.indexOf('s') + 1));
					double a = Math.toRadians(number);
					jt.setText(String.valueOf(Math.cos(a)));
				}
				else if (Formula.contains("tan")) {
					double number = Double.parseDouble(Formula.substring(Formula.indexOf('n') + 1));
					double a = Math.toRadians(number);
					jt.setText(String.valueOf(Math.tan(a)));
				}
				else if (Formula.contains("^")) {
					double number_1 = Double.parseDouble(Formula.substring(0, Formula.indexOf('^')));
					double number_2 = Double.parseDouble(Formula.substring(Formula.indexOf('^') + 1));
					jt.setText(String.valueOf(Math.pow(number_1, number_2)));
				}
				else if (Formula.contains("log")) {
					double number = Double.parseDouble(Formula.substring(Formula.indexOf('g') + 1));
					jt.setText(String.valueOf(Math.log(number)));
				}
				else if (Formula.contains("sqrt")) {
					double number = Double.parseDouble(Formula.substring(Formula.indexOf('t') + 1));
					if(number<0)System.out.println("skadh");
					try {
						check2(number);
					} catch (YiChangException2 e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					jt.setText(String.valueOf(Math.sqrt(number)));
				}
				else if (Formula.contains(FHH[1])) {
					double number_1 = Double.parseDouble(Formula.substring(0, Formula.indexOf('-')));
					double number_2 = Double.parseDouble(Formula.substring(Formula.indexOf('-') + 1));
					jt.setText(String.valueOf(number_1 - number_2));
				}
			} else {
				jt.setText(null);
			}
		}
	}

	void check(double x, double y) throws YiChangException {
		if (y == 0) {
			throw new YiChangException("您输入的分母为0,不能进行除法运算");
		} else
			return;
	}
	void check2(double x) throws YiChangException2 {
		if (x<0) {
			throw new YiChangException2("您输入的数为负数,不能进行开方运算");
		} else
			return;
	}

	public static void main(String[] args) {
		new Calculator("炒鸡简单的计算器");
	}
}
class YiChangException2 extends Exception {
	public YiChangException2() {
	}

	public YiChangException2(String message) {
		super(message);
	}
}
class YiChangException extends Exception {
	public YiChangException() {
	}

	public YiChangException(String message) {
		super(message);
	}
}

 

  • 8
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值