「四则运算的java程序」字符串四则运算java
今天给各位分享四则运算的java程序的知识,其中也会对字符串四则运算java进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
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程序
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 java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testMath extends Frame {
public static final int W_WIDTH=800,W_HEIGHT=600;
public static final int X_LOCATION=0,Y_LOCATION=0;
Image offScreemImage = null;
Font myFont = new Font("Arial",Font.CENTER_BASELINE,14);
boolean started = false,resulted=false;
boolean [] answer = new boolean [10];
String [] questions = {
"10+10*10 =",
"i'm the quesiton 2",
"i'm the quesiton 3",
"i'm the quesiton 4",
"i'm the quesiton 5",
"i'm the quesiton 6",
"i'm the quesiton 7",
"i'm the quesiton 8",
"i'm the quesiton 9",
"i'm the quesiton 10"
};
int [] questionA = {110,2,3,4,5,6,7,8,9,10};
int questionNo=0;
int finalresult=0;
TextField answerArea;
public void lanchFram(){
this.setLocation(X_LOCATION,Y_LOCATION);
this.setSize(W_WIDTH, W_HEIGHT);
setVisible(true);
this.setTitle("数学测验");
this.setResizable(false);
this.setBackground(Color.white);
new Thread(new PaintThread()).start();
this.setFont(myFont);
//closing window's event
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent a) {
System.exit(0);
}
});
this.setLayout(null);
answerArea = new TextField(10);
Panel toolBar = new Panel();
Panel answerText = new Panel();
Label notify = new Label("输入你的答案:");
answerText.add(notify);
answerText.add(answerArea);
Button start=new Button("start");
Button next = new Button("next");
Button last = new Button("last");
Button result = new Button("result");
answerArea.addActionListener(new answers());
start.addActionListener(new started());
last.addActionListener(new lastQuestion());
next.addActionListener(new nextQuestion());
result.addActionListener(new results());
toolBar.add(start);
toolBar.add(next);
toolBar.add(last);
toolBar.add(result);
answerText.setBounds(300,350,200,60);
toolBar.setBounds(250,450, 300, 30);
add(answerText);
add(toolBar);
}
class answers implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
if(questionA[questionNo]==Integer.parseInt(answerArea.getText())){
answer[questionNo]=true;
}
else{
answer[questionNo]=false;
}
answerArea.setText("");
}
catch(Exception x){
answerArea.setText("");
}
}
}
class started implements ActionListener{
public void actionPerformed(ActionEvent e){
started=true;
}
}
class nextQuestion implements ActionListener{
public void actionPerformed(ActionEvent e){
if(questionNo9started!resulted){
questionNo++;
}
}
}
class lastQuestion implements ActionListener{
public void actionPerformed(ActionEvent e){
if(questionNo0started!resulted){
questionNo--;
}
}
}
class results implements ActionListener{
public void actionPerformed(ActionEvent e){
if(started){
for(int i=0;i10;i++){
if(answer[i]){
finalresult +=10;
}
}
resulted=true;
}
}
}
public static void main(String[] args) {
testMath a = new testMath();
a.lanchFram();
}
public void paint(Graphics g)
{
if(started){
g.drawString("Question:"+(questionNo+1)+" "+questions [questionNo], 300, 250);
}
if(resultedstarted){
int x=600,y=50;
for(int i = 0;i10;i++){
g.drawString("Question"+i+": "+answer[i], x, y);
y+=30;
}
g.drawString("Final result:"+finalresult, x, y);
}
}
public void update(Graphics g) {
if(offScreemImage == null){
offScreemImage=this.createImage(W_WIDTH,W_HEIGHT);
}
Graphics gOffScreem = offScreemImage.getGraphics();
Color c = gOffScreem.getColor();
gOffScreem.setColor(Color.white);
gOffScreem.fillRect(0, 0, W_WIDTH,W_HEIGHT);
gOffScreem.setColor(c);
paint(gOffScreem);
g.drawImage(offScreemImage,0,0,null);
}
//set up the Frame update time
private class PaintThread implements Runnable{
public void run() {
while(true){
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
java四则运算
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class calculator extends JFrame implements ActionListener {
private boolean i=true,clickable = true ;//这里的clickabel 是用来判断小数点的 如果有小数点的话 clickable则为false就不对小数点进行添加了 i在下面有解释
private double value=0; //这个就是显示出来的值了
String operate = "="; //操作数 默认情况下是=号
JFrame jframe=new JFrame("计算机");
JTextField text1=new JTextField("0");
JButton back = new JButton("退格");
JButton daoshu = new JButton("1/x");
JButton remain = new JButton("%");
JButton PI = new JButton("PI"); //显示圆周率小数点后7位
JButton square = new JButton("x^2");
JButton cube = new JButton("x^3");
JButton plu=new JButton("+");
JButton min=new JButton("-");
JButton mul=new JButton("*");
JButton equ=new JButton("=");
JButton div=new JButton("/");
JButton code=new JButton("+/-");
JButton point=new JButton(".");
JButton num1=new JButton("1");
JButton num2=new JButton("2");
JButton num3=new JButton("3");
JButton num4=new JButton("4");
JButton num5=new JButton("5");
JButton num6=new JButton("6");
JButton num7=new JButton("7");
JButton num8=new JButton("8");
JButton num9=new JButton("9");
JButton num0=new JButton("0");
JButton num00=new JButton("00");
JButton clear=new JButton("C");
calculator(){
jframe.setSize(400, 400);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setBackground(Color.gray);
p.setLayout(new GridLayout(5, 5)); //采用的布局方式为网格布局
PI.addActionListener(this);
daoshu.addActionListener(this);
remain.addActionListener(this);
square.addActionListener(this);
cube.addActionListener(this);
plu.addActionListener(this);
equ.addActionListener(this);
num1.addActionListener(this);
num2.addActionListener(this);
clear.addActionListener(this);
num3.addActionListener(this);
num4.addActionListener(this);
num5.addActionListener(this);
num6.addActionListener(this);
num7.addActionListener(this);
num8.addActionListener(this);
num9.addActionListener(this);
num0.addActionListener(this);
num00.addActionListener(this);
min.addActionListener(this);
code.addActionListener(this);
div.addActionListener(this);
point.addActionListener(this);
mul.addActionListener(this);
back.addActionListener(this);
jframe.add(p);
p.add(PI);
p.add(code);
p.add(daoshu);
p.add(back);
p.add(clear);
p.add(num7);
p.add(num8);
p.add(num9);
p.add(plu);
p.add(remain);
p.add(num4);
p.add(num5);
p.add(num6);
p.add(min);
p.add(square);
p.add(num1);
p.add(num2);
p.add(num3);
p.add(mul);
p.add(cube);
p.add(num0);
p.add(num00);
p.add(point);
p.add(div);
p.add(equ);
jframe.add(text1,"North");
jframe.setLocation(300, 300);
jframe.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
String str1,str2;
Object soc=evt.getSource();
String s = evt.getActionCommand(); //获取按钮的数据
if(soc==back){ //进行退格操作
str1=text1.getText();
int n=str1.length();
text1.setText(str1.substring(0,n-1));
str2 = text1.getText();
}
else if (s.equals("x^2")) {
str1=text1.getText();
double k = Double.parseDouble(str1);
k *=k;
text1.setText(String.valueOf(k));
}
else if (s.equals("x^3")) {
str1=text1.getText();
double k = Double.parseDouble(str1);
k =k*k*k;
text1.setText(String.valueOf(k));
}
else if(s.equals("PI")){ //显示圆周率
double Pi=3.1415926;
String pi=String.valueOf(Pi);
text1.setText(pi);
}
else if(s.equals("+/-")){ //变换正负号
double a = 0;
str1=text1.getText();
a=Double.parseDouble(str1.trim());
a=-1*a;
str1=String.valueOf(a);
text1.setText(str1);
}
else if(s.equals("1/x")){ //求倒
str1=text1.getText();
double i = 1/Double.parseDouble(str1.trim());
String k = String.valueOf(i);
text1.setText(k);
}
else if(s.equals(".")){ //小数点
clickable=true;
for (int i = 0; i text1.getText().length(); i++)
if ('.' == text1.getText().charAt(i))
{
clickable=false;
break;
} //第一层判断是否里面含有小数点;
if(clickable==true) //第二层判断
text1.setText(text1.getText()+".");
i=false;
}
else if(s.equals("C")){ //清零 所有的数据变回初始值
value = 0;
text1.setText("0");
i=true;
operate = "=" ;
}
else if ('0' = s.charAt(0) s.charAt(0) = '9' ) { //若输入的为数字以及小数点的时候执行下面
if (i) { //判断获得的command是否为操作符
text1.setText(s);
}
else {
text1.setText(text1.getText() + s);
}
i = false;
}
else { //若输入运算操作符的话则执行下面的代码
count(Double.parseDouble(text1.getText().trim())); //这里一开始调用 count()方法的话首先执行的是operate.equals("=")
//执行上个操作符的运算
operate = s;
i = true;
}
}
private void count(double a) { //计算区域
if (operate.equals("+")) {
value += a;
}
else if (operate.equals("-")) {
value -= a;
}
else if (operate.equals("*")) {
value *= a;
}
else if (operate.equals("/")) {
value /= a;
}
else if (operate.equals("%")) {
value %= a;
}
else if (operate.equals("=")) {
value = a;
}
text1.setText(Double.toString(value));
}
public static void main(String[] arg)
{
calculator cal=new calculator();
}
}
这是例子 你可以参考下~监听的 部分
java的四则运算
import java.util.Scanner;
public class SimpleCalc {
public SimpleCalc() {}
public static void main(String[] args) {
calc();
}
public static void calc() {
Scanner reader = new Scanner(System.in);
char op;
double num1, 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;
case 0://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) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
result = num1 / num2;
break;
}
System.out.println("运算结果为:"+ result);
System.out.println("是否继续?是(Y)/否(N):");
} catch(Exception err) {
System.out.println("输入非法!");
}
if (!isContinue(reader)) {
break;
}
}
}
}
}
public static boolean isContinue(Scanner reader) {
boolean flag = false;
if (reader.hasNext()) {
char isContinue = reader.next().charAt(0);
if ('N' == isContinue) {
flag = false;
} else if ('Y' == isContinue) {
flag = true;
} else {
System.out.println("是否继续?是(Y)/否(N):");
flag = isContinue(reader);
}
}
return flag;
}
}
如何用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程序的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于字符串四则运算java、四则运算的java程序的信息别忘了在本站进行查找喔。