我如何在While循环中使用Java扫描仪 [英] how can i use java scanner in while loop

查看:38
本文介绍了我如何在While循环中使用Java扫描仪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止所拥有的:

This is what I have so far:

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

}

推荐答案

您正在尝试重新声明循环中的变量.您只想给 existing 变量赋予一个不同的值:

You are trying to redeclare the variable inside the loop. You only want to give the existing variable a different value:

while (question != 1) {
    System.out.println("Enter The Correct Number ! ");
    question = sc.nextInt();
}

这只是一个赋值,而不是一个声明.

This is just an assignment rather than a declaration.

这篇关于我如何在While循环中使用Java扫描仪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆