我认为问题出在switch语句中,当评分者尝试输入q时,我会在成绩中出现错误。''但IDE中没有错误。 error error2
switch (chV)
{
case 'q':
System.exit(0);
break;
case 'c':
int cntNonWhitespaces = getNumOfNonWSCharacters(textInput);
System.out.print("Number of non-whitespace characters: " + cntNonWhitespaces);
break;
case 'w':
int wordsCount = getNumOfWords(textInput);
System.out.println("Number of words: " + wordsCount);
break;
case 'f':
System.out.println("Enter a word or phrase to be found: ");
String findS = scannerInput.nextLine();
int findCount = findText(textInput, findS);
System.out.println("\"" + findS + "\""+ " " + "instances: " + findCount);
break;
case 'r':
String newstringVal = replaceExclamation(textInput);
System.out.println("Edited textInput: " + newstringVal);
break;
case 's':
newstringVal = shortenSpace(textInput);
System.out.println("Edited textInput:" + newstringVal);
break;
default:
System.out.println("Invalid option. Please try again");
}
我会附上我一直遇到的错误
完整代码:
import java.util.*;
public class AuthoringAssistant {
public static Scanner scannerInput = new Scanner(System.in);
public static String shortenSpace(String textInput) {
String tempVal = textInput.trim().replaceAll(" +", " ");
return tempVal;
}
public static String replaceExclamation(String textInput) {
String tempVal = textInput.replaceAll("!", ".");
return tempVal;
}
public static int findText(String textInput, String findS) {
int countVal = 0;
int it = 0;
while ((it = textInput.indexOf(findS)) != -1) {
textInput = textInput.substring(it +
findS.length());
countVal += 1;
}
return countVal;
}
public static int getNumOfWords(String textInput) {
textInput = shortenSpace(textInput);
String[] words1 = textInput.split(" ");
return words1.length;
}
public static int getNumOfNonWSCharacters(String textInput) {
textInput = textInput.trim().replaceAll("\\s", "");
return textInput.length();
}
public static void printMenu() {
System.out.println("MENU");
System.out.println("c - Number of non-whitespace characters");
System.out.println("w - Number of words");
System.out.println("f - Find text");
System.out.println("r - Replace all !'s");
System.out.println("s - Shorten spaces");
System.out.println("q - Quit");
System.out.println("");
System.out.println("Choose an option:");
}
public static void main(String[] args) {
while(true)
{
System.out.println("Enter a sample text:");
System.out.println("");
String textInput = scannerInput.nextLine();
System.out.print("You entered: " + textInput);
System.out.println("");
System.out.println("");
printMenu();
char chV = scannerInput.nextLine().charAt(0);
switch (chV)
{
case 'q':
System.exit(0);
break;
case 'c':
int cntNonWhitespaces = getNumOfNonWSCharacters(textInput);
System.out.print("Number of non-whitespace characters: " + cntNonWhitespaces);
break;
case 'w':
int wordsCount = getNumOfWords(textInput);
System.out.println("Number of words: " + wordsCount);
break;
case 'f':
System.out.println("Enter a word or phrase to be found: ");
String findS = scannerInput.nextLine();
int findCount = findText(textInput, findS);
System.out.println("\"" + findS + "\""+ " " + "instances: " + findCount);
break;
case 'r':
String newstringVal = replaceExclamation(textInput);
System.out.println("Edited textInput: " + newstringVal);
break;
case 's':
newstringVal = shortenSpace(textInput);
System.out.println("Edited textInput:" + newstringVal);
break;
default:
System.out.println("Invalid option. Please try again");
}
System.out.println();
}
}
}