四则运算安卓版ver.mk3

时间:2022-08-29 13:23:50

四则运算安卓版ver.mk3

在原有的基础上做了些许改动以及添加了一点小功能,以下是代码:

 package com.example.add;

 import java.io.File;

 import com.example.add.opt;
import com.example.add.R; import java.text.DecimalFormat; import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { MediaPlayer mp;
int x;//题目个数
int y=0;
int n;
int i=1;
char[] op = { '+', '-', '*', '/' };
int[] nov = new int[4];
int zf;//控制正负
int zz;//题型控制
String str = new String();
String str1 = new String();
int[] num1 = new int[5];
String s[] = new String[5];
int num2=0;
float[] anser=new float[5];//用户的答案
float[] trueanser=new float[5];//正确答案
File file;
int a=0;
int b=0;
int bb=0;
long begin=0,end=0,time=0;
String ss;
// int c = 0;
opt opt=new opt();;
EditText editText,xs;
StringBuffer text,text1;
String number_1="",number_2="";
boolean clicked=false;
byte style=0;
Button btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0;
Button start,btn_eq,btn_dot,btn_clear,btn_m1,btn_m2,btn_m3;
private Button btn_dot1;
/** Called when the activity is first created. */ public void stop(){
mp.reset();
mp=MediaPlayer.create(this, R.raw.unity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
text=new StringBuffer();
editText=(EditText)findViewById(R.id.result);
xs=(EditText)findViewById(R.id.xs);
mp=MediaPlayer.create(this, R.raw.unity);
//editText.setFocusable(false);
btn_1=(Button) findViewById(R.id.btn_1);
btn_1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
text.append("7");
editText.setText(text.toString()); } });
btn_2=(Button) findViewById(R.id.btn_2);
btn_2.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
text.append("8");
editText.setText(text.toString());
}
});
btn_3=(Button) findViewById(R.id.btn_3);
btn_3.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
text.append("9");
editText.setText(text.toString());
} });
btn_4=(Button) findViewById(R.id.btn_4);
btn_4.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
text.append("4");
editText.setText(text.toString());
} });
btn_5=(Button) findViewById(R.id.btn_5);
btn_5.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
text.append("5");
editText.setText(text.toString());
} });
btn_6=(Button) findViewById(R.id.btn_6);
btn_6.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
text.append("6");
editText.setText(text.toString());
} });
btn_7=(Button) findViewById(R.id.btn_7);
btn_7.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
text.append("1");
editText.setText(text.toString());
} });
btn_8=(Button) findViewById(R.id.btn_8);
btn_8.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
text.append("2");
editText.setText(text.toString());
} });
btn_9=(Button) findViewById(R.id.btn_9);
btn_9.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
text.append("3");
editText.setText(text.toString());
} }); btn_0=(Button) findViewById(R.id.btn_0);
btn_0.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
text.append("0");
editText.setText(text.toString());
} }); btn_dot=(Button) findViewById(R.id.btn_dot);
btn_dot.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
if(editText.getText().toString().indexOf(".")==-1){//已经有.了
if(text.length()==0){
text.append("0.");
}else{
text.append(".");
}
editText.setText(text.toString());
}
} }); btn_dot1=(Button) findViewById(R.id.btn_10);
btn_dot1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(clicked){
editText.setText("");
clicked=false;
text.setLength(0);
}
text.append("-");
editText.setText(text.toString());
} }); btn_m1=(Button) findViewById(R.id.music1);
btn_m1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
mp.start();
}
}); btn_m2=(Button) findViewById(R.id.music2);
btn_m2.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
stop();
}
}); btn_m3=(Button) findViewById(R.id.music3);
btn_m3.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
mp.pause();
}
}); btn_eq=(Button) findViewById(R.id.btn_eq);
btn_eq.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
if(b==1)
{
if(editText.getText().toString().length()!=0)
{
try
{
x=Integer.parseInt(editText.getText().toString());
if(x>=0 && x<=5)b++;
else Toast.makeText(MainActivity.this, "输入有误", 1).show();
}
catch(NumberFormatException x)
{
Toast.makeText(MainActivity.this, "输入有误", 1).show();
}
}
else Toast.makeText(MainActivity.this, "输入有误", 1).show();
editText.setText("");
number_1="";
number_2="";
style=0;
clicked=false;
text.setLength(0);
begin=System.currentTimeMillis();
}
if(b==2)
{
if(n!=0)anser[i-2]=Float.parseFloat(editText.getText().toString());
str1="第"+i+"道题目为:";
n=(int)(Math.random() * 4 + 2);//随机出运算符的数量
zz=(int)(Math.random()*2);
for(int j=0;j<n;j++)
{
//zf=(int)(Math.random()*2);
//if(zf==0)//负
// num1[j]=-(int)(Math.random()*100);//产生随机数
//else //正
num1[j]=(int)(Math.random()*100);
if(zz==1)num1[j]=(int)(Math.random()*10+1);
}
for(int k=0;k<n-1;k++)
{
nov[k]=(int)(Math.random()*4);
if(nov[k]==3&&num1[k+1]==0)
{
do
{
num1[k+1]=(int)(Math.random()*100);
}while(num1[k+1]==0);
}
}
str="";
if(zz==0)
{
for(int j=0;j<n;j++)
{
//if(num1[j]<0)str=str+"("+String.valueOf(num1[j])+")";
str=str+String.valueOf(num1[j]);
if(j==n-1)
{
//try
//{
//int ii=0;
try{
trueanser[i-1]=opt.Math(str);
DecimalFormat df = new DecimalFormat("#.00");
trueanser[i-1]=Float.parseFloat(df.format(trueanser[i-1]));
} catch (Exception e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
//}
/*catch (Exception e1)
{
System.out.println(e1);
//e1.printStackTrace();
}*/
str=str+"=";
//str=str+String.valueOf(trueanser[i-1]);
xs.setText(str1+str);
break;
}
str=str+op[nov[j]];
}
}
if(zz==1)
{
trueanser[i-1]=1;
str=String.valueOf(num1[0])+"!=";
for(int j=1;j<=num1[0];j++)
trueanser[i-1]=trueanser[i-1]*j;
//str=str+String.valueOf(trueanser[i-1]);
xs.setText(str1+str);
}
if(i==x+1)
{
b=3;
anser[i-2]=Float.parseFloat(editText.getText().toString());
}
else i++;
editText.setText("");
number_1="";
number_2="";
style=0;
clicked=false;
text.setLength(0);
}
if(b==3)
{
ss="";
y=0;
for(int j=0;j<x;j++)
{
if(anser[j]==trueanser[j])y++;
ss=ss+(j+1)+":"+trueanser[j]+";\n";
}
end=System.currentTimeMillis();
time=(end-begin)/1000;
xs.setText("答题完成!答对"+y+"道!\n"+"用时:"+time+"s 正确答案:");
editText.setSingleLine(false);
editText.setHorizontallyScrolling(false);
editText.setText(ss); b=0;
number_1="";
number_2="";
style=0;
clicked=false;
text.setLength(0);
}
}
});
start=(Button) findViewById(R.id.start);
start.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
xs.setText("请输入出题数量!(1-5)");
n=0;
i=1;
b=1;
}
});
btn_clear=(Button) findViewById(R.id.btn_clear);
btn_clear.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
editText.setText("");
number_1="";
number_2="";
style=0;
clicked=false;
text.setLength(0);
}
});
}
}

MainActivity

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/a1"
> <TextView
android:id="@+id/xs1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true" android:text="欢迎使用四则运算程序 ! ! !"
android:textColor="#ffd700"
android:textSize="28sp" > <requestFocus />
</TextView> <EditText
android:hint="此处显示题目"
android:id="@+id/xs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="20"
android:focusable="false" > <requestFocus />
</EditText> <EditText
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:digits="1234567890."
android:ems="20"
android:focusable="false"
android:numeric="decimal"
android:singleLine="true" /> <TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2,3" > <TableRow> <Button
android:id="@+id/btn_1"
android:text="7" /> <Button
android:id="@+id/btn_2"
android:text="8" /> <Button
android:id="@+id/btn_3"
android:text="9" />
</TableRow> <TableRow> <Button
android:id="@+id/btn_4"
android:text="4" /> <Button
android:id="@+id/btn_5"
android:text="5" /> <Button
android:id="@+id/btn_6"
android:text="6" />
</TableRow> <TableRow> <Button
android:id="@+id/btn_7"
android:text="1" /> <Button
android:id="@+id/btn_8"
android:text="2" /> <Button
android:id="@+id/btn_9"
android:text="3" />
</TableRow> <TableRow> <Button
android:id="@+id/btn_dot"
android:text="." /> <Button
android:id="@+id/btn_0"
android:text="0" /> <Button
android:id="@+id/btn_10"
android:text="-" />
</TableRow>
</TableLayout> <TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <Button
android:id="@+id/start"
android:layout_width="105.0dip"
android:layout_height="50.0dip"
android:text="开始" /> <Button
android:id="@+id/btn_eq"
android:layout_width="105.0dip"
android:layout_height="50.0dip"
android:text="确定" /> <Button
android:id="@+id/btn_clear"
android:layout_width="105.0dip"
android:layout_height="50.0dip"
android:text="清除" /> </TableRow> <TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" /> </TableRow> <TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" /> </TableRow> <TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <Button
android:id="@+id/music1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放" /> <Button
android:id="@+id/music3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停" /> <Button
android:id="@+id/music2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止" /> </TableRow>
</TableLayout> </LinearLayout>

fragment_main

 package com.example.add;
import java.util.Stack;
public class opt
{
public float Math(String str) throws Exception
{
String[] operater =new String[20];
String[] number = new String[20];
Stack countStack1 = new Stack();
Stack countStack2 = new Stack();
float result =0;
float x=0;
number = str.split("\\/|\\*|\\+|\\-");
operater= str.split("\\d+");
if(number.length<2)throw new Exception("请输入两位计算数以上!");
for(int i = 0; i<number.length;i++)
{
try
{
x=Float.parseFloat(number[i]);
}
catch(NumberFormatException e)
{
throw new Exception("输入错误!请重新输入!");
}
if(operater[i].equals("/"))if(x==0)throw new Exception("除数不能为0!");
countStack1.push(number[i]);
if(i!=number.length-1)
{
if(operater[i+1]!=null)countStack1.push(operater[i+1]);
}
}
while(!countStack1.isEmpty())countStack2.push(countStack1.pop());
String op;
while(!countStack2.isEmpty())
{
result=0;
op=countStack2.pop().toString();
if(op.equals("*"))
{
result=Float.parseFloat(countStack1.pop().toString())*Float.parseFloat(countStack2.pop().toString());
if(number.length==2)return result;
countStack1.push(result);
continue;
}
if(op.equals("/"))
{
result=Float.parseFloat(countStack1.pop().toString())/Float.parseFloat(countStack2.pop().toString());
if(number.length==2)return result;
countStack1.push(result);
continue;
}
countStack1.push(op);
}
while(!countStack1.isEmpty())countStack2.push(countStack1.pop());
while(!countStack2.isEmpty())
{
result=0;
op = countStack2.pop().toString();
if(op.equals("+"))
{
result=Float.parseFloat(countStack1.pop().toString())+Float.parseFloat(countStack2.pop().toString());
countStack1.push(result);
continue;
}
if(op.equals("-"))
{
result=Float.parseFloat(countStack1.pop().toString())-Float.parseFloat(countStack2.pop().toString());
countStack1.push(result);
continue;
}
countStack1.push(op);
}
return result;
}
}

opt

音乐文件就不发啦,只要自行丢一个MP3文件到res/raw里,改名为unity或者到代码里更改文件名都可

PS:送上背景图片~

四则运算安卓版ver.mk3

四则运算安卓版ver.mk3的更多相关文章

  1. 四则运算安卓版ver&period;mk2

    做了一点微小的变动....非常微小...不过美观点了就是...那就是----加背景! 变更后的部分代码: <LinearLayout xmlns:android="http://sch ...

  2. 实验二 四则运算 完成版 ver&period;1

    package size; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JMenuBar; im ...

  3. 微信5&period;4安卓版重回ios风格 导航菜单都放底栏位置

    微信5.4安卓版发布更新了,由于本人的手机设置软件自动更新,中午的时候才发现微信换成了5.4版本,启动微信后是一个大大的“转账,就是发消息”,进入微信界面有点小惊喜,导航菜单都改为底部tab方式,顶部 ...

  4. 结对编程项目——四则运算vs版

    结对编程项目--四则运算vs版 1)小伙伴信息:        学号:130201238 赵莹        博客地址:点我进入 小伙伴的博客 2)实现的功能: 实现带有用户界面的四则运算:将原只能在 ...

  5. 浩瀚技术 安卓版移动开单手持微POS PDA无线移动开单软件 -安卓版移动手持开单设备

    PDA数据采集器,是深圳浩瀚技术有限公司最新研发的一款安卓版移动手持开单设备,它通过WIFI和GPRS连接并访问电脑,从进销存软件中读取数据,实现移动开单,打破电脑开单模式. 它自带扫描器,可直接扫描 ...

  6. 中国首个 SaaS 模式的云告警平台安卓版 APP 上线

    今年一月底,国内首个 SaaS 模式的云告警平台 OneAlert 正式发布了 iOS 版 App 客户端,今天上午,安卓版 App 客户端也正式上线了!每个安卓用户,无需电脑,都可以通过手机全程跟踪 ...

  7. Facebook 开源安卓版 React Native,开发者可将相同代码用于网页和 iOS 应用开发

    转自:http://mt.sohu.com/20150915/n421177212.shtml Facebook 创建了React Java 库,这样,Facebook 的工程团队就可以用相同的代码给 ...

  8. 安卓版php服务器的mysql数据库增删改查简单案例

    界面: index.php文件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  9. 2017年05月10日记一次微项目投产 &vert; 安卓版微信内置浏览器不能解析gzip压缩过的mp4视频的问题

    前言 今天投产了一个小项目,一个很简单的H5,有播放视频功能,使用了videojs插件. 之前也做过数个视频播放,视频的转压都按照既定流程进行,文件放到FTP后,iphone和安卓机测试下来都没有问题 ...

随机推荐

  1. Rectangle Area &vert;&vert; LeetCode

    把交叉点的坐标求出来即可. #define max(a,b) ( (a)>(b)?(a):(b) ) #define min(a,b) ( (a)<(b)?(a):(b) ) int co ...

  2. postInvalidate、removeAllViewsInLayout、refreshDrawableState用法

    postInvalidate.invalidate:会调用控件的onDraw()重绘控件 refreshDrawableState:当控件在使用一个对控件状态敏感的Drawable对象时使用,如一个B ...

  3. 分布式消息系统jafka快速起步(转)

    Jafka 是一个开源的/性能良好的分布式消息系统.在上一篇文章中有所简单介绍.下面是一篇简单的入门文档.更多详细的文档参考wiki. Step 1: 下载最新的安装包 完整的安装指南在这里.最新的发 ...

  4. HttpWeb服务器之--用OO方式写

    虽然写的不是很好,但 最终解释权以及版权归13东倍所有! package com.web; import java.io.IOException; public class Test { public ...

  5. git权限

    itHub--创建新的分支(转) 如何在 GitHub 的项目中创建一个分支呢? 其实很简单啦,直接点击 Branch,然后在弹出的文本框中添加自己的 Branch Name 然后点击蓝色的Creat ...

  6. C&num; System&period;IO&period;FileStream

    为文件提供 Stream,既支持同步读写操作,也支持异步读写操作. using System; using System.IO; using System.Text; class Test { pub ...

  7. IE10、IE11使用 &lowbar;&lowbar;doPostBack 出现未定义问题

    在公司的老项目中分页控件使用了 __doPostBack 方式,在IE兼容模式下正常,在IE10.IE11中 __doPostBack 出现未定义问题. 百度查阅资料得知,这是微软NET环境下的一个B ...

  8. bzoj3672【NOI2014】购票

    题目描述   今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会.        全国的城市构成了一棵以SZ市为根的有根树,每个城市与 ...

  9. java二维码工具类,中间带LOGO的,很强大

    jar包下载maven 配置: Xml代码 收藏代码 <dependency> <groupId>com.google.zxing</groupId> <ar ...

  10. liunx工具学习之taskset

    当你优化多线程任务的时候,发现某个线程在不同的核上跳转较大,从而耗费CPU的时候想法肯定是想可以把对应线程绑定到特定的核上,可是每次这样操作每次尝试看效果的时候都要重启进程,那有没有一个工具可以直接处 ...