[英]Adding exception to java program
我無法添加String異常以防止輸入字符串而不是int時程序崩潰。 我確實環顧了一下,嘗試了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();
}
}
import java.util.*;
public class Binary{
public static void main(String[]args){
try {
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;
} }
} catch(Exception e) {
System.out.println("Invalid input");
}
}
public static int getInt( Scanner console, String prompt){
System.out.print(prompt);
while(!console.hasNext()){
console.next();
System.out.println("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();
}
} `enter code here`
try
/ catch
/ finally
是處理此問題的方法,但是如果您對異常處理不熟悉,則很難弄清楚如何處理它們。 而且即使將它們放在正確的位置,也需要處理尚未正確“清理”的字符串輸入,可以這么說,因此,級聯到分配a
位置並結束程序(因為所有是不是是否)。
關鍵是將可能引發異常的行放在try
塊中,然后通過一些錯誤處理catch
Exception
,然后繼續進行finally
需要發生的事情。 (您可以在這里省略finally
,但是我想確保您理解它,因為它很重要。在try
/ catch
之后finally
出現,並且該代碼塊中的任何內容都會在兩種情況下執行(除非您過早退出程序)。)
應該這樣做:
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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.