我如何为错误的输入向用户发出错误提示?
How do I give the user an error for the wrong input?
我正在尝试制作一个将温度转换为摄氏度的程序,但我卡住了
如果用户尝试输入数字以外的内容,如何给用户一个错误。有人有什么建议可以帮助我吗?
这是我的代码:
import java.text.DecimalFormat;
import java.util.Scanner;
public class FinalTemp {
public static void main(String[] args)
float celsius;
int temp;
String confirm = "Y/N";
Scanner keyboard = new Scanner(System.in);
do {
System.out.println("Enter temperature in fahrenheit: ");
temp = keyboard.nextInt();
while (confirm.equalsIgnoreCase("N"));
celsius =(float)((temp-32)*5.0/9.0);
celsius = Float.parseFloat(new DecimalFormat("##.##").format(celsius));
System.out.println();
if (celsius <= 0)
System.out.println(temp + " degrees in Fahrenheit is equal to " + celsius + "
degrees celsius");
if (celsius >= 0)
System.out.println(temp + " degrees in Fahrenheit is equal to " + celsius + "
degrees celsius");
System.out.println("Would you like to enter another temperature?");
System.out.println("Please type Y/N.");
confirm = keyboard.next();
}
while (confirm.equalsIgnoreCase("Y")); {
System.out.println();
System.out.println("Hello Instructor Fekete, This it the INF231 Final Assignment
for the class");
}
keyboard.close();
}
}
可能的解决方案:
boolean invalid = true;
while (invalid) {
try {
String tempStr = keyboard.next();
temp = Integer.parseInt(tempStr);
invalid = false;
}
catch (Exception x) {
System.out.println("Invalid value, inform a valid temperature");
}
}
那么你的整个代码将是这样的:
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
float celsius;
int temp = 0;
String confirm = "Y/N";
Scanner keyboard = new Scanner(System.in);
do {
System.out.println("Enter temperature in fahrenheit: ");
boolean invalid = true;
while (invalid) {
try {
String tempStr = keyboard.next();
temp = Integer.parseInt(tempStr);
invalid = false;
}
catch (Exception x) {
System.out.println("Invalid value, inform a valid temperature");
}
}
while (confirm.equalsIgnoreCase("N")) ;
celsius = (float) ((temp - 32) * 5.0 / 9.0);
celsius = Float.parseFloat(new DecimalFormat("##.##").format(celsius));
System.out.println();
if (celsius <= 0)
System.out.println(temp + " degrees in Fahrenheit is equal to " + celsius + " degrees celsius");
if (celsius >= 0)
System.out.println(temp + " degrees in Fahrenheit is equal to " + celsius + " degrees celsius");
System.out.println("Would you like to enter another temperature?");
System.out.println("Please type Y/N.");
confirm = keyboard.next();
}
while (confirm.equalsIgnoreCase("Y")); {
System.out.println();
System.out.println("Hello Instructor Fekete, This it the INF231 Final Assignment for the class ");
}
keyboard.close();
}
}
您 可以 执行此操作的另一种方法是接受来自用户的字符串并使用 String#matches() method with a small Regular Expression (正则表达式)作为参数检查它以验证提供的是有符号或无符号整数或浮点值,例如:
String ls = System.lineSeparator();
String confirm = "";
// Allow both fahrenheit & celsius to be entered as floating point values.
float temp, celsius;
Scanner keyboard = new Scanner(System.in);
do {
String temperature = "";
while (temperature.isEmpty()) {
System.out.print("Enter temperature in fahrenheit (c to cancel): -> ");
temperature = keyboard.nextLine().trim();
// Is Cancel desired?
if (temperature.equalsIgnoreCase("c")) {
System.out.println(ls + "Tempurture Convertion Canceled!");
confirm = "n";
break;
}
// Validate entry...
if (!temperature.matches("-?\d+(\.\d+)?")) {
System.out.println(ls + "Invalid Tempurature Entry (" + temperature
+ ")! Try again..." + ls);
temperature = "";
}
}
if (confirm.equalsIgnoreCase("n")) {
break;
}
temp = Float.parseFloat(temperature);
celsius = ((temp - 32.0f) * (5.0f / 9.0f));
celsius = Float.parseFloat(new java.text.DecimalFormat("##.##").format(celsius));
System.out.println();
if (celsius <= 0.0f) {
System.out.println(temp + " degrees Fahrenheit is equal to "
+ celsius + " degrees Celsius.");
}
else if (celsius >= 0) {
System.out.println(temp + " degrees in Fahrenheit is equal to "
+ celsius + " degrees celsius");
}
while(confirm.isEmpty()) {
System.out.println(ls + "Would you like to enter another temperature?");
System.out.print("Please enter Y or N: -> ");
confirm = keyboard.nextLine().trim();
// Validate entry...
if(!confirm.matches("(?i)[yn]")) {
System.out.println("Invalid Responce (" + confirm + ")! Try again...");
confirm = "";
}
}
System.out.println();
} while (confirm.equalsIgnoreCase("Y"));
System.out.println();
System.out.println("Hello Instructor Fekete, This it the INF231 "
+ "Final Assignment for the class.");
使用的正则表达式:
表达式:"-?\d+(\.\d+)?"
访问regex101.com for explanation
表达式:"(?i)[yn]"
访问regex101.com for explanation
我正在尝试制作一个将温度转换为摄氏度的程序,但我卡住了 如果用户尝试输入数字以外的内容,如何给用户一个错误。有人有什么建议可以帮助我吗? 这是我的代码:
import java.text.DecimalFormat;
import java.util.Scanner;
public class FinalTemp {
public static void main(String[] args)
float celsius;
int temp;
String confirm = "Y/N";
Scanner keyboard = new Scanner(System.in);
do {
System.out.println("Enter temperature in fahrenheit: ");
temp = keyboard.nextInt();
while (confirm.equalsIgnoreCase("N"));
celsius =(float)((temp-32)*5.0/9.0);
celsius = Float.parseFloat(new DecimalFormat("##.##").format(celsius));
System.out.println();
if (celsius <= 0)
System.out.println(temp + " degrees in Fahrenheit is equal to " + celsius + "
degrees celsius");
if (celsius >= 0)
System.out.println(temp + " degrees in Fahrenheit is equal to " + celsius + "
degrees celsius");
System.out.println("Would you like to enter another temperature?");
System.out.println("Please type Y/N.");
confirm = keyboard.next();
}
while (confirm.equalsIgnoreCase("Y")); {
System.out.println();
System.out.println("Hello Instructor Fekete, This it the INF231 Final Assignment
for the class");
}
keyboard.close();
}
}
可能的解决方案:
boolean invalid = true;
while (invalid) {
try {
String tempStr = keyboard.next();
temp = Integer.parseInt(tempStr);
invalid = false;
}
catch (Exception x) {
System.out.println("Invalid value, inform a valid temperature");
}
}
那么你的整个代码将是这样的:
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
float celsius;
int temp = 0;
String confirm = "Y/N";
Scanner keyboard = new Scanner(System.in);
do {
System.out.println("Enter temperature in fahrenheit: ");
boolean invalid = true;
while (invalid) {
try {
String tempStr = keyboard.next();
temp = Integer.parseInt(tempStr);
invalid = false;
}
catch (Exception x) {
System.out.println("Invalid value, inform a valid temperature");
}
}
while (confirm.equalsIgnoreCase("N")) ;
celsius = (float) ((temp - 32) * 5.0 / 9.0);
celsius = Float.parseFloat(new DecimalFormat("##.##").format(celsius));
System.out.println();
if (celsius <= 0)
System.out.println(temp + " degrees in Fahrenheit is equal to " + celsius + " degrees celsius");
if (celsius >= 0)
System.out.println(temp + " degrees in Fahrenheit is equal to " + celsius + " degrees celsius");
System.out.println("Would you like to enter another temperature?");
System.out.println("Please type Y/N.");
confirm = keyboard.next();
}
while (confirm.equalsIgnoreCase("Y")); {
System.out.println();
System.out.println("Hello Instructor Fekete, This it the INF231 Final Assignment for the class ");
}
keyboard.close();
}
}
您 可以 执行此操作的另一种方法是接受来自用户的字符串并使用 String#matches() method with a small Regular Expression (正则表达式)作为参数检查它以验证提供的是有符号或无符号整数或浮点值,例如:
String ls = System.lineSeparator();
String confirm = "";
// Allow both fahrenheit & celsius to be entered as floating point values.
float temp, celsius;
Scanner keyboard = new Scanner(System.in);
do {
String temperature = "";
while (temperature.isEmpty()) {
System.out.print("Enter temperature in fahrenheit (c to cancel): -> ");
temperature = keyboard.nextLine().trim();
// Is Cancel desired?
if (temperature.equalsIgnoreCase("c")) {
System.out.println(ls + "Tempurture Convertion Canceled!");
confirm = "n";
break;
}
// Validate entry...
if (!temperature.matches("-?\d+(\.\d+)?")) {
System.out.println(ls + "Invalid Tempurature Entry (" + temperature
+ ")! Try again..." + ls);
temperature = "";
}
}
if (confirm.equalsIgnoreCase("n")) {
break;
}
temp = Float.parseFloat(temperature);
celsius = ((temp - 32.0f) * (5.0f / 9.0f));
celsius = Float.parseFloat(new java.text.DecimalFormat("##.##").format(celsius));
System.out.println();
if (celsius <= 0.0f) {
System.out.println(temp + " degrees Fahrenheit is equal to "
+ celsius + " degrees Celsius.");
}
else if (celsius >= 0) {
System.out.println(temp + " degrees in Fahrenheit is equal to "
+ celsius + " degrees celsius");
}
while(confirm.isEmpty()) {
System.out.println(ls + "Would you like to enter another temperature?");
System.out.print("Please enter Y or N: -> ");
confirm = keyboard.nextLine().trim();
// Validate entry...
if(!confirm.matches("(?i)[yn]")) {
System.out.println("Invalid Responce (" + confirm + ")! Try again...");
confirm = "";
}
}
System.out.println();
} while (confirm.equalsIgnoreCase("Y"));
System.out.println();
System.out.println("Hello Instructor Fekete, This it the INF231 "
+ "Final Assignment for the class.");
使用的正则表达式:
表达式:"-?\d+(\.\d+)?"
访问regex101.com for explanation
表达式:"(?i)[yn]"
访问regex101.com for explanation