Java If语句不是';没有人读
这就是if语句的编写方式Java If语句不是';没有人读,java,Java,这就是if语句的编写方式 //Check and see if current is less than the next character. if (node.thisChar().charAt(0) < current.thisChar().charAt(0) ) { temp = current; previous = node;
//Check and see if current is less than the next character.
if (node.thisChar().charAt(0) < current.thisChar().charAt(0) )
{
temp = current;
previous = node;
node.next = current;
System.out.println("Its in here");
break;
}// end if
previous = current;
current = current.next;
//what was empty at the end of the list now has a new node being linked.
System.out.println(current.thisChar().charAt(0) + " This is the current");
System.out.println(node.thisChar().charAt(0) + " This is the new character");
System.out.println("Character " + node.thisChar() + " has been added");
//检查当前字符是否小于下一个字符。
if(node.thisChar().charAt(0)
问题在于突出显示的if语句。例如,如果节点字母
'd'
小于当前字母'f'
,则If语句将被跳过,从而生成未排序的列表。永远不会到达if语句中的输出。奇怪的是,我的输出如何显示带有thisChar().CharAt()
的字符(thisChar
仅返回字符,类型为String
);表明这些陈述是有效的。除非问题在于变量的类型是String
,否则我不知道if语句发生了什么。如有任何建议,我们将不胜感激。您可以选择与该方法进行比较
string.compareTo(String)
范例
String s1, s2;
//blabla
//giving string variables values
s1.compareTo(s2)
有关更多信息,请参见此项。小问题和如此庞大的代码已发布如果“节点”小于“当前”,则您的条件为真。如果您想在本例中跳过它,则必须反转条件。将三条sysout语句复制到If语句之前,看看是否有任何帮助。我不理解您的问题,因为charAt(X)返回指定索引X处的char值(而不是返回字符串),因此如果您比较两个char值,与比较两个整数值相同。我认为错误在别处。我想你的意思是
compare to
而不是compare
,我会看看这是否有效,但这与我在if语句中所说的是一样的。我的System.Out确实准确地打印出了输入的字母,这让我觉得if语句进行比较的方式有问题。希望这能奏效