网站建设资讯

NEWS

网站建设资讯

算术我最棒java代码 java编写一个算术测试小软件

用Java设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算

下午发

临江网站建设公司成都创新互联公司,临江网站设计制作,有大型网站制作公司丰富经验。已为临江近千家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的临江做网站的公司定做!

五分钟搞定,先给你发个简单的,照例子自己去做你想要的计算器

package aaa;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class YunSuan implements ActionListener,ItemListener

{

public static JFrame jf;

public static Container c;

public static JTextField jtf1,jtf2,jtf3;

public static JButton jb1;

public static Choice c2;

public static String fuhao="";

public YunSuan()

{

jf=new JFrame("加减乘除运算");

c=jf.getContentPane();

c.setLayout(new FlowLayout());

jtf1=new JTextField(10);

jtf2=new JTextField(10);

jtf3=new JTextField(10);

c2=new Choice();

jb1=new JButton("=");

}

public void Jiemian()

{

c.add(jtf1);

c2.addItemListener(this);

c2.add("");

c2.add("+");

c2.add("-");

c2.add("*");

c2.add("/");

c.add(c2);

c.add(jtf2);

jb1.addActionListener(this);

c.add(jb1);

c.add(jtf3);

jf.setLocation(200,100);

jf.setVisible(true);

jf.pack();

}

public static void main(String args[])

{

YunSuan ys=new YunSuan();

ys.Jiemian();

}

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

double s1=Integer.parseInt(jtf1.getText());

double s2=Integer.parseInt(jtf2.getText());

double result=0;

if(fuhao.equals("+"))

{

result=s1+s2;

String result2=String.valueOf(result);

jtf3.setText(result2);

}

if(fuhao.equals("-"))

{

result=s1-s2;

String result2=String.valueOf(result);

jtf3.setText(result2);

}

if(fuhao.equals("*"))

{

result=s1*s2;

String result2=String.valueOf(result);

jtf3.setText(result2);

}

if(fuhao.equals("/"))

{

result=s1/s2;

String result2=String.valueOf(result);

jtf3.setText(result2);

}

}

public void itemStateChanged(ItemEvent ie)

{

if(ie.getSource()==c2)

{

String str1=c2.getSelectedItem();

fanhui(str1);

}

}

public String fanhui(String str2)

{

return fuhao=str2;

}

}

用java编写程序 课题:算术运算测试

你好,按照你的要求代码如下,可以直接运行

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class test {

public static void main(String[] args) {

int count = 0;

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

for (int i = 0; i 10; i++) {

int a = (int) (Math.random() * 100);

int b = (int) (Math.random() * 100);

int c = (int) (Math.random() * 2);

int r = 0;

switch (c) {

case 0:

if (a + b = 100) {

a = 100 - b;

}

System.out.print(a + " + " + b + " = ");

r = a + b;

break;

case 1:

if (a b) {

int t = a;

a = b;

b = t;

}

System.out.print(a + " - " + b + " = ");

r = a - b;

}

int input;

try {

input = Integer.valueOf(in.readLine());

} catch (Exception e1) {

System.out.println("答案错误\n");

continue;

}

if (input == r) {

count++;

System.out.println("答案正确\n");

} else {

System.out.println("答案错误\n");

}

}

if (null != in) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

System.out.println("答对数量:" + count + "\n得分: " + count * 10);

}

}

求编写一个超级简单的Java的程序源代码

class Stack_Float

{

float nums[];

int top;

Stack_Float()

{

nums = new float[50];

top = -1;

}

boolean IsEmpty()

{

if(top == -1)

return true;

else

return false;

}

float Pop_Stack()

{

if(top==-1)

{

return 0;

}

top--;

return nums[top + 1];

}

float GetTop()

{

return nums[top];

}

void Push_Stack(float num)

{

if(top == 49)

return;

top++;

nums[top] = num;

}

}

/*****************************************************************************/

class Stack_Char

{

char str[];

int top;

Stack_Char()

{

str = new char[50];

top = -1;

}

boolean IsEmpty()

{

if(top==-1)

return true;

else

return false;

}

void Push_Stack(char ch)

{

if(top == 49)

return;

top++;

str[top] = ch;

}

char Pop_Stack()

{

if(top == -1)

return '\0';

top--;

return str[top + 1];

}

char GetTop()

{

if(top == -1)

{

System.out.print("error");

System.exit(0);

}

return str[top];

}

}

/*****************************************************************************/

public class jisuanqi extends javax.swing.JFrame

{

String show = "";

public jisuanqi()

{

initComponents();

}

char[] TranSmit(char str[])

{

char houzhui[] = new char[50]; //存放后缀表达式的字符串

int i = 0,j = 0;

char c = str[i];

Stack_Char s = new Stack_Char(); //存放运算符的栈

while(c != '=') //对算术表达式扫描未结束时

{

if(c = '0' c = '9')

{

while(c = '0' c = '9')//数字直接入栈

{

houzhui[j]=c;

j++;

i++;

c=str[i];

}

houzhui[j]='#';//用#隔开数字

j++;

}

switch(c) //扫描到运算符时

{

case '+':

case '-':

case '*':

case '/':

if(s.IsEmpty() == true) //栈空,直接入栈

{

s.Push_Stack(c);

i++;

c=str[i];

break;

}

if(ComPare(s.GetTop(),c) == -1)

{

s.Push_Stack(c); //入栈

i++;

c=str[i];

break;

}

if(ComPare(s.GetTop(),c) == 1)

{

houzhui[j]=s.Pop_Stack();//出栈元素存入后缀表达式

j++;

break;

}

}

}

while(s.IsEmpty() != true)//把剩余的运算符直接出栈

{

houzhui[j]=s.Pop_Stack();

j++;

}

houzhui[j] = '=';//后缀表达式后面加 =

j++;

houzhui[j] = '\0';

j++;

return houzhui;

}

float Count(char str[])

{

Stack_Float s = new Stack_Float();//定义存放数字的栈

char c = str[0];

int i = 0,j = 0;

float result = 0,temp,left,right;

while(c != '=') //未扫描到 = 时

{

if(c = '0' c = '9')//扫描到数字

{

temp = 0;

while(c != '#')//未读到分隔符时

{

temp = temp * 10 + c - '0';

i++;

c = str[i];

}

s.Push_Stack(temp);//进栈

}

switch(c)//扫描到运算符时

{

case '+':

{

result = s.Pop_Stack() + s.Pop_Stack();//2个数字出栈相加

s.Push_Stack(result);//最后得数进栈

break;

}

case '-':

{

right = s.Pop_Stack();//右操作数出栈

left = s.Pop_Stack();//左操作数出栈

result = left - right;

s.Push_Stack(result);

break;

}

case '*':

{

result = s.Pop_Stack() * s.Pop_Stack();//2个数字出栈相乘

s.Push_Stack(result);

break;

}

case '/':

{

right = s.Pop_Stack();//右操作数出栈

left = s.Pop_Stack();//左操作数出栈

result = left / right;

s.Push_Stack(result);

break;

}

}

i++;

c = str[i];

}

return result;

}

int ComPare(char a,char b) //判断运算符的优先级函数

{

int s[][] =

{// 栈顶元素高于算术表达式中的元素时, 返回 1,否则返回 -1

{1,1,-1,-1},

{1,1,-1,-1},

{1,1,1,1},

{1,1,1,1},

};

char x1[]={'+','-','*','/'};//栈顶元素

char x2[]={'+','-','*','/'};//算术表达式中的元素

int k=0,m,n = 0;

for(m=0;m4;m++) //查找2个进行比较的运算符在表中的位置,并返回比较结果

{

for(n=0;n4;n++)

{

if(x1[m]==ax2[n]==b)

{

k=1;break; //找到比较结果后,跳出循环

}

}

if(k==1)

break;

}

return s[m][n];//返回比较结果

}

/*****************************************************************************/

@SuppressWarnings("unchecked")

// editor-fold defaultstate="collapsed" desc="Generated Code"//GEN-BEGIN:initComponents

private void initComponents() {

text = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();

jButton4 = new javax.swing.JButton();

jButton5 = new javax.swing.JButton();

jButton6 = new javax.swing.JButton();

jButton7 = new javax.swing.JButton();

jButton8 = new javax.swing.JButton();

jButton9 = new javax.swing.JButton();

jButton10 = new javax.swing.JButton();

jButton11 = new javax.swing.JButton();

jButton12 = new javax.swing.JButton();

jButton13 = new javax.swing.JButton();

jButton14 = new javax.swing.JButton();

jButton21 = new javax.swing.JButton();

jButton22 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

getContentPane().setLayout(null);

getContentPane().add(text);

text.setBounds(10, 10, 270, 30);

jButton1.setText("1");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

getContentPane().add(jButton1);

jButton1.setBounds(10, 50, 60, 25);

jButton2.setText("2");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

}

});

getContentPane().add(jButton2);

jButton2.setBounds(80, 50, 60, 25);

jButton3.setText("3");

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton3ActionPerformed(evt);

}

});

getContentPane().add(jButton3);

jButton3.setBounds(150, 50, 60, 25);

jButton4.setText("4");

jButton4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton4ActionPerformed(evt);

}

});

getContentPane().add(jButton4);

jButton4.setBounds(220, 50, 60, 25);

jButton5.setText("5");

jButton5.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton5ActionPerformed(evt);

}

});

getContentPane().add(jButton5);

jButton5.setBounds(10, 80, 60, 25);

jButton6.setText("6");

jButton6.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton6ActionPerformed(evt);

}

});

getContentPane().add(jButton6);

jButton6.setBounds(80, 80, 60, 25);

jButton7.setText("7");

jButton7.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton7ActionPerformed(evt);

}

});

getContentPane().add(jButton7);

jButton7.setBounds(150, 80, 60, 25);

jButton8.setText("8");

jButton8.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton8ActionPerformed(evt);

}

});

getContentPane().add(jButton8);

jButton8.setBounds(220, 80, 60, 25);

jButton9.setText("9");

jButton9.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton9ActionPerformed(evt);

}

});

getContentPane().add(jButton9);

jButton9.setBounds(10, 110, 60, 25);

jButton10.setText("0");

jButton10.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton10ActionPerformed(evt);

}

});

getContentPane().add(jButton10);

jButton10.setBounds(80, 110, 60, 25);

jButton11.setText("+");

jButton11.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton11ActionPerformed(evt);

}

});

getContentPane().add(jButton11);

jButton11.setBounds(150, 110, 60, 25);

jButton12.setText("-");

jButton12.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton12ActionPerformed(evt);

}

});

getContentPane().add(jButton12);

jButton12.setBounds(220, 110, 60, 25);

jButton13.setText("*");

jButton13.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton13ActionPerformed(evt);

}

});

getContentPane().add(jButton13);

jButton13.setBounds(10, 140, 60, 25);

jButton14.setText("/");

jButton14.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton14ActionPerformed(evt);

}

});

getContentPane().add(jButton14);

jButton14.setBounds(80, 140, 60, 25);

jButton21.setText("CE");

jButton21.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton21ActionPerformed(evt);

}

});

getContentPane().add(jButton21);

jButton21.setBounds(150, 140, 60, 25);

jButton22.setText("=");

jButton22.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton22ActionPerformed(evt);

}

});

getContentPane().add(jButton22);

jButton22.setBounds(220, 140, 60, 25);

pack();

}// /editor-fold//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

show += "1";

text.setText(show);

}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

show += "2";

text.setText(show);

}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

show += "3";

text.setText(show);

}//GEN-LAST:event_jButton3ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

show += "4";

text.setText(show);

}//GEN-LAST:event_jButton4ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed

show += "5";

text.setText(show);

}//GEN-LAST:event_jButton5ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed

show += "6";

text.setText(show);

}//GEN-LAST:event_jButton6ActionPerformed

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed

show += "7";

text.setText(show);

}//GEN-LAST:event_jButton7ActionPerformed

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed

show += "8";

text.setText(show);

}//GEN-LAST:event_jButton8ActionPerformed

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed

show += "9";

text.setText(show);

}//GEN-LAST:event_jButton9ActionPerformed

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed

show += "0";

text.setText(show);

}//GEN-LAST:event_jButton10ActionPerformed

private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed

show += "*";

text.setText(show);

}//GEN-LAST:event_jButton13ActionPerformed

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed

show += "/";

text.setText(show);

}//GEN-LAST:event_jButton14ActionPerformed

private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton21ActionPerformed

show = "";

text.setText("");

}//GEN-LAST:event_jButton21ActionPerformed

private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton22ActionPerformed

show += "=";

text.setText(show);

char str1[] = new char[50];

char str2[] = new char[50];

float result = 0;

str1 = show.toCharArray();

str2 = TranSmit(str1);

result = Count(str2);

text.setText("" + result);

show = "";

}//GEN-LAST:event_jButton22ActionPerformed

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed

show += "-";

text.setText(show);

}//GEN-LAST:event_jButton12ActionPerformed

private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed

show += "+";

text.setText(show);

}//GEN-LAST:event_jButton11ActionPerformed

public static void main(String args[])

{

jisuanqi j = new jisuanqi();

j.setBounds(300, 300, 300, 195);

j.setVisible(true);

}

// Variables declaration - do not modify//GEN-BEGIN:variables

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton10;

private javax.swing.JButton jButton11;

private javax.swing.JButton jButton12;

private javax.swing.JButton jButton13;

private javax.swing.JButton jButton14;

private javax.swing.JButton jButton2;

private javax.swing.JButton jButton21;

private javax.swing.JButton jButton22;

private javax.swing.JButton jButton3;

private javax.swing.JButton jButton4;

private javax.swing.JButton jButton5;

private javax.swing.JButton jButton6;

private javax.swing.JButton jButton7;

private javax.swing.JButton jButton8;

private javax.swing.JButton jButton9;

private javax.swing.JTextField text;

// End of variables declaration//GEN-END:variables

}

Java编写一个能够自动生成小朋友算术练习题目的程序。设计一个程序,要求能够随机生成每个题目。

package com.demo;

import java.math.BigDecimal;

import java.util.Random;

import java.util.Scanner;

/*

* Java编写一个能够自动生成小朋友算术练习题目的程序。设计一个程序,要求能够随机生成每个题目。

* 编写一个能够自动生成小朋友算术练习题目的程序。设计一个程序,要求能够随机生成每个题目,并具有以下基本功能:

* (1)要求题目以单项选择题及填空题两种形式出现。

* (2)完成一题后自动判断答案是否正确,分别弹出不同的提示,并自动更新成下一题。

* (3)单击“退出”时,汇总本次计算结果,共完成几题,正确率,并给出相应评语。确定后正式退出。

*/

public class publicDemo2 {

public static void main(String[] args) {

Random random = new Random();

StringBuilder stringBuilder = new StringBuilder();

String[] symbol = {"+","-","*","/"};

String question_tip = "选择题(除法保留小数点后两位):";

stringBuilder.append(question_tip).append("\n");

System.out.println(question_tip);

int a;

int b;

String f;

String result_str;

for (int i=1;i11;i++){

a = random.nextInt(50);

b = random.nextInt(50);

f = symbol[random.nextInt(3)];

String question = i+"、"+a+f+b+"=";

System.out.println(question);

stringBuilder.append(question);

Scanner sc = new Scanner(System.in);

String sc_str = sc.next();

if("/".equals(f)){

double scan_value = Double.parseDouble(sc_str);

double result_value = a / b;

BigDecimal bigDecimal1 = new BigDecimal(scan_value);

scan_value = bigDecimal1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

BigDecimal bigDecimal2 = new BigDecimal(result_value);

result_value = bigDecimal2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

result_str = (scan_value==result_value)?"回答正确!":"回答错误!";

stringBuilder.append(scan_value).append(";(答案:"+result_value+")"+result_str).append("\n");

}else{

int scan_value = Integer.parseInt(sc_str);

int result_value;

if("+".equals(f)){

result_value = a + b;

}else if("-".equals(f)){

result_value = a - b;

}else if("*".equals(f)){

result_value = a * b;

}else{

break;

}

result_str = (scan_value==result_value)?"回答正确!":"回答错误!";

stringBuilder.append(scan_value).append(";(答案:"+result_value+")"+result_str).append("\n");

}

}

System.out.println(stringBuilder.toString());

}

}


当前文章:算术我最棒java代码 java编写一个算术测试小软件
当前网址:http://cdweb.net/article/ddgsjip.html