JAVA初学三记

二维数组乘法


public class pratice2 {
    public static void multi(int a[][], int b[][])
    {
        int c[][] = new int [a.length][b[0].length];
        int i = 0, j = 0, k , cnt = 0;
        while(j < c[i].length)
        {
            for(k = 0 ; k < c.length ; k++)
            {
                c[i][j] += a[i][k] * b[k][j];
            }
            if(++j == c[i].length)//判满条件
            {
                j = 0;
                i++;
                if(++cnt == c.length)
                {
                    cnt = 0;
                    break;
                }
            }
        }
        for(i = 0; i < c.length; i++)
        {
            for(int x : c[i])
            {
                System.out.print(x + " ");
            }
            System.out.println();
        }
    }
    public static void main(String[] args)
    {
        int a[][] = {{1,2},{3,4}};
        int b[][] = {{4,3},{2,1}};
//		int a[][] = {{1,2,3},{4,5,6},{7,8,0}};
//		int b[][] = {{1,2,1},{1,1,2},{2,1,1}};
//		int a[][] = {{2,1},{4,3}};
//		int b[][] = {{2},{1}};
        System.out.println("乘法:");
        multi(a,b);
    }
}

精确至小数点后三位

public static String precision(double sq)
	{
		int get;
		sq += 0.0005;
		get = (int)(sq*1000);
		sq = (double)get/1000;
		String result = String.valueOf(sq);
		if(get%1000 == 0)
		{
			result += "00";
		}
		else if(get%10 == 0)
		{
			result += "0";
			if(get%100 == 0)
			{
				result += "0";			
			}
		}
		return result;
	}
public static String precision(double sq)
	{
		int get;
		sq += 0.0005;
		get = (int)(sq*1000);
		sq = (double)get/1000;
		String result = String.valueOf(sq);
		if(get%1000 == 0)
		{
			result += "00";
		}
		else if(get%10 == 0)
		{
			result += "0";
			if(get%100 == 0)
			{
				result += "0";			
			}
		}
		return result;
	}
DecimalFormat df = new DecimalFormat("0.000");
System.out.println(df.format(1.2345));


String.format("%.3f",12.3456);


GUI_简易的计算器按键布局

import javax.swing.*;
import java.awt.*;
//import java.awt.event.*;
//import java.math.*;
//import static java.math.BigDecimal.*;
 
public class SimpleCalculator extends  JFrame{
    JButton[] buttons;
    String[] s = {"1","2","3","4","5","6","7","8","9","0",".","+","-","*","/","="};
    public SimpleCalculator(String title)
    {
        super(title);
        setLayout(new GridLayout(4,4));
        buttons = new JButton[s.length];
        for (int i = 0; i < s.length; i++)
        {
            buttons[i] = new JButton(s[i]);
            add(buttons[i]);
            //buttons[i].addActionListener(new ButtonHandler(s[i]));
        }
    }
}
    public static void main(String[] args) {
        SimpleCalculator c = new SimpleCalculator("简易计算器界面");
        c.setSize(250,150);
        c.setVisible(true);
        c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值