为什么我的 java 程序不工作?它不允许我输入我的字符串语句比例。
Why isn't my java program working?? It doesn't allow me to input my string statement scale.
这是我编写的代码,用于将华氏度转换为摄氏度,反之亦然。老师说我们必须使用switch语句。
import java.util.Scanner;
public class hope
{
public static void main(String[]args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of degrees: ");
double degrees = keyboard.nextDouble();
System.out.println("Enter the scale: ");
String scale = keyboard.nextLine();
switch(scale)
{
case "F":
case "f":
double fahrenheit = (degrees - 32) * .5556;
System.out.println(degrees + scale + " equals " + fahrenheit + " degrees celcius");
break;
case "C":
case "c":
double celcius = (degrees + 32) * 1.8;
System.out.println(degrees + scale + " equals " + celcius + " degrees Fahrenheit");
break;
default:
System.out.println("I don't know what you're talking about. Chaw. ");
break;
}
}
}
问题是当我 运行 程序允许我输入度数,然后显示打印语句,然后跳转到默认大小写,不允许我输入比例。
您输入的双倍后将被keyboard.nextLine();
消耗
所以你必须再输入keyboard.nextLine();
。
double degrees = keyboard.nextDouble();
keyboard.nextLine();
System.out.println("Enter the scale: ");
String scale = keyboard.nextLine();
您需要消耗新线。 Scanner.nextDouble()
将只读取下一个 token 作为 double,没有行终止符。要么在读完double后加上keyboard.nextLine()
:
double degrees = keyboard.nextDouble();
keyboard.nextLine();
或在输入行上使用Double.parseDouble(String)
:
String degreesString = keyboard.nextLine();
double degrees = Double.parseDouble(degreesString);
如果扫描仪输入实用程序不是强制性的,我会建议使用 BufferedReader。
package com.tempcalculator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
String degrees = getInput("Degrees?");
String scale = getInput("Scale?");
switch (scale.charAt(0)) {
case 'F':
case 'f':
double fahrenheit = (Double.parseDouble(degrees) - 32) * .5556;
System.out.println(degrees + scale + " equals " + fahrenheit + " degrees Celsius");
break;
case 'C':
case 'c':
double celsius = (Double.parseDouble(degrees) + 32) * 1.8;
System.out.println(degrees + scale + " equals " + celsius + " degrees Fahrenheit");
break;
default:
System.out.println("Whoops! Error 404: Requested scale was not found. ");
break;
}
}
public static String getInput(String prompt) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return in.readLine();
} catch (IOException e) {
return "Error: " + e.getMessage();
}
}
}
这是我编写的代码,用于将华氏度转换为摄氏度,反之亦然。老师说我们必须使用switch语句。
import java.util.Scanner;
public class hope
{
public static void main(String[]args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of degrees: ");
double degrees = keyboard.nextDouble();
System.out.println("Enter the scale: ");
String scale = keyboard.nextLine();
switch(scale)
{
case "F":
case "f":
double fahrenheit = (degrees - 32) * .5556;
System.out.println(degrees + scale + " equals " + fahrenheit + " degrees celcius");
break;
case "C":
case "c":
double celcius = (degrees + 32) * 1.8;
System.out.println(degrees + scale + " equals " + celcius + " degrees Fahrenheit");
break;
default:
System.out.println("I don't know what you're talking about. Chaw. ");
break;
}
}
}
问题是当我 运行 程序允许我输入度数,然后显示打印语句,然后跳转到默认大小写,不允许我输入比例。
您输入的双倍后将被keyboard.nextLine();
所以你必须再输入keyboard.nextLine();
。
double degrees = keyboard.nextDouble();
keyboard.nextLine();
System.out.println("Enter the scale: ");
String scale = keyboard.nextLine();
您需要消耗新线。 Scanner.nextDouble()
将只读取下一个 token 作为 double,没有行终止符。要么在读完double后加上keyboard.nextLine()
:
double degrees = keyboard.nextDouble();
keyboard.nextLine();
或在输入行上使用Double.parseDouble(String)
:
String degreesString = keyboard.nextLine();
double degrees = Double.parseDouble(degreesString);
如果扫描仪输入实用程序不是强制性的,我会建议使用 BufferedReader。
package com.tempcalculator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
String degrees = getInput("Degrees?");
String scale = getInput("Scale?");
switch (scale.charAt(0)) {
case 'F':
case 'f':
double fahrenheit = (Double.parseDouble(degrees) - 32) * .5556;
System.out.println(degrees + scale + " equals " + fahrenheit + " degrees Celsius");
break;
case 'C':
case 'c':
double celsius = (Double.parseDouble(degrees) + 32) * 1.8;
System.out.println(degrees + scale + " equals " + celsius + " degrees Fahrenheit");
break;
default:
System.out.println("Whoops! Error 404: Requested scale was not found. ");
break;
}
}
public static String getInput(String prompt) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return in.readLine();
} catch (IOException e) {
return "Error: " + e.getMessage();
}
}
}