How to put both an int variable and String Variable inside .equals() that matches with a Boolean variable?

huangapple go评论74阅读模式
英文:

How to put both an int variable and String Variable inside .equals() that matches with a Boolean variable?

问题

以下是翻译好的内容:

我是Java的新手。

如何将int year = 1999int变量和String name = "Tai"String变量放在name.equals();内部,以便如果intString的值与boolean r的值匹配,则boolean r变为true

boolean r的值为"Tai","John",1999,2000,2001和2002。

我必须使用.equals();,不能使用任何其他替代方法。

Java

public class example {
  public static void main(String[] args) {

   //boolean值为"Tai","John",1999,2000,2001和2002。
   boolean r = false;

   String name = "Tai";

   int year = 1999;
   
   //如果name.equals()的值与boolean值匹配,则布尔值为真。  
   boolean r = name.equals(year 和 "Tai");


  }
}

供参考,这是一个类似我想在Java中使用.equals();实现的链接:

https://stackoverflow.com/questions/32643638/boolean-variables-and-if-statement-fails-for-true-statements

英文:

I am new to Java.

How to put the int variable of int year = 1999 and the String variable of String name = "Tai" together inside name.equals(); so that boolean r gets true if the int and String values match boolean r values?

The boolean r values are "Tai", "John", 1999, 2000, 2001, and 2002.

I must use .equals(); and I cannot use any other alternative.

Java

public class example {
  public static void main(String[] args) {

   //boolean values are "Tai", "John", 1999, 2000, 2001, and 2002.
   boolean r = false;

   String name = "Tai";

   int year = 1999;
   
   //Boolean gets true if name.equals() values matches boolean values.  
   boolean r = name.equals(year and "Tai");


  }
}

For reference, here is a link of something similar I want to achieve but in Java with .equals();:

https://stackoverflow.com/questions/32643638/boolean-variables-and-if-statement-fails-for-true-statements

答案1

得分: 2

在Java中,逻辑运算符是 &&

因此你可以这样写:

boolean r = name.equals("Tai") && year == 1999;

需要注意的是,在Java中基本数据类型使用 == 进行比较,而对象使用 .equals() 方法进行比较。

英文:

in Java logical and is &&

So you write:

boolean r = name.equals("Tai") && year == 1999;

Note that primitives in java compares through ==, while objects through .equals()

答案2

得分: 0

如果必须使用`.equals()`来匹配一个整数和一个字符串那么可以使用`String.valueOf(int)``int`转换为`String`。示例

    public class example {
      public static void main(String[] args) {
    
       boolean r = false;
       String name = "Tai";
       int year = 1999;
       
       r = name.equals(String.valueOf(year));
      }
    }
英文:

If you must use .equals() to match an integer and a string then, convert the int to String using String.valueOf(int). Example :

public class example {
  public static void main(String[] args) {

   boolean r = false;
   String name = "Tai";
   int year = 1999;
   
   r = name.equals(String.valueOf(year));
  }
}

huangapple
  • 本文由 发表于 2020年9月21日 11:30:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63985830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定