-3

我正在使用循环语句创建用于减法、乘法、除法和加法的 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();
    }
}

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

4

1 回答 1

0

减法公式是 yx
乘法公式是 y*x
除法公式是 y/x

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

此外,如果您尝试不断等待另一个号码,那么您所写的内容将无法正常工作。您可以拥有用户输入的数字数组并在最后进行计算。

于 2013-02-01T20:09:04.757 回答