当我使用||时在if语句中,android studio告诉我“运算符||不能用于'int','double'” 所以有什么问题?请帮忙 如果代码不好我不要生气我是编程的新手。 这是用Android studio编写的
package com.ftech.kalkulatorzamalumaturu;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Kalkulator extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kalkulator);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public void onOcistiClick(View v) {
EditText e1 = (EditText)findViewById(R.id.e1);
EditText e2 = (EditText)findViewById(R.id.e2);
EditText e3 = (EditText)findViewById(R.id.e3);
EditText e4 = (EditText)findViewById(R.id.e4);
EditText e5 = (EditText)findViewById(R.id.e5);
EditText e6 = (EditText)findViewById(R.id.e6);
TextView t1 = (TextView)findViewById(R.id.tvresult);
TextView t2 = (TextView)findViewById(R.id.pt);
e1.setText("");
e2.setText("");
e3.setText("");
e4.setText("");
e5.setText("");
e6.setText("");
t1.setText("");
t2.setText("");
}
public void onButtonClick(View v) {
EditText e1 = (EditText)findViewById(R.id.e1);
EditText e2 = (EditText)findViewById(R.id.e2);
EditText e3 = (EditText)findViewById(R.id.e3);
EditText e4 = (EditText)findViewById(R.id.e4);
EditText e5 = (EditText)findViewById(R.id.e5);
EditText e6 = (EditText)findViewById(R.id.e6);
TextView t1 = (TextView)findViewById(R.id.tvresult);
TextView t2 = (TextView)findViewById(R.id.pt);
double num1 = Double.parseDouble(e1.getText().toString());
double num2 = Double.parseDouble(e2.getText().toString());
double num3 = Double.parseDouble(e3.getText().toString());
double num4 = Double.parseDouble(e4.getText().toString());
double num5 = Double.parseDouble(e5.getText().toString());
double num6 = Double.parseDouble(e6.getText().toString());
if ( num1 = 0 || num2 = 0) {
t2.setText("Popuni neko polje!");
}else {
double sum = (num1*4) + (num2*5) + (num3*5) + num4 + num5 + num6;
t2.setText("Broj bodova od skole je:");
t1.setText(String.valueOf(sum));
}
}
}
答案 0 :(得分:5)
您需要以相同的方式进行比较,但需要使用double
值。而不是使用0(int
)使用0.0(double
):
if ( n == 0.0 || c== 0.0 ){
System.out.println("What do i write instead of \"?\" ");
}
答案 1 :(得分:1)
equals
运算符仅在Java中运行 booleans 。在您的字符串示例中,您没有将其与字符串一起使用,而是将其与布尔值一起使用,因为boolean
会返回if( n.equals("") || c.equals("") ){
// ^^^^^^^^^^^^----^^^^^^^^^^^^------- booleans
System.out.println(/*...*/);
}
:
==
所以你需要做的就是使用能给你一个布尔值的东西。例如,您的if ( n == 0 || c== 0 ){
// ^^^^^^----^^^^^------- booleans
System.out.println(/*...*/);
}
会:
double d1 = 1.0;
double d2 = 0.0;
if (d1 || d2) {
// ^^^^^^^^------- WON'T WORK, doubles are not booleans
}
你无法做的是期望它将双打转换为布尔值:
dataSample = read.table("sample6.txt", fill = TRUE)
sampleMatrix = as.matrix(dataSample)
sampleVector = as.vector(sampleMatrix)
h = hist(sampleVector, plot=F)
x =c(min(sampleVector, na.rm=T), max(sampleVector, na.rm=T))
ylim = range(0, max(get("density", h), max(density)))
hist(sampleVector, prob = T, col = "lightgreen", xlim = x,
ylim = ylim, main = "Histogram of data sample", xlab = "sample", ylab = "density")
有些语言会将这些价值观强加给布尔人。 Java不会。
答案 2 :(得分:1)
您正在与
进行比较if ( num1 = 0 || num2 = 0)
什么时候应该
if ( num1 == 0 || num2 == 0)
一个=
是分配,而不是比较。
答案 3 :(得分:0)
double i = 1.5;
double j = 3.4;
if((i==4.3) || (j==2.1)){
//to do code here
}
我没有任何错误。