【发布时间】:2020-02-29 00:00:07
【问题描述】:
我试图只允许用户从给定的输入中进行选择。如果他们为第一个 optionGate 选择了 "f"、"F"、"c" 或 "C" 以外的其他内容,则会提示他们重试并返回扫描仪.下一个门optionGate2也是同样的情况。
通过阻止用户输入与给定选项不同的输入,这些门完成了我希望它们完成的任务。当两个布尔值都转到true 并且通过了大门时,我的问题就出现了。由于某种原因,扫描仪仍然在无限循环中要求用户输入。然后我可以一遍又一遍地在扫描仪中输入任何内容,程序就不再运行了。
对发生的事情有什么建议或建议吗?
Scanner in = new Scanner(System.in);
boolean optionGate = false;
boolean optionGate2 = false;
String tempLabel = ""; //initialize to F
String precipLabel = ""; //initialize to in
while(optionGate != true){
System.out.println("Choose the temperature scale (F = Fahrenheit, C = Celsius:)");
String scaleT = in.nextLine();
if("f".equalsIgnoreCase(scaleT)){
tempLabel = "F";
optionGate = true;
}
else if("c".equalsIgnoreCase(scaleT)){
tempLabel = "C";
optionGate = true;
}
else{
System.out.println("Error: not an option. Try again:");
optionGate = false;
}
}
while(optionGate2 != true){
System.out.println("Choose the precipitation scale (i = inches, c = centimeters:)");
String scaleP = in.nextLine();
if("i".equalsIgnoreCase(scaleP)){
precipLabel = "in";
optionGate2 = true;
}
else if("c".equalsIgnoreCase(scaleP)){
precipLabel = "cm";
optionGate2 = true;
}
else{
System.out.println("Error: not an option. Try again:");
optionGate2 = false;
}
}
编辑:在上面的 sn-p 之后添加代码。 (很抱歉明显缺乏 stackoverflow 知识。这是我在平台上的第一篇文章)。
String [] month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
double [] temperature ={60.9, 62.6, 67.4, 71.5, 77.1, 81.2, 82.4, 82.5, 81.1, 75.3, 68.8, 63.0}; //initialize with Fahrenheit values
double [] precipitation ={2.4, 2.4, 3.5, 2.4, 3.7, 7.4, 7.2, 6.3, 5.8, 2.7, 2.3, 2.3}; //initialize with inch values
double[] updatedTemperature;
updatedTemperature = new double[temperature.length];
double[] updatedPrecipitation;
updatedPrecipitation = new double[precipitation.length];
while("f".equalsIgnoreCase(tempLabel)){
for(int i = 0 ; i < temperature.length ; i++) {
updatedTemperature[i] = temperature[i];
}
if("in".equals(precipLabel)){
for(int i = 0 ; i < precipitation.length ; i++){
updatedPrecipitation[i] = precipitation[i];
}
}
else if("cm".equals(precipLabel)){
for(int i = 0 ; i < precipitation.length ; i++){
updatedPrecipitation[i] = (precipitation[i] * 2.54);
}
}
}
while("c".equalsIgnoreCase(tempLabel)){
for(int i = 0 ; i < temperature.length ; i++) {
updatedTemperature[i] = ((temperature[i] - 32) * (5.0/9));
}
if("in".equals(precipLabel)){
for(int i = 0 ; i < precipitation.length ; i++){
updatedPrecipitation[i] = precipitation[i];
}
}
else if("cm".equals(precipLabel)){
for(int i = 0 ; i < precipitation.length ; i++){
updatedPrecipitation[i] = (precipitation[i] * 2.54);
}
}
}
//Processing - calculate average temperature and total precipitation
for( int index = 0; index < updatedTemperature.length; index++)
{
sum += updatedTemperature[index];
}
avg = sum/updatedTemperature.length;
编辑:这是我的主要方法中的所有代码:
//Declare and initialize variables
Scanner in = new Scanner(System.in);
String city = "Orlando";
String state = "FL";
double sum = 0;
double avg = 0;
boolean optionGate = false;
boolean optionGate2 = false;
String tempLabel = ""; //initialize to F
String precipLabel = ""; //initialize to in
while(optionGate != true){
System.out.println("Choose the temperature scale (F = Fahrenheit, C = Celsius:)");
String scaleT = in.nextLine();
if("f".equalsIgnoreCase(scaleT)){
tempLabel = "F";
optionGate = true;
}
else if("c".equalsIgnoreCase(scaleT)){
tempLabel = "C";
optionGate = true;
}
else{
System.out.println("Error: not an option. Try again:");
optionGate = false;
}
}
while(optionGate2 != true){
System.out.println("Choose the precipitation scale (i = inches, c = centimeters:)");
String scaleP = in.nextLine();
if("i".equalsIgnoreCase(scaleP)){
precipLabel = "in";
optionGate2 = true;
}
else if("c".equalsIgnoreCase(scaleP)){
precipLabel = "cm";
optionGate2 = true;
in.close();
}
else{
System.out.println("Error: not an option. Try again:");
optionGate2 = false;
}
}
String [] month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
double [] temperature ={60.9, 62.6, 67.4, 71.5, 77.1, 81.2, 82.4, 82.5, 81.1, 75.3, 68.8, 63.0}; //initialize with Fahrenheit values
double [] precipitation ={2.4, 2.4, 3.5, 2.4, 3.7, 7.4, 7.2, 6.3, 5.8, 2.7, 2.3, 2.3}; //initialize with inch values
double[] updatedTemperature;
updatedTemperature = new double[temperature.length];
double[] updatedPrecipitation;
updatedPrecipitation = new double[precipitation.length];
while("f".equalsIgnoreCase(tempLabel)){
for(int i = 0 ; i < temperature.length ; i++) {
updatedTemperature[i] = temperature[i];
}
if("in".equals(precipLabel)){
for(int i = 0 ; i < precipitation.length ; i++){
updatedPrecipitation[i] = precipitation[i];
}
}
else if("cm".equals(precipLabel)){
for(int i = 0 ; i < precipitation.length ; i++){
updatedPrecipitation[i] = (precipitation[i] * 2.54);
}
}
}
while("c".equalsIgnoreCase(tempLabel)){
for(int i = 0 ; i < temperature.length ; i++) {
updatedTemperature[i] = ((temperature[i] - 32) * (5.0/9));
}
if("in".equals(precipLabel)){
for(int i = 0 ; i < precipitation.length ; i++){
updatedPrecipitation[i] = precipitation[i];
}
}
else if("cm".equals(precipLabel)){
for(int i = 0 ; i < precipitation.length ; i++){
updatedPrecipitation[i] = (precipitation[i] * 2.54);
}
}
}
//Processing - calculate average temperature and total precipitation
for( int index = 0; index < updatedTemperature.length; index++)
{
sum += updatedTemperature[index];
}
avg = sum/updatedTemperature.length;
//Output: display table of weather data including average and total
System.out.println();
System.out.println(" Weather Data");
System.out.println(" Location: " + city +", " + state);
System.out.println("Month Temperature (" + tempLabel + ") Precipitation (" + precipLabel + ")");
System.out.println();
System.out.println("***************************************************");
for( int index = 0; index < temperature.length; index++)
{
System.out.println(month[index] + "\t " + updatedTemperature[index] + "\t\t " + updatedPrecipitation[index]);
}
System.out.println("***************************************************");
System.out.println("Average: " + avg + " Total: " + sum);
【问题讨论】:
-
无论问题是什么,它似乎都不在您显示的代码部分。之后会发生什么?
-
无法重现。如果我将其复制/粘贴到一个空的
main方法中并运行程序,当两个提示都得到正确回答时,程序就会停止。如果您的程序不断提示您,则它在您显示的代码后面的代码中,我们无法帮助修复看不见的代码中的错误。 -
我建议添加日志并检查控制卡住的行。我非常怀疑问题确实出在您发布的 sn-p 上。
-
如果程序中的其他任何地方都没有使用扫描仪类,那么这个 sn-p 之后的代码是否仍然是问题?
-
已编辑以包含上一个 sn-p 之后的代码。
标签: java loops boolean infinite