import?java.io.*;
創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、冷水江網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、HTML5建站、成都做商城網(wǎng)站、集團公司官網(wǎng)建設、成都外貿(mào)網(wǎng)站建設公司、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為冷水江等各大城市提供網(wǎng)站開發(fā)制作服務。
public?class?Yunsuan{
public?static?void?main(String[]?args){
try{
String?x,y;
double?a,b,he,cha,ji,shang;
InputStreamReader?is=?new?InputStreamReader(System.in);
BufferedReader?in=new?BufferedReader(is);
System.out.println("請輸入:");
x=in.readLine();
a=Double.parseDouble(x);
y=in.readLine();
b=Double.parseDouble(y);
he=a+b;
cha=a-b;
ji=a*b;
shang=a/b;
System.out.println(a+"和"+b+"的和是:"+he);
System.out.println(a+"和"+b+"的差是:"+cha);
System.out.println(a+"和"+b+"的積是:"+ji);
System.out.println(a+"和"+b+"的商是:"+shang);
}
catch(IOException?e){
System.out.println(e);
}
}
}
編譯沒有問題了,但運行出錯
可以這么寫:
import?java.io.*;
import?java.util.Scanner;
public?class?Yunsuan{
public?static?void?main(String[]?args)?throws?IOException{
String?x;
String?y;
double?a,b,he,cha,ji,shang;
Scanner?scanner=new?Scanner(System.in);
/*?InputStreamReader?is=?new?InputStreamReader(System.in);
BufferedReader?in=new?BufferedReader(is);*/
System.out.println("請輸入:");
x=scanner.next();
a=Double.valueOf(x);
y=scanner.next();
b=Double.valueOf(y);
he=a+b;
cha=a-b;
ji=a*b;
shang=a/b;
System.out.println(a+"和"+b+"的和是:"+he);
System.out.println(a+"和"+b+"的差是:"+cha);
System.out.println(a+"和"+b+"的積是:"+ji);
System.out.println(a+"和"+b+"的商是:"+shang);
}
}
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個,但下面的計算表達式好像少了一個,所以我加了一個除法的計算表達式,若理解有誤,還望說明。
import java.text.DecimalFormat;
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("無法識別改運算符");
}
}
}