gpt4 book ai didi

java - java中的百分比函数

转载 作者:行者123 更新时间:2023-11-29 18:41:09 25 4
gpt4 key购买 nike

<分区>

我是一名 11 岁的应用程序程序员,我正在制作一个计算器应用程序。我想添加一个百分比按钮,以便用户可以找到百分比,但是当我输入 100 的 50% 时,它会给我 0,即使正确答案是 50。它还会说其他方程式的错误答案。请帮忙!这是我的 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));
}
});
}}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com