import java.text.DecimalFormat;
成都創(chuàng)新互聯(lián)服務緊隨時代發(fā)展步伐,進行技術革新和技術進步,經(jīng)過10余年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設計師、專業(yè)的網(wǎng)站實施團隊以及高素質(zhì)售后服務人員,并且完全形成了一套成熟的業(yè)務流程,能夠完全依照客戶要求對網(wǎng)站進行成都網(wǎng)站建設、網(wǎng)站設計、建設、維護、更新和改版,實現(xiàn)客戶網(wǎng)站對外宣傳展示的首要目的,并為客戶企業(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("請輸入第一個數(shù):");
double x = scanner.nextDouble();
System.out.print("請輸入第二個數(shù):");
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("請輸入正確的數(shù)據(jù)!");
}
System.out.print("是否繼續(xù)?continue:繼續(xù),任意字符:結束");
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("無法識別改運算符");
}
}
}
(1)你做加減 乘除時均忘記符號位了,僅處理了符號全是正的情形。
建議不需要符號位,將正負號放到分子中,化簡時保證分母是正的即可
如分數(shù)-3/4 a=-3 b=4
否則加減法均需要分4種情況處理,乘除法也需要判斷同符號不同符號兩種情況處理
(2)加減 乘除的結果現(xiàn)在保留在當前分數(shù)對象中,若不想修改當前分數(shù),
可將方法中增加一個臨時分數(shù)對象
如加法
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();
}
不過建議加減 乘除的結果不是返回字符串對象,而是返回分數(shù)對象,以方便多個分數(shù)運算。
(3)化簡分數(shù)算法過于麻煩,找公約數(shù)可用輾轉相除法具體代碼如下
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");//格式化結果保留兩位小數(shù)
String all = "";//所有的計算表達式連在一起
System.out.println("請輸入表達式的個數(shù),只能為正整數(shù)");
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();
}
}
測試結果截圖如下:
你的測試用例的輸入的表達式的個數(shù)是4個,但下面的計算表達式好像少了一個,所以我加了一個除法的計算表達式,若理解有誤,還望說明。
運算就使用double吧。Double d=1+(2-4); 數(shù)字計算只能用double。或者String s=1+(2-4)+"";
網(wǎng)站題目:java分數(shù)四則運算代碼 java簡易四則運算計算器代碼
文章來源:http://www.chinadenli.net/article42/doogghc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站改版、企業(yè)網(wǎng)站制作、自適應網(wǎng)站、、響應式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)