【发布时间】:2016-12-30 12:30:53
【问题描述】:
我正在尝试将 z 的值打印为输出,但我的代码没有完成执行..它到达“here”行但从未到达最后一行“z is”。
我猜 s = sc.nextInt(); 是问题所在。
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x = 0;
int y = 0;
int z = 0;
int u = 0;
int n = sc.nextInt();
int s = sc.nextInt();
while(sc.hasNextInt()) {
if(s != -1){
y = s;
if(sc.hasNextInt()){
s = sc.nextInt();
}
}
while(s == -1){
x++;
System.out.println("s is "+s);
z = Math.abs(y - x) + u;
System.out.println("s is "+s);
System.out.println("x is " + x+ " y is "+ y+" z is "+z);
if(sc.hasNextInt()){
s = sc.nextInt();
System.out.println("s33 is "+s);
}
}
if(z != 0){
u = z;
}
x = 0;
y = 0;
System.out.println("here");
}
System.out.println("z is" +z);
}
}
谢谢。
【问题讨论】:
-
你调试过这个吗?因为你有两个循环,我猜这里有一个无限循环。但这也可能卡在扫描仪上。或者两者都^^
-
调用
nextInt()函数而不是将值存储在之前的变量中不是一个好主意....在每个nextInt()语句中,程序都希望您输入一个整数。 -
是的,我调试了它,这就是为什么有很多打印语句@AxelH
-
我不明白你在存储它之前存储它是什么意思......我有点初学者所以谢谢你们两个@progy_rock
标签: java