当用户输入 == null 但它不起作用时,使用 try-catch 捕获异常
Using try-catch to catch exception when user input == null but it won't work
如果用户没有在两者或其中之一上输入任何内容,则 try-catch 应该会捕获,但现在它什么都不做。如果您输入一个句子而不是一个字符,它会崩溃,反之亦然。我对 Java 很陌生,但这是我所拥有的:
import javax.swing.*;
public class Main {
public static void main(String[] args) {
countSentence();
}
static void countSentence() {
String input;
char ch;
int count = 0;
int countLetters = 0;
input = JOptionPane.showInputDialog(null, "Write a sentence");
ch = JOptionPane.showInputDialog(null, "Write a character").charAt(0);
if (!input.equals("") || ch != ' ') {
try {
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) != ' ')
count++;
}
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == ch)
countLetters++;
}
JOptionPane.showMessageDialog(null,
"The sentence has " + count + " characters \n" + "The character " + ch
" occurs " + countLetters + " times. " +
"First time at index place " + input.indexOf(ch) +
"\n Last time at index place " + input.lastIndexOf(ch));
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "You need to input both a sentence and a character!");
}
}
}
}
这里你引用了第一个位置的字符
ch = JOptionPane.showInputDialog(null, "Write a character").charAt(0);
但如果用户未输入内容,代码将失败并显示 索引超出范围 异常。
我会像这样重写
static void countSentence() {
String input;
String ch;
int count = 0;
int countLetters = 0;
input = JOptionPane.showInputDialog(null, "Write a sentence");
ch = JOptionPane.showInputDialog(null, "Write a character");
if (input.equals("") || ch.equals("")) {
JOptionPane.showMessageDialog(null, "You need to input both a sentence and a character!");
} else {
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) != ' ')
count++;
}
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == ch.charAt(0))
countLetters++;
}
JOptionPane.showMessageDialog(null,
"The sentence has " + count + " characters \n" + "The character " + ch +
" occurs " + countLetters + " times. " +
"First time at index place " + input.indexOf(ch) +
"\n Last time at index place " + input.lastIndexOf(ch));
}
} }
不要使用 try-catch 进行流量控制。
看:
https://wiki.c2.com/?DontUseExceptionsForFlowControl
如果用户没有在两者或其中之一上输入任何内容,则 try-catch 应该会捕获,但现在它什么都不做。如果您输入一个句子而不是一个字符,它会崩溃,反之亦然。我对 Java 很陌生,但这是我所拥有的:
import javax.swing.*;
public class Main {
public static void main(String[] args) {
countSentence();
}
static void countSentence() {
String input;
char ch;
int count = 0;
int countLetters = 0;
input = JOptionPane.showInputDialog(null, "Write a sentence");
ch = JOptionPane.showInputDialog(null, "Write a character").charAt(0);
if (!input.equals("") || ch != ' ') {
try {
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) != ' ')
count++;
}
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == ch)
countLetters++;
}
JOptionPane.showMessageDialog(null,
"The sentence has " + count + " characters \n" + "The character " + ch
" occurs " + countLetters + " times. " +
"First time at index place " + input.indexOf(ch) +
"\n Last time at index place " + input.lastIndexOf(ch));
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "You need to input both a sentence and a character!");
}
}
}
}
这里你引用了第一个位置的字符
ch = JOptionPane.showInputDialog(null, "Write a character").charAt(0);
但如果用户未输入内容,代码将失败并显示 索引超出范围 异常。
我会像这样重写
static void countSentence() {
String input;
String ch;
int count = 0;
int countLetters = 0;
input = JOptionPane.showInputDialog(null, "Write a sentence");
ch = JOptionPane.showInputDialog(null, "Write a character");
if (input.equals("") || ch.equals("")) {
JOptionPane.showMessageDialog(null, "You need to input both a sentence and a character!");
} else {
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) != ' ')
count++;
}
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == ch.charAt(0))
countLetters++;
}
JOptionPane.showMessageDialog(null,
"The sentence has " + count + " characters \n" + "The character " + ch +
" occurs " + countLetters + " times. " +
"First time at index place " + input.indexOf(ch) +
"\n Last time at index place " + input.lastIndexOf(ch));
}
} }
不要使用 try-catch 进行流量控制。 看: https://wiki.c2.com/?DontUseExceptionsForFlowControl