Java计算器中需要十进制

时间:2018-11-04 17:15:47

标签: java android android-studio math calculator

我正在使此计算器正常工作,但用户无法输入十进制数字。我不知道如何允许用户执行此操作。请帮忙!

package com.example.lenovouser.calculator123;
//IMPORT
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
// ASSIGNING THE NAMES OR VARIABLES
TextView result, textView8;
EditText number1, number2;
Button add,subtract,divide,multiply,squareroot,square,cube,clear,Exponent,percent,sind, cosd, tand;
ToggleButton switch2, switch3;
double resultnum = 0.0;
float num1 = 0, num2 = 0, num3 = 0, p = 0, e = 0;`
boolean radiant = false, degree = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //ASSIGNING THE VARIABLES TO THE ANDROID WIDGETS.
    result = (TextView)findViewById(R.id.sum);
    number1 = (EditText)findViewById(R.id.number1);
    number2 = (EditText)findViewById(R.id.number2);
    add = (Button)findViewById(R.id.button);
    subtract = (Button)findViewById(R.id.button2);
    multiply = (Button)findViewById(R.id.button3);
    divide = (Button)findViewById(R.id.button4);
    squareroot =(Button)findViewById(R.id.button6);
    square = (Button)findViewById(R.id.button5);
    clear = (Button)findViewById(R.id.button8);
    Exponent = (Button)findViewById(R.id.button9);
    percent = (Button)findViewById(R.id.button10);
    sind = (Button)findViewById(R.id.button18);
    cosd = (Button)findViewById(R.id.button19);
    tand = (Button)findViewById(R.id.button20);
    switch2 = (ToggleButton) findViewById(R.id.switch2);
    switch3 = (ToggleButton) findViewById(R.id.switch3);
    textView8 = (TextView)findViewById(R.id.textView8);
    cube = (Button)findViewById(R.id.cube);

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //ADD

            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());

            if (num1 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }

            if (num2 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }


            resultnum = num1 + num2;
            result.setText(String.valueOf(resultnum));
        }
    });




    subtract.setOnClickListener(new View.OnClickListener(){
        //SUBTRACT
        @Override
        public void onClick(View v){

            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());

            if (num1 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }

            if (num2 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }


            resultnum = num1 - num2;
            result.setText(String.valueOf(resultnum));
        }
    });




    multiply.setOnClickListener(new View.OnClickListener(){
        //MULTIPLY
        @Override
        public void onClick(View v){

            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }

            if (num2 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }

            resultnum = num1 * num2;
            result.setText(String.valueOf(resultnum));
        }
    });





    divide.setOnClickListener(new View.OnClickListener(){
        //DIVIDE
        @Override
        public void onClick(View v){

            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }

            if (num2 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }

            resultnum = num1 / num2;
            result.setText(String.valueOf(resultnum));
        }
    });


    squareroot.setOnClickListener(new View.OnClickListener(){
        //SQUAREROOT
        @Override
        public void onClick(View v){

            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());

            if (num1 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }




            resultnum = (Math.sqrt(num1));
            result.setText(String.valueOf(resultnum));
        }
    });


    square.setOnClickListener(new View.OnClickListener(){
        //SQUARE
        @Override
        public void onClick(View v){
            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());

            if (num1 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }




            resultnum = num1*num1;
            result.setText(String.valueOf(resultnum));
        }
    });


    cube.setOnClickListener(new View.OnClickListener(){
        //SQUARE
        @Override
        public void onClick(View v){

            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());

            if (num1 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }



            resultnum = num1*num1*num1;
            result.setText(String.valueOf(resultnum));
        }
    });


    clear.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            //CLEAR
            num1 = (0);
            num2 = (0);
            num3 = (0);
            number1.setText("");
            number2.setText("");
            //txt.setText("");
            resultnum = 0;
            result.setText(String.valueOf(resultnum));
        }
    });


    Exponent.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //EXPONENT

            if (number1.getText().length() != 0) {
                p = Float.parseFloat(number1.getText().toString());
            }
            if (number2.getText().length() != 0) {
                e = Float.parseFloat(number2.getText().toString());
            }
            if (p == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }
            if (e == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }
            resultnum = (Math.pow(p, e));
            p = 0;
            e = 0;
            result.setText(String.valueOf(resultnum));
        }
    });



    percent.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //PERCENT
            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a number",Toast.LENGTH_SHORT).show();
                return;
            }

            if (num2 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }
            num1 = Float.parseFloat(number1.getText().toString());
            num2 = Integer.parseInt(number2.getText().toString());
            float resultnum = num1 * num2 / 100;
            result.setText(String.valueOf(resultnum));
        }
    });




    sind.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //SIN num2
            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }


            if (degree)
            {
                num1 = Integer.parseInt(number1.getText().toString());
                //num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.sin(Math.toDegrees(num1));
                result.setText(String.valueOf(resultnum));
            }

            if (radiant){
                num1 = Integer.parseInt(number1.getText().toString());
                //num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.sin(Math.toRadians(num1));
                result.setText(String.valueOf(resultnum));

                if (!degree)
                {

                }

                if (!radiant){

                }
            }
        }
    });


    cosd.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }


            //COS num2
            if (degree)
            {
                num1 = Integer.parseInt(number1.getText().toString());
                // num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.cos(Math.toDegrees(num1));
                result.setText(String.valueOf(resultnum));
            }

            if (radiant){
                num1 = Integer.parseInt(number1.getText().toString());
                //num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.cos(Math.toRadians(num1));
                result.setText(String.valueOf(resultnum));

            }

            if (!degree)
            {
                Toast.makeText(getApplicationContext(),"Please choose degree 
or radiant",Toast.LENGTH_SHORT).show();
                return;
            }

            if (!radiant){
                Toast.makeText(getApplicationContext(),"Please choose degree 
or radiant",Toast.LENGTH_SHORT).show();
                return;
            }
        }

    });


    tand.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());

            //TAN num2
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),"Please enter a 
number",Toast.LENGTH_SHORT).show();
                return;
            }


            if (degree)
            {
                num1 = Integer.parseInt(number1.getText().toString());
                //num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.tan(Math.toDegrees(num1));
                result.setText(String.valueOf(resultnum));
            }

            if (radiant){
                num1 = Integer.parseInt(number1.getText().toString());
                // num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.tan(Math.toRadians(num1));
                result.setText(String.valueOf(resultnum));

            }



            if (!degree)
            {
                Toast.makeText(getApplicationContext(),"Please choose degree 
or radiant",Toast.LENGTH_SHORT).show();
                return;
            }

            if (!radiant){
                Toast.makeText(getApplicationContext(),"Please choose degree 
or radiant",Toast.LENGTH_SHORT).show();
                return;
            }
        }
    });


    switch2.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //switchR
            radiant = true;
            degree = false;

            textView8.setText(("Radians MODE"));
        }
    });

    switch3.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //switchD
            degree = true;
            radiant = false;
            textView8.setText(("Degree MODE"));
        }
    });


}}

计算器工作完美,并且一切都在其中,仅是小数。我在网上搜索,尝试了所有网站,但没有找到任何东西。所以,请帮忙!

1 个答案:

答案 0 :(得分:-1)

似乎您使用的是Floats而不是double / BigDecimal(了解两者,然后选择一个对您更有用)