gpt4 book ai didi

java - Java 新手,对名称/变量引用有疑问

转载 作者:行者123 更新时间:2023-12-01 08:51:24 24 4
gpt4 key购买 nike

我已经使用这个网站搜索特定错误两个学期了。我知道我要问的问题比我在该网站上搜索/阅读的大多数内容更广泛和基本,但这是我在类之外必须寻求信息的唯一地方。

现在我正在使用 Android Studio 开发一个简单的照片计算器手机应用程序。它有 3 个单选按钮,每个按钮都分配了一个特定值。我知道我引用每个按钮并尝试触发计算的方式有问题。

当前的一些错误:

    Cannot resolve method'getText()'
Operator '<' cannot be applied to 'android.widget.editText','int'
Operator "*" cannot be applied to 'double', 'andriod.widget.EditText'

下面是代码:

        package com.example.squirreloverlord.ccarringtonphonephotoprint;

import android.icu.text.DecimalFormat;
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.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
double small = 19;
double medium = 49;
double large = 79;
double Result;
double inputUser;
double Number;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);

final EditText inputUser = (EditText) findViewById(R.id.editTextNumber);
final RadioButton radioButton4x6 = (RadioButton) findViewById(R.id.radioButton4x6);
final RadioButton radioButton5x7 = (RadioButton) findViewById(R.id.radioButton5x7);
final RadioButton radioButton8x10 = (RadioButton)findViewById(R.id.radioButton8x10);
final TextView Result = (TextView) findViewById(R.id.textViewResult);
Button Calculate = (Button) findViewById(R.id.buttonCalculate);

Calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputUser=Double.parseDouble(Number.getText( ).toString( ));
DecimalFormat tenth = new DecimalFormat("#.#");

if(radioButton4x6.isChecked( )) {
if (inputUser <50) {
Number = small * inputUser;
Result.setText(tenth.format(Result) + " is your total cost!");

} else {
Toast.makeText(MainActivity.this,"Please enter a value less than 50", Toast.LENGTH_LONG).show();

}
}
if (radioButton5x7.isChecked( )) {
if (inputUser <50) {
Number = medium * inputUser;
Result.setText(tenth.format(Result) + " is your total cost!");
} else {
Toast.makeText(MainActivity.this,"Please enter a value less than 50", Toast.LENGTH_LONG).show();
}
}
if (radioButton8x10.isChecked()) {
if (inputUser <50) {
Number = large * inputUser;
Result.setText(tenth.format(Result) + " is your total cost!");

} else {
Toast.makeText(MainActivity.this,"Please enter a value less than 50", Toast.LENGTH_LONG).show();
}
}
}
});

}
}

预先感谢您提供的任何帮助。

最佳答案

我认为你应该像这样更改变量:

double inputUser;

final EditText inputUser = (EditText) findViewById(R.id.editTextNumber);

inputUser 会对这段代码感到困惑

inputUser=Double.parseDouble(Number.getText( ).toString(  ));

如果我是你,我会这样改变:

final EditText editUser= (EditText) findViewById(R.id.editTextNumber);

editUser=Double.parseDouble(editUser.getText( ).toString( ));

然后喜欢代码editUser <50它将获得值

希望有帮助。

根据你的 double 印,改变如下:

prints=Double.parseDouble(editUser.getText( ).toString(  ));

那么你可以这样使用:

if (prints<50)

关于java - Java 新手,对名称/变量引用有疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42381779/

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