十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
不仅可以加减还可以乘除,代码如下:
衡东ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
package 计算器;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
boolean flag = false;
JTextField jtf = new JTextField();
public static void main(String[] args) {
new Calculator();
}
public Calculator() {
setSize(300, 300);
setTitle("计算器");
Container c = getContentPane();
jtf.setHorizontalAlignment(JTextField.RIGHT);
JButton jb = new JButton("=");
jb.addActionListener(this);
JPanel jp = new JPanel();
c.add(jtf, BorderLayout.NORTH);
c.add(jp, BorderLayout.CENTER);
c.add(jb, BorderLayout.SOUTH);
jp.setLayout(new GridLayout(4, 4));
addButton(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void addButton(JPanel jp) {
JButton b = null;
b = new JButton("1");
b.addActionListener(this);
jp.add(b);
b = new JButton("2");
b.addActionListener(this);
jp.add(b);
b = new JButton("3");
b.addActionListener(this);
jp.add(b);
b = new JButton("+");
b.addActionListener(this);
jp.add(b);
b = new JButton("4");
b.addActionListener(this);
jp.add(b);
b = new JButton("5");
b.addActionListener(this);
jp.add(b);
b = new JButton("6");
b.addActionListener(this);
jp.add(b);
b = new JButton("-");
b.addActionListener(this);
jp.add(b);
b = new JButton("7");
b.addActionListener(this);
jp.add(b);
b = new JButton("8");
b.addActionListener(this);
jp.add(b);
b = new JButton("9");
b.addActionListener(this);
jp.add(b);
b = new JButton("*");
b.addActionListener(this);
jp.add(b);
b = new JButton("0");
b.addActionListener(this);
jp.add(b);
b = new JButton(".");
b.addActionListener(this);
jp.add(b);
b = new JButton("C");
b.addActionListener(this);
jp.add(b);
b = new JButton("/");
b.addActionListener(this);
jp.add(b);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "=") {
if (jtf.getText().matches("\\d+\\.?\\d*[\\+\\-\\*\\/]\\d+\\.?\\d*")) {
String s = jtf.getText();
Pattern p = Pattern.compile("\\d+\\.?\\d*");
Matcher m = p.matcher(s);
m.find();
double firstNum = Double.parseDouble(m.group());
m.find();
double secondNum = Double.parseDouble(m.group());
if (jtf.getText().matches("\\d+\\.?\\d*\\+\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum + secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\-\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum - secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\*\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum * secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\/\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum / secondNum));
}
} else {
JOptionPane.showMessageDialog(null, "请输入正确表达式!");
}
flag = true;
} else if (e.getActionCommand() == "C") {
jtf.setText("");
} else {
if (flag) {
if (e.getActionCommand() != "+" e.getActionCommand() != "-"
e.getActionCommand() != "*"
e.getActionCommand() != "/") {
//System.out.println(e.getActionCommand() != "+");
jtf.setText("");
}
flag = false;
}
jtf.setText(jtf.getText() + e.getActionCommand());
}
}
}
import java.util.Scanner;
public class P {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
P prm=new P();
int a,b;
System.out.println("请输入两个整数(以空格分隔):");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("两数相加后等于:"+prm.add(a,b));
System.out.println("两数相减后等于:"+prm.sub(a,b));
System.out.println("两数相除后等于:"+prm.divide(b,a));
sc.close();
}
int add(int a,int b) {
return a+b;
}
int sub(int a,int b) {
if(ab)
return a-b;
else
return b-a;
}
int divide(int a,int b) {
if(0==b)
System.out.println("错误");
if(0==a)
System.out.println("除数不能为0");
return b/a;
}
}
附上代码:
Date date = new Date("2014/1/10 18:20");
Date date2 = new Date("2014/1/11 3:5");
long temp = date2.getTime() - date.getTime(); //相差毫秒数
long hours = temp / 1000 / 3600; //相差小时数
long temp2 = temp % (1000 * 3600);
long mins = temp2 / 1000 / 60; //相差分钟数
System.out.println("date2 与 date 相差" + hours + "小时"+ mins + "分钟");
****************************************希望能够帮助到你!************************************************
如果我的回答对你有帮助,
别忘了点击我的回答下方【选为满意答案】按钮。
谢谢!
在a.jsp中写如下代码:
请输入
form action="b.jsp" method="post"
上课作业比率:input type="text" name="zy" / br /
上课实训:input type="text" name="sksx" / br /
平时考勤: input type="text" name="kq" / br /
平时实训:input type="text" name="pssx" / br /
/form
b.jsp中..
%String s = request.getParameter("zy")
//.......
%
懒得写了太麻烦@_@
java编程实现分数的加减乘除运算的步骤如下:
1、打开eclipse,创建一个Java工程,在此工程里新建一个类;
2、在新建的类中,添加4个运算类;
3、在主方法中调用对应的方法即可完成分数的加减乘除运算了。
具体实现代码如下:
public class Demo {
public static void main(String[] args) {
System.out.println(jia(1, 2));
System.out.println(jian(1, 2));
System.out.println(cheng(1, 2));
System.out.println(chu(1, 2));
}
//加法运算
private static float jia(float x,float y) {
return x + y;
}
//减法运算
private static float jian(float x,float y) {
return x - y;
}
//乘法运算
private static float cheng(float x,float y) {
return x * y;
}
//除法运算
private static float chu(float x,float y) {
return x / y;
}
}