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

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

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

  1. public class example {
  2. public static void main(String[] args) {
  3. //boolean值为"Tai","John",1999,2000,2001和2002。
  4. boolean r = false;
  5. String name = "Tai";
  6. int year = 1999;
  7. //如果name.equals()的值与boolean值匹配,则布尔值为真。
  8. boolean r = name.equals(year "Tai");
  9. }
  10. }

供参考,这是一个类似我想在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

  1. public class example {
  2. public static void main(String[] args) {
  3. //boolean values are "Tai", "John", 1999, 2000, 2001, and 2002.
  4. boolean r = false;
  5. String name = "Tai";
  6. int year = 1999;
  7. //Boolean gets true if name.equals() values matches boolean values.
  8. boolean r = name.equals(year and "Tai");
  9. }
  10. }

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中,逻辑运算符是 &&

因此你可以这样写:

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

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

英文:

in Java logical and is &&

So you write:

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

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

答案2

得分: 0

  1. 如果必须使用`.equals()`来匹配一个整数和一个字符串那么可以使用`String.valueOf(int)``int`转换为`String`示例
  2. public class example {
  3. public static void main(String[] args) {
  4. boolean r = false;
  5. String name = "Tai";
  6. int year = 1999;
  7. r = name.equals(String.valueOf(year));
  8. }
  9. }
英文:

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

  1. public class example {
  2. public static void main(String[] args) {
  3. boolean r = false;
  4. String name = "Tai";
  5. int year = 1999;
  6. r = name.equals(String.valueOf(year));
  7. }
  8. }

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:

确定