zoukankan      html  css  js  c++  java
  • 结对编程-四则运算

       结对对象:马巍乃(学号: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;
    }

    }

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

  • 相关阅读:
    Linux centos 安装 Node.js
    maven 常用命令
    linux centos 设置笔记本合盖不待机
    linux centos 网卡有关调试
    Linux centos 安装 maven 3.5.4
    Linux centos 安装 jenkins & 本地构建jar & 远程构建jar
    Linux centos 安装 tomcat 7
    Linux centos 安装 JDK 8
    JS正则 replace()方法全局替换变量(可以对变量进行全文替换)
    node:json与csv互转
  • 原文地址:https://www.cnblogs.com/chuanshi123/p/5371471.html
Copyright © 2011-2022 走看看