抛出异常,即使数据类型正确
我已经构建了一个货币转换程序,并且需要用户输入。在该计划的某一点,我要求他们想要兑换的货币。但是,没有任何东西阻止他们将“java”作为货币或任何其他词汇。抛出异常,即使数据类型正确
这里是我当前的代码,我会解释多一点什么我在寻找进一步下跌:
import java.util.Scanner;
public class Exchange {
public static void main(String[] args) {
int i = 1;
Scanner inn = new Scanner(System.in);
try{
while (i > 0){
double USDrate = 0.12808;
double EURrate = 0.107643821;
double GBPrate = 0.098872935;
System.out.println("Hello, what currency would you like to convert to?");
System.out.println("Types: USD/EUR/GBP");
String type = inn.nextLine();
System.out.println("Hello, how much would you like to convert?");
double NOK = inn.nextDouble();
if(type.equalsIgnoreCase("USD")){
System.out.printf(NOK + "kr equals $%.2f \n", (NOK*USDrate));
}else if(type.equalsIgnoreCase("EUR")){
System.out.printf(NOK + "kr equals €%.2f \n", (NOK*EURrate));
}else if(type.equalsIgnoreCase("GBP")){
System.out.printf(NOK + "kr equals £%.2f \n", (NOK*GBPrate));
}else {
// This is where I would like to throw an exception if what they put in doesn't make sense.
}
inn.nextLine();
System.out.println("Would you like to convert more?");
System.out.println(" YES/NO ");
String ans = inn.nextLine();
if(ans.equalsIgnoreCase("NO")){
i = 0;
}
}
}
catch(Exception e){
System.out.println("You've entered an incorrect value! Restart.");
}
}
}
我想除了采取while
-loop内的最后else
语句来下进行,正如你所看到的,我有一个评论,那就是那个意思。我还想在代码块中有同样的例外,我问他们是否想要转换更多。
任何人都可以提供帮助吗?如果您需要的任何细节丢失,我会根据要求给他们。
在此先感谢。
我想出了如何使它成为我想要的方式。也不例外必要的,但这里是我的解决方案:
import java.util.Scanner;
public class Exchange {
public static void main(String[] args) {
int i = 1;
Scanner inn = new Scanner(System.in);
try{
while (i > 0){
double USDrate = 0.12808;
double EURrate = 0.107643821;
double GBPrate = 0.098872935;
System.out.println("Hello, what currency would you like to convert to?");
System.out.println("Types: USD/EUR/GBP");
String type = inn.nextLine();
if(!type.equalsIgnoreCase("USD") && !type.equalsIgnoreCase("EUR") && !type.equalsIgnoreCase("GBP")){
System.out.println("Not available currency! Try again.");
break;
}
System.out.println("How much would you like to convert?");
double NOK = inn.nextDouble();
if(type.equalsIgnoreCase("USD")){
System.out.printf(NOK + "kr equals $%.2f \n", (NOK*USDrate));
}else if(type.equalsIgnoreCase("EUR")){
System.out.printf(NOK + "kr equals €%.2f \n", (NOK*EURrate));
}else if(type.equalsIgnoreCase("GBP")){
System.out.printf(NOK + "kr equals £%.2f \n", (NOK*GBPrate));
}
inn.nextLine();
System.out.println("Would you like to convert more?");
System.out.println(" YES/NO ");
String ans = inn.nextLine();
if(ans.equalsIgnoreCase("NO")){
i = 0;
}
}
}
catch(Exception e){
System.out.println("You've entered an incorrect value! Restart.");
}
}
}
使用的另一种方法:)感谢您的答案不管怎么说,他们帮了不少忙。
而不是使用if/else if... block
我建议使用switch语句。在默认情况下,你可以抛出一个异常。因为你赶上Exception
只是让throw new Exception();
type = type.toUpperCase();
switch(type) {
case "USD":
System.out.printf(NOK + "kr equals $%.2f \n", (NOK*USDrate));
break;
case "EUR":
System.out.printf(NOK + "kr equals €%.2f \n", (NOK*EURrate));
break;
case "GBP":
System.out.printf(NOK + "kr equals £%.2f \n", (NOK*GBPrate));
break;
default:
throw new Exception();
}
而且不要忘记关闭扫描仪
finally {
inn.close();
}
'switch'语句到底做了什么?你能帮我分解一下吗?我对编程非常陌生。 当我不从文件读取时,是否需要关闭扫描仪? –
'switch'和'if/elseif/else'之间没有任何功能上的区别,而且你不应该关闭'System.in'。 –
@JarrodRoberson即使'switch'和'if else'之间没有功能上的区别,在我看来,开关更清晰。你能解释一下,或者给我一个源码的链接,为什么你不应该关闭'System.in'?我得到了教导,你应该总是关闭扫描仪?! – KilledByCheese
为什么你想在这里抛出异常!
在else块
System.out.println("You've entered an incorrect value! Please provide correct value"); // something similar.
只是打印一些有意义的信息。如果你真的想抛出异常,然后写在下面else块的代码。
throw new Exception();
我对Java和编程一般都很陌生,所以想抛出异常是因为我不知道更好。 编辑:我想我真正想要的是程序提示用户写一个正确的值,或在这种情况下,正确的单词。一个简单的印刷会这样做吗?会有例外吗? –
我将打印消息而不是抛出异常,并要求用户重新启动程序。 – nagendra547
嗯,好的。不过,我想要做的是让程序立即提示用户输入。例如,通过打印“输入错误,只能转换为Usd,Eur或Gbp”,然后要求用户输入其中任何一个的新用户输入。那可能吗? –
import java.util.Scanner;
public class Exchange {
public static void main(String[] args) throws Exception {
int i = 1;
Scanner inn = new Scanner(System.in);
while (i > 0) {
double USDrate = 0.12808;
double EURrate = 0.107643821;
double GBPrate = 0.098872935;
System.out.println("Hello, what currency would you like to convert to?");
System.out.println("Types: USD/EUR/GBP");
String type = inn.nextLine();
System.out.println("Hello, how much would you like to convert?");
double NOK = inn.nextDouble();
if (type.equalsIgnoreCase("USD")) {
System.out.printf(NOK + "kr equals $%.2f \n", (NOK * USDrate));
} else if (type.equalsIgnoreCase("EUR")) {
System.out.printf(NOK + "kr equals €%.2f \n", (NOK * EURrate));
} else if (type.equalsIgnoreCase("GBP")) {
System.out.printf(NOK + "kr equals £%.2f \n", (NOK * GBPrate));
} else {
throw new Exception();
}
inn.nextLine();
System.out.println("Would you like to convert more?");
System.out.println(" YES/NO ");
String ans = inn.nextLine();
if (ans.equalsIgnoreCase("NO")) {
i = 0;
}
}
}
/*(Exception e) {
System.out.println("You've entered an incorrect value! Restart.");*/
}
它会扔在别的part.In代码其他部分的异常将无法正常工作,有一个例外,它会直接转到catch块。
请阅读[我如何写一个很好的答案?](http:// *。 com/help/how-to-answer),然后再尝试回答更多问题,因为这种尝试毫无意义。 –
你有什么试过?你看起来是99%的方式。此外,你不一定需要在这里例外。您可以预先检查货币,并在输入错误时循环。 – Carcigenicate
为了“有一个例外”,你需要抛出它:抛出新的SomeException(),但我不认为这就是你想要的 – 2017-09-04 17:11:19
你将如何去预先检查货币和“循环,而输入是坏的“?如果你不介意。我很新鲜,所以我不明白所有条款。为什么我不想要一个例外,我什么时候需要它?请原谅所有问题,但我真的很感谢你的帮助。 –