温度不正确的用户输入 [英] Temperature incorrect user input
问题描述
重复温度编程项目,添加一个允许
的循环,用户可以继续输入温度,直到它们进入Q或
q以退出程序的该部分。用户应输入一个Q或q
退出,一个空字符串继续。 (任何其他条目应导致
重复该问题)如果用户输入不同的字母
表示温度为F或C(大写或小写)),请打印
错误消息并要求用户输入正确的温度标度
,而不需要新的数值。
Repeat the temperature programming project adding a loop that allows the user to continue entering in temperatures, until they enter a Q or q to exit that portion of the program. The user should enter a Q or q to exit and an empty String to continue. (Any other entry should cause the question to be repeated) If the user enters a different letter that F or C(either upper or lower case) for the temperature, print an error message and ask the user to enter the correct temperature scale without asking for a new numeric value.
这是我有到目前为止....我无法得知它是否进入65D而不是65C。我不知道如果他们输入D而不是F / f或C / c ...如何循环通过代码。
This is what I have so far....I cannot get it to read whether or not they entered 65D instead of 65C. And I can't figure out how to loop it back through the code if they enter D instead of F/f or C/c...
import java.util.Scanner;
public class AssignmentFour {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello, I can convert Fahrenheit to Celsius!");
System.out.println("Please enter the temperature you want converted.");
System.out.println("Followed by: 'C/c' or 'F/f'");
String input2 = keyboard.next();
do{
String temp = input2.trim();
String degreesAsString = temp.substring(0, temp.length()-1);
double degrees = Double.parseDouble(degreesAsString);
if(temp.endsWith("C") || temp.endsWith("c")){
double degreeF = 9 * degrees / 5 + 32;
System.out.println(input2 + " is equal to: ");
System.out.printf("%.2f", degreeF);
System.out.println(" Fahrenheit.");
} else if(temp.endsWith("F") || temp.endsWith("f")){
double degreeC = 5 * (degrees - 32) / 9;
System.out.println(input2 +" is equal to: ");
System.out.printf("%.2f", degreeC);
System.out.println(" Celsius.");
} else if (temp.endsWith(""));{
System.out.println("ERROR: Please enter either 'F/f or C/c'.");
}
break;
} while(!input2.equals(""));
System.out.println("");
}
推荐答案
只需移动你的 ... = keyboard.next()
在你的循环中读取新的用户输入。另外,你应该删除 break ;
语句,目前正在阻止执行任何循环。
just move your ... = keyboard.next()
inside your do-while loop to read new user inputs when you're looping.. Also, you should remove the break;
statement that is currently just preventing from any loop to be done.
这篇关于温度不正确的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!