Java 抛出异常,即使数据类型正确
我已经建立了一个货币转换程序,它需要用户输入。在程序中的某个时刻,我询问他们想要兑换的货币。然而,没有什么能阻止他们将“java”作为一种货币或任何其他词语 这是我当前的代码,我将进一步解释我要查找的内容:Java 抛出异常,即使数据类型正确,java,exception,exception-handling,Java,Exception,Exception Handling,我已经建立了一个货币转换程序,它需要用户输入。在程序中的某个时刻,我询问他们想要兑换的货币。然而,没有什么能阻止他们将“java”作为一种货币或任何其他词语 这是我当前的代码,我将进一步解释我要查找的内容: import java.util.Scanner; public class Exchange { public static void main(String[] args) { int i = 1; Scanner inn = new Scann
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
-循环中的最后一个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语句。在默认情况下,可以引发异常。因为您正在捕获异常
只需生成抛出新异常()代码>
别忘了关闭扫描仪
finally {
inn.close();
}
它将在else部分抛出异常。在代码中,else部分将不起作用,因为存在异常,它将直接转到catch块 你试过什么?你似乎有99%的成功率。此外,这里不一定需要例外。您可以预先检查货币,并在输入不正确时循环。要“产生异常”,您需要抛出它:throw new SomeException()
,但我不认为这是您想要的。您如何进行货币预检查并“在输入不正确时循环”?如果你不介意的话。我很新鲜,所以我还不懂所有的术语。为什么我不想要一个例外,我什么时候想要它?请原谅所有的问题,但我真的很感谢你的帮助。switch语句到底是做什么的?你能帮我把它分解一下吗?我对编程很陌生。当扫描仪不从文件中读取时,我需要关闭扫描仪吗?开关
和if/elseif/else
之间绝对没有功能上的区别,并且您永远不应该关闭系统。在
@JarrodRoberson中,即使开关
和if else
之间没有功能上的区别,在我看来,这种转变更容易理解。你能解释一下或者给我一个链接到一个源代码,为什么你永远不应该关闭系统?我被教导你应该永远关闭扫描仪?!除了关闭系统的其他有害建议之外,这就是“答案”的问题。在
中。在尝试回答更多问题之前,请阅读,因为这种尝试毫无意义。
finally {
inn.close();
}
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.");*/
}