Java中的百分比函数

时间:2018-10-28 12:54:41

标签: java android xml android-studio math

我是11岁的应用程序程序员,正在制作计算器应用程序。我想添加一个百分比按钮,以便用户可以找到percantage,但是当我输入100的50%时,即使正确的答案是50,它也会给我0。对于其他方程式,它也说错了答案。请帮忙!这是我的Java代码:

package com.example.lenovouser.calculatoraghav;
//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;

public class MainActivity extends AppCompatActivity {
// ASSIGNING THE NAMES OR VARIABLES
TextView result;
EditText number1, number2, txt;
Button 
add,subtract,divide,multiply,squareroot,square,clear,Exponent,percent;
double resultnum;
int num1,num2,num3;

@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);
    txt = (EditText)findViewById(R.id.txt);
    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);



    add.setOnClickListener(new View.OnClickListener(){

@Override
    public void onClick(View v){
        //ADD
        num1 = Integer.parseInt(number1.getText() .toString());
        num2 = Integer.parseInt(number2.getText() .toString());
        resultnum = num1 + num2;
        result.setText(String.valueOf(resultnum));
}
});




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

            num1 = Integer.parseInt(number1.getText() .toString());
            num2 = Integer.parseInt(number2.getText() .toString());
            resultnum = num1 - num2;
            result.setText(String.valueOf(resultnum));
        }
    });




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

            num1 = Integer.parseInt(number1.getText() .toString());
            num2 = Integer.parseInt(number2.getText() .toString());
            resultnum = num1 * num2;
            result.setText(String.valueOf(resultnum));
        }
    });





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

            num1 = Integer.parseInt(number1.getText() .toString());
            num2 = Integer.parseInt(number2.getText() .toString());
            resultnum = num1 / num2;
            result.setText(String.valueOf(resultnum));
        }
    });


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

            num1 = Integer.parseInt(number1.getText() .toString());

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


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

            num1 = Integer.parseInt(txt.getText() .toString());

            resultnum = 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
            int p = Integer.parseInt(number1.getText() .toString());
            int e = Integer.parseInt(number2.getText() .toString());
            resultnum = (Math.pow((double)p, (double)e));
            result.setText(String.valueOf(resultnum));
        }
    });



    percent.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //PERCENT
            num1 = Integer.parseInt(number1.getText() .toString());
            num2 = Integer.parseInt(number2.getText() .toString());
            resultnum = num1/num2;
            resultnum = resultnum * 100;
            result.setText(String.valueOf(resultnum));
        }
    });
}}

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
    android:id="@+id/number1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="26dp"
    android:ems="10"
    android:hint="number1"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/number2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="92dp"
    android:ems="10"
    android:hint="number2"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/sum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="15dp"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="24dp"
    android:text="result"
    android:textSize="25sp"
    app:layout_constraintStart_toStartOf="@+id/button"
    app:layout_constraintTop_toBottomOf="@+id/number2" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="18dp"
    android:background="@android:color/holo_blue_bright"
    android:text="ADD-+"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/sum" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="22dp"
    android:background="@android:color/holo_red_light"
    android:text="SUBTRACT--"
    app:layout_constraintStart_toStartOf="@+id/button"
    app:layout_constraintTop_toBottomOf="@+id/button" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="23dp"
    android:background="@android:color/holo_green_dark"
    android:text="MULTIPLY-X"
    app:layout_constraintStart_toStartOf="@+id/button2"
    app:layout_constraintTop_toBottomOf="@+id/button2" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="52dp"
    android:text="DIVIDE-/"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="@+id/button3"
    tools:background="@android:color/holo_orange_light" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="17dp"
    android:layout_marginRight="17dp"
    android:text="Square"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button2" />

<Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="32dp"
    android:text="Square Root"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button2" />

<EditText
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="square number"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button4" />

<Button
    android:id="@+id/button8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="10dp"
    android:text="CLEAR"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/number2" />

<Button
    android:id="@+id/button9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="108dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:text="Exponent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/number2" />

<Button
    android:id="@+id/button10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginBottom="8dp"
    android:text="Percent"
    app:layout_constraintBottom_toTopOf="@+id/txt"
    app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

到目前为止,计算器在加,减等方面的表现都很好。这是困扰我的百分比。这是我的百分比函数:

        percent.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //PERCENT
            num1 = Integer.parseInt(number1.getText() .toString());
            num2 = Integer.parseInt(number2.getText() .toString());
            resultnum = num1/num2;
            resultnum = resultnum * 100;
            result.setText(String.valueOf(resultnum));
        }
    });
}}

5 个答案:

答案 0 :(得分:1)

Ray在正确的轨道上,但是即使使用double也会给您带来奇怪的行为,因为底层系统是基于位的,并且不能完美地表示以10为基数的小数。 如果先乘以100,您将得到整数并得到整数结果。 试试这个:100 * num1 / num2

答案 1 :(得分:1)

整数不是进行数学计算的好选择,请考虑使用Double以获得更高的精度。

这是因为,尽管Integer的范围介于-2,147,483,648 .. 2,147,483,647之间,但它不能采用除整数以外的值。

在Integer数据类型中将5除以100的结果将不是0,05,而是0,因为内部将进行舍入运算。然后,当您将0乘以100时,您仍然会得到0。

答案 2 :(得分:0)

我认为您的主要问题是执行Integer类型的百分比计算。最好使用double,因为在Java中,当将较小的整数除以较大的整数时,将得到0。

尝试替换:

Double percentage = Double.valueOf(number1.getText().toString())
Integer num2 = Integer.parseInt(number2.getText().toString());
Double resultnum = percentage * num2 / 100;

这只是草稿样本。可以肯定的是,最好在Double这样的计算器应用中执行所有计算。

如果您的最终目标是建立一个可靠的计算器,则可以查看更精确的数据类型:BigDecimal

答案 3 :(得分:0)

这是您的问题:

resultnum = num1/num2;

将整数除以整数时,就是在进行整数除法。这意味着您需要在进行计算之前将数字转换为双精度,否则将得到结果的整数部分。当然,0 * 100等于零。

尝试一下:

resultnum = num1/(num2*1.0d)

除以双倍数时,会得到双倍的结果,因此只需要将分频器转换为双倍数即可。

答案 4 :(得分:0)

将一个int数除以另一个int数将得到整个除法的结果。

对于百分比,如果期望百分比在0到100%之间,则结果应始终为0。

执行除法或乘法运算时,应使用float / double变量;)