Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java while(scan.nextLine()!=“$”)_Java_Loops_While Loop - Fatal编程技术网

java while(scan.nextLine()!=“$”)

java while(scan.nextLine()!=“$”),java,loops,while-loop,Java,Loops,While Loop,基本上,我试图做一个while循环,并一直读取,直到行输入为$。读取时,循环应该退出 但是,我得到一个运行时异常,即java.util.InputMismatchException 代码如下: while (scan.nextLine() != "$") { temp1 = scan.nextInt(); temp2 = scan.nextInt(); addEdge(temp1, temp2); } 首先,使用!等于而不是= 至于例外情况,您是否可能在nextInt调

基本上,我试图做一个while循环,并一直读取,直到行输入为$。读取时,循环应该退出

但是,我得到一个运行时异常,即java.util.InputMismatchException

代码如下:

while (scan.nextLine() != "$") {
    temp1 = scan.nextInt();
    temp2 = scan.nextInt();
    addEdge(temp1, temp2);
}
首先,使用!等于而不是= 至于例外情况,您是否可能在nextInt调用中输入$1?

首先,使用!等于而不是= 至于例外情况,您是否可能在nextInt调用中输入$n?

请尝试以下操作:

while (!"$".equals(scan.nextLine()))
请尝试以下方法:

while (!"$".equals(scan.nextLine()))

您需要使用while条件中读取的行

当你打电话给nextLine时,你不仅要检查它是否等于美元,而且还要把它扔掉

我假设你正在阅读如下输入:我打赌你是

1 2
3 4
0 1
4 5
$
请尝试以下操作:

String nextLineStr;
while( !((nextLinestr = scan.nextLine()).equals("$")))
{
String tokens [] = nextLineStr.split(" ");
temp1 = Integer.parseInt(tokens[0]);
temp2 = Integer.parseInt(tokens[1]);
addEdge(temp1,temp2);
}

作为旁注:注意,我把你的“!=”换掉了对于notequals方法,这是因为不应使用==或=

您需要使用while条件中读取的行

当你打电话给nextLine时,你不仅要检查它是否等于美元,而且还要把它扔掉

我假设你正在阅读如下输入:我打赌你是

1 2
3 4
0 1
4 5
$
请尝试以下操作:

String nextLineStr;
while( !((nextLinestr = scan.nextLine()).equals("$")))
{
String tokens [] = nextLineStr.split(" ");
temp1 = Integer.parseInt(tokens[0]);
temp2 = Integer.parseInt(tokens[1]);
addEdge(temp1,temp2);
}

作为旁注:注意,我把你的“!=”换掉了对于notequals方法,这是因为不应使用==或=

这两条语句中的一条正在引发异常-

        temp1 = scan.nextInt();
        temp2 = scan.nextInt();

发生的情况是,在调用这些函数时,您正在输入一个非整数值

这两条语句中的一条正在引发异常-

        temp1 = scan.nextInt();
        temp2 = scan.nextInt();

发生的情况是,在调用这些函数时,您正在输入一个非整数值

首先,!=在Java中,不用于字符串或其他对象比较…请稍后再试!scan.nextLine.Equals$您给出的输入字符串是什么?对于初学者,!=在Java中,不用于字符串或其他对象比较…请稍后再试!scan.nextLine.Equals$您给出的输入字符串是什么?以上两点都有效。$不是整数,在比较字符串时需要使用.equals。我建议完全删除nextInt调用,并使用Integer.parseInt将整数转换为字符串。以上两点都是有效的。$不是整数,在比较字符串时需要使用.equals。我建议完全放弃nextInt调用,并使用Integer.parseInt将整数转换为字符串。谢谢。最近我在C++中编码了很多,忘记了java的.Malm方法谢谢。最近我在C++中编码了很多,忘记了java的.Advess方法。