Java 循环执行,而
我试着做一个循环,但是“{”符号阻止了所有打印行的重复,我想在用户输入完序列值后,循环会重新显示整个第一个字符串Java 循环执行,而,java,loops,do-while,Java,Loops,Do While,我试着做一个循环,但是“{”符号阻止了所有打印行的重复,我想在用户输入完序列值后,循环会重新显示整个第一个字符串 import java.util.Scanner; public class FtoC{ static Scanner cin = new Scanner (System.in); public static void main(String[] args) { char userInput = ' '; System.o
import java.util.Scanner;
public class FtoC{
static Scanner cin = new Scanner (System.in);
public static void main(String[] args)
{
char userInput = ' ';
System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius"
+ "\nEnter " + "\"C\"" + " or " + "\"c\"" + " for Celsius to Fahrenheit"
+ "\nEnter something else to quit." +"\n");
userInput = cin.next().charAt(0);
if ((userInput == 'F') || (userInput =='f'))
{print("Enter a temp in Fahrenheit"
+"\n I will tell you temp in Celsius" +"\n");
far2cel();
}
else if ((userInput == 'C') || (userInput =='c')) {
print("Enter a temp in Celsius:"
+"\n I will tell you temp in fahrenheit" +"\n");
cel2far();
} else {
print("Terminating the program" + "\n");
}
}
private static void cel2far() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
print(celsius + " celsius is " + ((celsius * 9 / 5.0) + 32)
+ " Fahrenheit");
}
private static void far2cel() {
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5 / 9.0))
+ " celsius");
}
private static void print(String string) {
System.out.print("\n" + string);
}
}
我不认为我得到了你想要的,但这样你可以运行程序,直到用户在程序的第一部分输入不同于“F”、“F”、“C”、“C”的内容
import java.util.Scanner;
public class FtoC{
static Scanner cin = new Scanner (System.in);
public static void main(String[] args)
{
while(true) {
char userInput = ' ';
System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius"
+ "\nEnter " + "\"C\"" + " or " + "\"c\"" + " for Celsius to Fahrenheit"
+ "\nEnter something else to quit." +"\n");
userInput = cin.next().charAt(0);
if ((userInput == 'F') || (userInput =='f'))
{print("Enter a temp in Fahrenheit"
+"\n I will tell you temp in Celsius" +"\n");
far2cel();
}
else if ((userInput == 'C') || (userInput =='c')) {
print("Enter a temp in Celsius:"
+"\n I will tell you temp in fahrenheit" +"\n");
cel2far();
} else {
print("Terminating the program" + "\n");
break;
}
}
}
private static void cel2far() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
print(celsius + " celsius is " + ((celsius * 9 / 5.0) + 32)
+ " Fahrenheit");
}
private static void far2cel() {
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5 / 9.0))
+ " celsius");
}
private static void print(String string) {
System.out.print("\n" + string);
}
}
我已将其放入while循环中,并在“终止程序”部分添加了“break;”。我没有看到循环。您可以编辑此内容以发布您尝试过的循环代码吗?您是认真地询问代码中没有单个循环的do-while循环吗?请尝试以下操作:“{”符号防止所有打印行重复”呃,不。是时候阅读旧的教程了。我想帮你找到你尝试过的方法的问题。为了让我更容易,你能用你尝试过的循环来发布这个东西,而不是其他的代码吗?如果你要在每个方法中实例化一个
扫描仪输入
,为什么cin
{你用过符号吗?谢谢。有时候最简单的解决方案就是最好的。我一直在尝试一个“做一段时间”的语句,但那不起作用。没问题,我很高兴它帮了你的忙!谢谢你选择我的最佳答案,我真的很感激。