基于android studio Java 计算器 开方代码
时间: 2023-06-28 17:04:10 浏览: 204
以下是基于 Android Studio 的 Java 计算器开方代码:
```java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText etNumber;
Button btnSqrt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNumber = findViewById(R.id.etNumber);
btnSqrt = findViewById(R.id.btnSqrt);
btnSqrt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double number = Double.parseDouble(etNumber.getText().toString());
double result = Math.sqrt(number);
etNumber.setText(String.valueOf(result));
}
});
}
}
```
需要在布局文件中添加一个 EditText 和一个 Button,分别设置其 ID 为 etNumber 和 btnSqrt,然后在 Activity 中进行相应的初始化和监听。当用户点击开方按钮时,程序会将 EditText 中的数字转换为 double 类型并进行开方运算,最后将结果显示在 EditText 中。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)