大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.text.DecimalFormat;
成都创新互联服务紧随时代发展步伐,进行技术革新和技术进步,经过10余年的发展和积累,已经汇集了一批资深网站策划师、设计师、专业的网站实施团队以及高素质售后服务人员,并且完全形成了一套成熟的业务流程,能够完全依照客户要求对网站进行成都网站建设、网站设计、建设、维护、更新和改版,实现客户网站对外宣传展示的首要目的,并为客户企业品牌互联网化提供全面的解决方案。
import java.util.Scanner;
public class Zhidao {
public static void main(String[] args) {
String condition = "";
Zhidao zhidao = new Zhidao();
do{
Scanner scanner = new Scanner(System.in);
try{
System.out.print("请输入第一个数:");
double x = scanner.nextDouble();
System.out.print("请输入第二个数:");
double y = scanner.nextDouble();
System.out.print("请输入运算符:");
String s = scanner.next();
char z = s.charAt(0);
zhidao.yunsuan(x, y, z);
}catch(Exception e){
System.out.println("请输入正确的数据!");
}
System.out.print("是否继续?continue:继续,任意字符:结束");
condition = scanner.next();
}while("continue".equals(condition));
}
public static void yunsuan(double x,double y,Character z){
DecimalFormat r=new DecimalFormat();
r.applyPattern("#0.00");
if(z.equals('+')){
System.out.println(x+"+"+y+"=" + r.format((x+y)));
} else if(z.equals('-')){
System.out.println(x+"-"+y+"=" + r.format((x-y)));
} else if(z.equals('*')){
System.out.println(x+"*"+y+"=" + r.format((x*y)));
} else if(z.equals('/')){
if(y==0){
System.out.println("被除数不能为0");
} else{
System.out.println(x+"/"+y+"=" + r.format((x/y)));
}
}else{
System.out.println("无法识别改运算符");
}
}
}
(1)你做加减 乘除时均忘记符号位了,仅处理了符号全是正的情形。
建议不需要符号位,将正负号放到分子中,化简时保证分母是正的即可
如分数-3/4 a=-3 b=4
否则加减法均需要分4种情况处理,乘除法也需要判断同符号不同符号两种情况处理
(2)加减 乘除的结果现在保留在当前分数对象中,若不想修改当前分数,
可将方法中增加一个临时分数对象
如加法
public String add(CNumber oper)
{
CNumber result=new CNumber();
result.a=this.a*oper.b+oper.a*this.b;
result.b=this.b*oper.b;
return result.toString();
}
不过建议加减 乘除的结果不是返回字符串对象,而是返回分数对象,以方便多个分数运算。
(3)化简分数算法过于麻烦,找公约数可用辗转相除法具体代码如下
public void simpleCNumber() //化简结果仍保存在当前对象中,不需要另建对象
{
long m=(long)Math.abs(a), n=b , r ;
if(a==0) {b=1;return;}
while r!=0
{
m=n; n=r; r=m%n;
}
a/=n; b/=n;
}
最后一个提示没看懂意思。import java.util.Random;
public class JiS {
public static void main(String[] args)
{
Random r=new Random();
char[]ch=new char[]{'+','-','*','/'};
boolean flag=true;
while(flag){
int a=r.nextInt(10001);
int b=r.nextInt(10001);
char c=ch[r.nextInt(ch.length)];
// System.out.println(a+","+b+","+c);
switch(c)
{
case '+':
if(a+b=10000){System.out.println(a+"+"+b+"="+(a+b));flag=false;}
break;
case '-':
if(a-b=0){System.out.println(a+"-"+b+"="+(a-b));flag=false;}
break;
case '*':
if(a*b=10000){System.out.println(a+"*"+b+"="+a*b);flag=false;}
break;
case '/':
if(b!=0){System.out.println(a+"/"+b+"="+a/b);flag=false;}
break;
}
}
}
}
(首先建个类,把这些复制粘贴进去)
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());
b9.addActionListener(new U());
b0.addActionListener(new U());
bjia.addActionListener(new Fu());
bjian.addActionListener(new Fu());
bcheng.addActionListener(new Fu());
bchu.addActionListener(new Fu());
bdeng.addActionListener(new Deng());
bqingchu.addActionListener(new Qing());
p2.setLayout(new GridLayout(6, 3));
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b0);
p2.add(bjia);
p2.add(bjian);
p2.add(bcheng);
p2.add(bchu);
p2.add(bdian);
p2.add(bqingchu);
p2.add(bdeng);
}
public void o() {
frame.setLayout(new BorderLayout());
frame.add(pl, BorderLayout.NORTH);
frame.add(p2, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
F f = new F();
f.y();
f.p();
f.o();
}
}
(再新建个类 把这些也复制粘贴进去)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class U implements ActionListener {
public static String str = "";
public static String a = "";
public static String b = "";
public static String k = "";
public void actionPerformed(ActionEvent e) {
String w = e.getActionCommand();//字
if (k.equals("")) {
a += w;
F.show.setText(a);
} else {
b += w;
F.show.setText(b);
}
}
}
(再新建一个,把下面的复制粘贴)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Deng implements ActionListener {
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(U.a);
int b = Integer.parseInt(U.b);
int c = 0;
if (U.k.equals("+")) {
c = a + b;
} else
if (U.k.equals("-")) {
c = a - b;
} else
if (U.k.equals("*")) {
c = a * b;
} else
if (U.k.equals("/")) {
c = a / b;
} else {
}
String d = String.valueOf(c);
F.show.setText(d);
U.a = d;
U.b = "";
U.k = "";
}
}
(在建一个 复制粘贴)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Fu implements ActionListener {
public void actionPerformed(ActionEvent e) {
String a = e.getActionCommand();//字
U.k = a;
}
}
(在建一个)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Qing implements ActionListener {
public void actionPerformed(ActionEvent e) {
U.a = "";
U.b = "";
U.k = "";
F.show.setText("");
}
}
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个,但下面的计算表达式好像少了一个,所以我加了一个除法的计算表达式,若理解有误,还望说明。
运算就使用double吧。Double d=1+(2-4); 数字计算只能用double。或者String s=1+(2-4)+"";