四则运算代码java 四则运算java编程( 三 )


System.out.println(a+"和"+b+"的和是:"+he);
System.out.println(a+"和"+b+"的差是:"+cha);
System.out.println(a+"和"+b+"的积是:"+ji);
System.out.println(a+"和"+b+"的商是:"+shang);
}
}
java简单的四则运算.public class Arithmetic {
public static void Ari(){
Scanner scan = new Scanner(System.in);
StringBuffer buffer = new StringBuffer();
DecimalFormat dec = new DecimalFormat("0.00");//格式化结果保留两位小数
String all = "";//所有的计算表达式连在一起
System.out.println("请输入表达式的个数,只能为正整数");
int n = scan.nextInt();
System.out.println("请依次输入要计算的表达式");
for(int i=0;in+1;i++){
buffer = buffer.append(scan.nextLine()+",");
}
all = buffer.substring(0, buffer.lastIndexOf(","));
String allAri[] = all.split(",");
String ari = "";//不同的算法表达式
float add;//加法的计算结果
float subtract;//减肥的计算结果
float multi;//乘法的计算结果
float divison;//除法的计算结果
int model;//模运算的计算结果
for(int j=0;jallAri.length;j++){
ari = allAri[j];
if(ari.contains("+")){
String tempAry[] = ari.split("[+]");
add = Float.valueOf(tempAry[0])+Float.valueOf(tempAry[1]);
System.out.println(dec.format(add));
}else if(ari.contains("-")){
String tempAry[] = ari.split("[-]");
subtract = Float.valueOf(tempAry[0])-Float.valueOf(tempAry[1]);
System.out.println(dec.format(subtract));
}else if(ari.contains("*")){
String tempAry[] = ari.split("[*]");
multi = Float.valueOf(tempAry[0])*Float.valueOf(tempAry[1]);
System.out.println(dec.format(multi));
}else if(ari.contains("/")){
String tempAry[] = ari.split("[/]");
divison = Float.valueOf(tempAry[0])/Float.valueOf(tempAry[1]);
System.out.println(dec.format(divison));
}else if(ari.contains("%")){
String tempAry[] = ari.split("[%]");
model = Integer.valueOf(tempAry[0])%Integer.valueOf(tempAry[1]);
System.out.println(model);
}
}
}
public static void main(String[] args) {
Ari();
}
}
测试结果截图如下:
你的测试用例的输入的表达式的个数是4个,但下面的计算表达式好像少了一个 , 所以我加了一个除法的计算表达式,若理解有误,还望说明 。
如何用Java编写四则运算程序?(首先建个类,把这些复制粘贴进去)
import java.awt.*;
import javax.swing.*;
public class F {
JFrame frame = new JFrame("计算机");
JPanel pl = new JPanel();
JPanel p2 = new JPanel();
static JTextField show = new JTextField();
static JButton b0 = new JButton("0");
static JButton b1 = new JButton("1");
static JButton b2 = new JButton("2");
static JButton b3 = new JButton("3");
static JButton b4 = new JButton("4");
static JButton b5 = new JButton("5");
static JButton b6 = new JButton("6");
static JButton b7 = new JButton("7");
static JButton b8 = new JButton("8");
static JButton b9 = new JButton("9");
JButton bjia = new JButton("+");
JButton bjian = new JButton("-");
JButton bcheng = new JButton("*");
JButton bchu = new JButton("/");
JButton bdian = new JButton(".");
JButton bdeng = new JButton("=");
JButton bqingchu = new JButton("清除");
public void y() {
pl.setLayout(new GridLayout(1, 1));
pl.add(show);
}
public void p() {
b1.addActionListener(new U());
b2.addActionListener(new U());
b3.addActionListener(new U());
b4.addActionListener(new U());
b5.addActionListener(new U());
b6.addActionListener(new U());
b7.addActionListener(new U());
b8.addActionListener(new U());

推荐阅读