如何使用循环语句进行减法,乘法和除法

时间:2013-02-01 19:41:38

标签: android

我用循环语句创建用于减法,乘法,除法和加法的android应用程序

case 1: if user insert= 3 then looping = 3x
case 2: user input 1st looping = 5 then dialog show = 5
case 3: user input 2nd = 10 then dialog show 5+10
case 4: user input 3rd = 90 then dialog show 5+10+90

这是我的添加代码

package com.test.testing;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class arithmatic extends Activity {

     int y = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        final LayoutInflater inflater = this.getLayoutInflater();
        final View v=inflater.inflate(R.layout.dialoginputloop, null);

        new AlertDialog.Builder(arithmatic.this)
       // .setIcon(R.drawable.ic_launcher)
        .setTitle("Operator")
        .setView(v)
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int whichButton) {

              EditText op = (EditText)v.findViewById(R.id.operator);
              int n = Integer.valueOf(op.getText().toString());

            for (int i = 0;i<n;i++){

                LayoutInflater inflater = arithmatic.this.getLayoutInflater();
                final View v2=inflater.inflate(R.layout.dialoginputnumber, null) ;
                new AlertDialog.Builder(arithmatic.this)
                    //.setIcon(R.drawable.ic_launcher)
                    .setTitle("input number")
                    .setView(v2)
                    .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dlg, int sumthin) {

                            EditText number = (EditText)v2.findViewById(R.id.number);
                            String a = number.getText().toString();
                            int x = Integer.valueOf(a);
                            y = y + x; //my formula to addition looping

                            new AlertDialog.Builder(arithmatic.this)
                                .setTitle("result")
                                .setMessage(Integer.toString(y))
                                .setNeutralButton("Close", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dlg, int sumthin) {
                                    }
                                })
                                .show(); 

                        }
                  })
                  .show();

              }

             }

          })

          .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {

                 finish();
               }
           })
           .show();
    }
}

如何为减法,乘法和除法创建公式?

1 个答案:

答案 0 :(得分:0)

减法公式为y-x
乘法公式为y * x
除法公式为y / x

如果你为每个方法制作一个单独的方法,它会更清楚。

此外,如果你试图不断等待另一个数字,那么你写的东西就不会起作用了。您可以拥有用户输入的数字数组,并在最后进行计算。