public class javapractice14
{
public static void main(String[] args)
{
int largest = Integer.MIN_VALUE;
int smallest = Integer.MAX_VALUE;
Scanner console = new Scanner(System.in);
System.out.println("Please enter desired amount of numbers (-1 ends program)");
for( int number = console.nextInt(); number != -1; number = console.nextInt()); //Enter -1 to end program
{
我收到的错误如下,说无法找到符号:变量号
我相信在for循环中声明了数字,是不是在那里声明了?我已经尝试在循环外声明“ int number; ”,但它以相同的结果结束。我使用:What do I have to do to display the largest and the smallest number using a loop?作为参考,帮助我解决部分问题。
largest = number > largest ? number : largest;
smallest = number < smallest ? number : smallest;
}
System.out.println("Your largest number is: " + largest);
System.out.println("You smallest number is: " + smallest);
}
}
答案 0 :(得分:0)
for( int number = console.nextInt(); number != -1; number = console.nextInt());
&lt; - 看到你的for循环结束时有一个分号,它意味着从那里循环结束
所以你不能从循环外部引用for循环中声明的块变量。这就是你得到编译错误的原因
删除那个分号。然后它必须正常工作
for( int number = console.nextInt(); number != -1; number = console.nextInt()){ // no semicol at the end
// your logic
}
// print your numbers