導航:首頁 > 編程語言 > java實現四則運算

java實現四則運算

發布時間:2022-08-06 05:23:31

1. 如何讓java以傳入參數的方法實現簡單四則運算

// String str = args[0];
// 模擬輸入
// 獲取輸入的內容
String str = "1.1+2";
if (str.contains("+")) {
String[] strs = str.split("\\+");
double arg1 = Double.valueOf(strs[0]);
double arg2 = Double.valueOf(strs[1]);
System.out.println(arg1 + arg2);
}
str = "1.1-2";
if (str.contains("-")) {
String[] strs = str.split("\\-");
double arg1 = Double.valueOf(strs[0]);
double arg2 = Double.valueOf(strs[1]);
System.out.println(arg1 - arg2);
}
str = "1.1*2";
if (str.contains("*")) {
String[] strs = str.split("\\*");
double arg1 = Double.valueOf(strs[0]);
double arg2 = Double.valueOf(strs[1]);
System.out.println(arg1 * arg2);
}
str = "1.1/2";
if (str.contains("/")) {
String[] strs = str.split("\\/");
double arg1 = Double.valueOf(strs[0]);
double arg2 = Double.valueOf(strs[1]);
System.out.println(arg1 / arg2);
}
寫的比較死。不然的話可能需要其他工具類的支持。

2. java實現四則運算

最後一個提示沒看懂意思。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;
}
}
}
}

3. 用java編寫四則運算(要求多個類,位元組編寫)

importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;

{
=1L;
JTextFieldtext1,text2;
JButtonbut1,but2,but3,but4;
JLabellab1;

publicBuju(){
super("布局模式");
setBounds(20,20,500,400);
ContainercontentPane=getContentPane();
contentPane.setLayout(newBorderLayout(10,10));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPaneljp1=newJPanel(newFlowLayout());
JPaneljp2=newJPanel(newFlowLayout());

text1=newJTextField(5);
text2=newJTextField(5);
lab1=newJLabel("輸出結");

jp2.add(text1);
jp2.add(text2);
jp2.add(lab1);

but1=newJButton("+");
but2=newJButton("-");
but3=newJButton("*");
but4=newJButton("/");
but1.addActionListener(this);
but2.addActionListener(this);
but3.addActionListener(this);
but4.addActionListener(this);

jp1.add(but1);
jp1.add(but2);
jp1.add(but3);
jp1.add(but4);

contentPane.add(jp2,BorderLayout.CENTER);
contentPane.add(jp1,BorderLayout.SOUTH);
}

publicvoidactionPerformed(java.awt.event.ActionEventevt){
doublex,y,s;
Stringstr;
x=Double.valueOf(text1.getText());
y=Double.valueOf(text2.getText());

if(evt.getSource()==but1){
s=x+y;
str=String.valueOf(s);
lab1.setText(str);
}
if(evt.getSource()==but2){
s=x-y;
str=String.valueOf(s);
lab1.setText(str);
}
if(evt.getSource()==but3){
s=x*y;
str=String.valueOf(s);
lab1.setText(str);
}
if(evt.getSource()==but4){
s=x/y;
str=String.valueOf(s);
lab1.setText(str);
}
}

publicstaticvoidmain(String[]args){
Bujubj=newBuju();
bj.setVisible(true);
}
}

結果運行:

4. 請用java編寫一個計算四則運算的方法

計算器的最好的演算法將中綴表達式轉換為後綴表達式,樓主可以搜索以下相關資料 ,
也可以 char[] c = xString.toCharArray(); 然後通過 asc 碼來比較,不果邏輯不好控制

5. java的四則運算

importjava.util.Scanner;

publicclassSimpleCalc{
publicSimpleCalc(){}

publicstaticvoidmain(String[]args){
calc();
}

publicstaticvoidcalc(){
Scannerreader=newScanner(System.in);
charop;
doublenum1,num2,result;

while(true){
System.out.println("請選擇運算符:1、+;2、-;3、*;4、/");
if(reader.hasNext()){
op=reader.nextChar();
switch(op){
case'1':
op=1;
break;
case'2':
op=2;
break;
case'3':
op=3;
break;
case'4':
op=4;
break;
case0://EOF
default:
System.out.println("輸入非法!");
op=0;
break;
}
if(0!=op){
try{
System.out.println("請輸入第一個運算數:");
if(reader.hasNext()){
num1=reader.nextDouble();
}
System.out.println("請輸入第二個運算數:");
if(reader.hasNext()){
num2=reader.nextDouble();
}
switch(op){
case1:
result=num1+num2;
break;
case2:
result=num1-num2;
break;
case3:
result=num1*num2;
break;
case4:
result=num1/num2;
break;
}
System.out.println("運算結果為:"+result);
System.out.println("是否繼續?是(Y)/否(N):");
}catch(Exceptionerr){
System.out.println("輸入非法!");
}
if(!isContinue(reader)){
break;
}
}
}
}
}

publicstaticbooleanisContinue(Scannerreader){
booleanflag=false;

if(reader.hasNext()){
charisContinue=reader.next().charAt(0);

if('N'==isContinue){
flag=false;
}elseif('Y'==isContinue){
flag=true;
}else{
System.out.println("是否繼續?是(Y)/否(N):");
flag=isContinue(reader);
}
}

returnflag;
}
}

6. java用按鈕實現四則運算

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TestResult extends JFrame implements ActionListener{
JTextField jfforward,jfcenter,jfbehind;
JButton jequals;
JComboBox list;
TestResult(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
jfforward = new JTextField(8);
jfcenter = new JTextField(8);
jfbehind = new JTextField(8);
jequals = new JButton("=");
list = new JComboBox();
setTitle("四則運算");
list.addItem("+");
list.addItem("-");
list.addItem("*");
list.addItem("/");
add(jfforward);
add(list);
add(jfcenter);
add(jequals);
add(jfbehind);
jequals.addActionListener(this);
this.setResizable(false);
this.setSize(500, 500);
this.setVisible(true);

}

public static void main(String[] args) {
new TestResult();
}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==jequals){
switch(list.getSelectedIndex()){
case 0:{
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n+m;
jfbehind.setText(""+sum);
}
break;
case 1:{
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n-m;
jfbehind.setText(""+sum);
}
break;
case 2:{
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n*m;
jfbehind.setText(""+sum);
}
break;
case 3:{
int n = Integer.parseInt(jfforward.getText());
int m = Integer.parseInt(jfcenter.getText());
int sum=n/m;
jfbehind.setText(""+sum);
}
break;
}
}

}

}
基本符合要求,我也不,就是照著書再加上自己的一點思路做的,其實你也可以
不夠健壯

7. 如何用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("");

}

}

8. 定義一個方法,實現四則運算的功能。java

import java.text.DecimalFormat;
import java.util.Scanner;

public class Zhi {

public static void main(String[] args) {
String condition = "";
Zhi = new Zhi();
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);
.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("無法識別改運算符");
}
}

}

9. Java中怎麼能實現從鍵盤輸入幾個數字後進行四則運算

Java中提供了一個Scanner類,接受用戶從鍵盤敲入的數值,具體 使用例子如下:

Scanners=newScanner(System.in);//接受鍵盤輸入的數值。
intstr=s.next();//將輸入的字元串轉換為int類型,就可以進行四則運算。
閱讀全文

與java實現四則運算相關的資料

熱點內容
加密狗ldk和超級狗的區別 瀏覽:917
vbnet入門經典pdf 瀏覽:631
貴陽網紅帶貨app閃嘜怎麼樣 瀏覽:559
怎麼在農行app上更新個人信息 瀏覽:676
基於51單片機的智能垃圾分類設計 瀏覽:848
蘋果app怎麼轉到安卓手機上 瀏覽:143
pe系統的命令 瀏覽:647
先進先出調度演算法 瀏覽:46
評測學演算法 瀏覽:436
安卓手機的鍵盤縮小了怎麼回事 瀏覽:142
網路共享文件夾恢復軟體 瀏覽:351
華為怎麼給部分照片加密 瀏覽:378
修復TF卡的命令 瀏覽:389
androidxmlnstools 瀏覽:746
下載沒完成的app怎麼消除不掉 瀏覽:575
視頻號演算法PDF 瀏覽:351
高考題演算法 瀏覽:111
開機桌面新建文件夾沒了 瀏覽:923
前海人壽保險app怎麼下載 瀏覽:939
壓縮空氣會降溫嗎 瀏覽:727