二维数组乘法
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);
}
}