如何检查字符串是否与对象不相等? [英] How do you check if a string is not equal to an object?
问题描述
我想要使用的代码!=符号。但由于我使用String,我怎么不等于Sign。她是我的代码。所以我希望所有这些声明都不相等,所以它可以打印Tie Game。
if (数组[ 0 ] ==数组[currentPlayer]&&数组[ 1 ] ==
数组[currentPlayer]&&数组[ 2 ] ==数组[currentPlayer])
以上代码是指一切都相等的时候。但我希望这些陈述不要相等。
例如,
数组[ 0 ] ==数组[currentPlayer]&&数组[ 1 ] ==数组[currentPlayer]
&&数组[ 2 ] ==数组[currentPlayer]不等于数组[ 0 ] ==数组[currentPlayer]
&&数组[ 1 ] ==数组[currentPlayer]&&数组[ 2 ] ==数组[currentPlayer]
然后打印领带游戏。
请记住我没有使用过Int或Char,我正在使用String。
看看这里:比较字符串和字符串部分 [ ^ ]
正如我在评论中提到的,你必须对数据进行操作,而不是对字符串进行操作。 Java是非常强大的编程语言。它可以创建自定义类,例如
// < span class =code-comment> definition
public class Player {
public String 名称;
public Int Score;
public Player( String n,Int s){
名字= n;
得分= s;
}
public toString(){
return 名称+ 得分为: + Score.toString();
}
}
// 实施:
ArrayList< player> players = new ArrayList< player>();
players.Add( new 播放器( Adam, 50 ));
players.Add( new 播放器( Eve, 55 ));
// 其他内容......
播放器播放器1 = players.get( 0 );
玩家player2 = players.get( 1 );
if (player1.equals(player2)){
// 两个对象在名称和分数上相等
}
你们看看差异?
有关比较Java中对象的更多信息,请参阅: Java编程:比较对象 [ ^ ]
您使用等于
方法。
< pre lang =java> if (!someVariable.equals( Bob))
System.out.println( 不是Bob! 跨度>);
else
System.out.println( 是Bob !!);
查看此链接了解更多详情:
Java - String equals()方法 [ ^ ]
I ave a code where I want to use != sign. But since I am using String, How do I not equals Sign. Hers is my code. So I want all this statement not to equal to each other so it can print Tie Game.
if (Array[0] == Array[currentPlayer] && Array [1] ==
Array[currentPlayer] && Array [2] == Array[currentPlayer])
The above code is when everything equals to each other. But I want this statements not to equal each other.
For example,
Array[0] == Array [currentPlayer] && Array[1] == Array [currentPlayer]
&& Array[2]== Array [currentPlayer] doesnot equal Array[0] == Array [currentPlayer]
&& Array[1] == Array [currentPlayer] && Array[2]== Array[currentPlayer]
then print Tie Game.
Keep in mind that I have not used Int or Char, I am using String.
Have a look here: Comparing Strings and Portions of Strings[^]
As i mentioned in comment, you have to operate on data, not on strings. Java is really powerful programming language. It enables to create custom classes, such as
//definition public class Player{ public String Name; public Int Score; public Player(String n, Int s){ Name = n; Score = s; } public toString() { return Name + "'s score is: " + Score.toString(); } } //Implementation: ArrayList<player> players = new ArrayList<player>(); players.Add(new Player("Adam", 50)); players.Add(new Player("Eve", 55)); //other stuff... Player player1 = players.get(0); Player player2 = players.get(1); if(player1.equals(player2)){ //two objects are equal on Name and Score }
Do you see the difference?
For further information about comparing objects in Java, please see: Java Programming: Comparing Objects[^]
You use theequals
method.
if (!someVariable.equals("Bob")) System.out.println("Is not Bob!"); else System.out.println("Is Bob!!");
Check out this link for more details:
Java - String equals() Method[^]
这篇关于如何检查字符串是否与对象不相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!