结对编程-四则运算

   结对对象:马巍乃(学号:2013110426)博客地址:http://www.cnblogs.com/vshow/

结对编程贡献:1:1.

源代码:

MainActivity.java

package com.example.mldemo;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {


private EditText num;
private Button start;
private int num2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num=(EditText) findViewById(R.id.editText1);
start=(Button) findViewById(R.id.button1);

// num2=Integer.parseInt(num.getText().toString());//循环次数





start.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
num2=Integer.parseInt(num.getText().toString());//循环次数
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("data", num2);
startActivity(intent);

}
});


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);

}

}

secondActivity.java

package com.example.mldemo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SecondActivity extends Activity {

private ArrayList<String> operations=new ArrayList<String>();
private ArrayAdapter<String> numadapter;
private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// 取出mainactivity传递过来的数据
Intent intent = getIntent();
int num2 = intent.getIntExtra("data", -1);

lv = (ListView) findViewById(R.id.listView1);
for (int i = 0; i < num2; i++) {
operations.add(operation());
}

numadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, operations);

lv.setAdapter(numadapter);

}

public String operation() {

int randomNum1; // 随机数第一个值
int randomNum2; // 随机数第二个值

String operation = null;
int operator;// 运算符

operator = (int) (Math.random() * 4);
randomNum1 = (int) (Math.random() * 100 + 1);
randomNum2 = (int) (Math.random() * 100 + 1);
// 通过if语句判断
if (operator == 0) {
operation = (randomNum1 + "+" + randomNum2 + "=").toString();
}

if (operator == 1) {
operation = (randomNum1 + "-" + randomNum2 + "=").toString();
}

if (operator == 2) {
operation = (randomNum1 + "*" + randomNum2 + "=").toString();
}

if (operator == 3) {


operation = (randomNum1 + "/" + randomNum2 + "=").toString();

}

return operation;
}

}

通过本次的学习,让我对结对编程的概念有了新的认识和体会,也让我的编码能力更加深了,我感到特别开心,么么哒

posted @ 2016-04-09 13:28  君临天下丶  阅读(168)  评论(0编辑  收藏  举报