在要求用户输入变量后,无法解析为变量 [英] Cannot be resolved to a variable, after asking user to input variable

查看:153
本文介绍了在要求用户输入变量后,无法解析为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是显而易见的事情,但是当我试图强制用户继续输入值直到他输入有效值时,我得到一个错误,该变量无法解析。

It's probably something obvious, but when I'm trying to force the user to keep entering a value until he enters a valid value, I get an error that the variable cannot be resolved.

然后,如果我事先声明它,我会收到错误的重复值。

Then if I declare it beforehand I get an error saying duplicate value.

do {
    float x = Float.parseFloat(javax.swing.JOptionPane.showInputDialog("Enter 1");
} while (x != 1);

编译器不应该先执行 do 语句,然后再担心

Isn't the compiler supposed to execute the do statement first, before worrying about the while?

推荐答案

在循环之前声明 x ,使其范围更加如下所示:

declare x before the loop, make its scope more broder like below :

float x = 0.0f;
do {
    x = Float.parseFloat(javax.swing.JOptionPane.showInputDialog("Enter 1"); 
} while (x != 1);

这篇关于在要求用户输入变量后,无法解析为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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