重复温度编程项目,添加允许的循环 用户继续进入温度,直到他们输入Q或 q退出程序的那一部分。用户应输入Q或q 退出并返回一个空字符串以继续。 (任何其他条目应该导致 要重复的问题)如果用户输入不同的字母 对于温度,用F或C(大写或小写),打印一个 错误消息并要求用户输入正确的温标 没有要求新的数值。
这就是我到目前为止......我无法理解它们是否输入65D而不是65C。如果他们输入D代替F / f或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("");
}
答案 0 :(得分:1)
只需在您的do-while循环内移动... = keyboard.next()
,即可在您循环播放时读取新的用户输入。此外,您应该删除当前正在阻止的break;
语句循环要完成。
答案 1 :(得分:0)
请考虑以下其他方法。
package lala;
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();
System.out.println(temp);
String degreesAsString = deg(temp);
System.out.println(degreesAsString);
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.equals("q") || temp.equals("Q")){
} else{
System.out.println("ERROR: Please enter either 'F/f or C/c'.");
}
System.out.println("\nProvide new temperature:");
input2=keyboard.next();
} while(!input2.equals(""));
System.out.println("");
}
public static String deg(String s){
if(s==null || "".equals(s)) return "";
StringBuilder sb = new StringBuilder();
for(int i=0;i<s.length();i++){
if(Character.isDigit(s.charAt(i))) sb.append(s.charAt(i));
}
System.out.println(sb.toString());
return sb.toString();
}
}