gpt4 book ai didi

java - 在java程序中添加异常

转载 作者:行者123 更新时间:2023-12-02 03:13:09 24 4
gpt4 key购买 nike

我在添加字符串异常以防止在输入字符串而不是整数时程序崩溃时遇到问题。我环顾四周并尝试了 try{}catch{},但我的程序仍然会因字符串而崩溃。我正在寻找修复 getInt()。

import java.util.*;
public class Binary{

public static void main(String[]args){
Scanner in = new Scanner(System.in);
String a = "";
boolean cont = true;
while(cont){
printb(convert(getInt(in, "Enter a number: ")));
System.out.println("Do you want to continue (yes or no)?");
a = in.next();
if(a.equals("yes"))
cont = true;
else{
System.out.println("You answerd no. Have a nice day.");
cont = false;
}
}


}

public static int getInt( Scanner console, String prompt){
System.out.print(prompt);
while(!console.hasNext()){
try{
console.next();
System.out.println("Not an integer, try again.");
System.out.print(prompt);
}
catch(Input MismatchException exception){
System.out.print("Not an integer, try again.");
System.out.print(prompt);
}

}
return console.nextInt();
}

public static int[] convert(int decimal){

int decimalCopy = decimal;
int len = 0;
while(decimal != 0){
decimal/=2;
len++;
}
decimal = decimalCopy;

int[] b = new int[len];
int index = 0;
while(decimal !=0){
if(decimal%2 != 0)
b[index] = 1;
else{
b[index] = 0;
}
decimal/=2;
index++;
}

return b;

}

public static void printb(int[] b){
for(int i = b.length-1; i>=0; i--){
System.out.print(b[i]);
}
System.out.println();
}

}

最佳答案

try/catch/finally是处理这个问题的方法,但是如果您不熟悉异常处理,那么弄清楚如何处理它们可能会很棘手。即使您确实将它们放在正确的位置,您也需要处理尚未正确“清理”的字符串输入,因此可以级联到 a 的位置。被分配并结束程序(因为所有不是"is"的都是“否”)。

关键是将可能抛出异常的行放在 try 中 block ,然后 catch Exception进行一些错误处理,然后继续使用 finally 进行需要发生的事情。 (您可以在这里省略finally,但我想确保您理解它,因为它很重要。finally出现在try/catch之后,并且该代码块中的任何内容都将在任一情况下执行(除非您提前退出程序)。)

这应该可以做到:

while (cont) {

// getInt() is the troublemaker, so we try it:
// Notice I have changed 'number' to 'integer' here - this improves
// UX by prompting the user for the data type the program expects:

try {
printb(convert(getInt(in, "Enter an integer: ")));
}

// we catch the Exception and name it. 'e' is a convention but you
// could call it something else. Sometimes we will use it for info,
// and in this case we don't really need it, but Java expects it
// nonetheless.
// We do our error handling here: (notice the call to in.next() -
// this ensures that the string that was entered gets properly
// handled and doesn't cascade down to the assignment of 'a') - if
// this confuses you, try it without the in.next() and see what
// happens!

catch (Exception e) {
System.out.println("\nPlease enter an integer.\n");
in.next();
}

// Again, in this case, the finally isn't necessary, but it can be
// very handy, so I'm using it for illustrative purposes:

finally {
System.out.println("Do you want to continue (yes or no)?");
a = in.next();
if (a.equals("yes")) {
cont = true;
} else {
System.out.println("You answered no. Have a nice day.");
cont = false;
}
}
}

关于java - 在java程序中添加异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40745405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com