import java.text.DecimalFormat;
成都創(chuàng)新互聯(lián)服務(wù)緊隨時(shí)代發(fā)展步伐,進(jìn)行技術(shù)革新和技術(shù)進(jìn)步,經(jīng)過10余年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計(jì)師、專業(yè)的網(wǎng)站實(shí)施團(tuán)隊(duì)以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶要求對(duì)網(wǎng)站進(jìn)行成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、建設(shè)、維護(hù)、更新和改版,實(shí)現(xiàn)客戶網(wǎng)站對(duì)外宣傳展示的首要目的,并為客戶企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。
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("請(qǐng)輸入第一個(gè)數(shù):");
double x = scanner.nextDouble();
System.out.print("請(qǐng)輸入第二個(gè)數(shù):");
double y = scanner.nextDouble();
System.out.print("請(qǐng)輸入運(yùn)算符:");
String s = scanner.next();
char z = s.charAt(0);
zhidao.yunsuan(x, y, z);
}catch(Exception e){
System.out.println("請(qǐng)輸入正確的數(shù)據(jù)!");
}
System.out.print("是否繼續(xù)?continue:繼續(xù),任意字符:結(jié)束");
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("被除數(shù)不能為0");
} else{
System.out.println(x+"/"+y+"=" + r.format((x/y)));
}
}else{
System.out.println("無法識(shí)別改運(yùn)算符");
}
}
}
(1)你做加減 乘除時(shí)均忘記符號(hào)位了,僅處理了符號(hào)全是正的情形。
建議不需要符號(hào)位,將正負(fù)號(hào)放到分子中,化簡時(shí)保證分母是正的即可
如分?jǐn)?shù)-3/4 a=-3 b=4
否則加減法均需要分4種情況處理,乘除法也需要判斷同符號(hào)不同符號(hào)兩種情況處理
(2)加減 乘除的結(jié)果現(xiàn)在保留在當(dāng)前分?jǐn)?shù)對(duì)象中,若不想修改當(dāng)前分?jǐn)?shù),
可將方法中增加一個(gè)臨時(shí)分?jǐn)?shù)對(duì)象
如加法
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();
}
不過建議加減 乘除的結(jié)果不是返回字符串對(duì)象,而是返回分?jǐn)?shù)對(duì)象,以方便多個(gè)分?jǐn)?shù)運(yùn)算。
(3)化簡分?jǐn)?shù)算法過于麻煩,找公約數(shù)可用輾轉(zhuǎn)相除法具體代碼如下
public void simpleCNumber() //化簡結(jié)果仍保存在當(dāng)前對(duì)象中,不需要另建對(duì)象
{
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;
}
最后一個(gè)提示沒看懂意思。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;
}
}
}
}
(首先建個(gè)類,把這些復(fù)制粘貼進(jìn)去)
import java.awt.*;
import javax.swing.*;
public class F {
JFrame frame = new JFrame("計(jì)算機(jī)");
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();
}
}
(再新建個(gè)類 把這些也復(fù)制粘貼進(jìn)去)
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);
}
}
}
(再新建一個(gè),把下面的復(fù)制粘貼)
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 = "";
}
}
(在建一個(gè) 復(fù)制粘貼)
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;
}
}
(在建一個(gè))
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");//格式化結(jié)果保留兩位小數(shù)
String all = "";//所有的計(jì)算表達(dá)式連在一起
System.out.println("請(qǐng)輸入表達(dá)式的個(gè)數(shù),只能為正整數(shù)");
int n = scan.nextInt();
System.out.println("請(qǐng)依次輸入要計(jì)算的表達(dá)式");
? ?for(int i=0;in+1;i++){
? ? buffer = buffer.append(scan.nextLine()+",");
? ?}
? ?all = buffer.substring(0, buffer.lastIndexOf(","));
? ?String allAri[] = all.split(",");
? ?String ari = "";//不同的算法表達(dá)式
? ?float add;//加法的計(jì)算結(jié)果
? ?float subtract;//減肥的計(jì)算結(jié)果
? ?float multi;//乘法的計(jì)算結(jié)果
? ?float divison;//除法的計(jì)算結(jié)果
? ?int model;//模運(yùn)算的計(jì)算結(jié)果
? ?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();
}
}
測(cè)試結(jié)果截圖如下:
你的測(cè)試用例的輸入的表達(dá)式的個(gè)數(shù)是4個(gè),但下面的計(jì)算表達(dá)式好像少了一個(gè),所以我加了一個(gè)除法的計(jì)算表達(dá)式,若理解有誤,還望說明。
運(yùn)算就使用double吧。Double d=1+(2-4); 數(shù)字計(jì)算只能用double?;蛘逽tring s=1+(2-4)+"";