本文目錄一覽:
定義一個方法,實現四則運算的功能。java
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(“請輸入第一個數:”);
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(“無法識別改運算符”);
}
}
}
如何用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());
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(“”);
}
}
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中怎麼能實現從鍵盤輸入幾個數字後進行四則運算
Java中提供了一個Scanner類,接受用戶從鍵盤敲入的數值,具體 使用例子如下:
Scanner s = new Scanner(System.in);//接受鍵盤輸入的數值。
int str = s.next();//將輸入的字元串轉換為int類型,就可以進行四則運算。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/309545.html