gpt4 book ai didi

java - 我如何在 while 循环中使用 java 扫描器

转载 作者:行者123 更新时间:2023-11-29 05:52:47 26 4
gpt4 key购买 nike

这是我目前所拥有的:

int question = sc.nextInt(); 

while (question!=1){

System.out.println("Enter The Correct Number ! ");

int question = sc.nextInt(); // This is wrong.I mean when user enters wrong number the program should ask user one more time again and again until user enters correct number.
// Its error is : duplicate local variable

}

最佳答案

您在循环外声明 int question,然后在循环内再次声明。

删除循环内的 int 声明。

在 Java 中,变量的范围取决于它在哪个子句中声明。如果您在 try 或 a while 或许多其他子句中声明变量,则该变量对于该子句是局部的。

关于java - 我如何在 while 循环中使用 java 扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13284949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com