gpt4 book ai didi

java - 无法让我的 MismatchException 与 switch 一起正常工作

转载 作者:行者123 更新时间:2023-12-01 18:47:22 25 4
gpt4 key购买 nike

问题已解决。

所以我正在完成我的作业,最后要做的就是实现一些简单的错误处理,但事实证明我不太确定我做错了什么。

我有 5 个案例,需要输入 0、1、2、3、4。效果很好。

但是如果输入不是数字,它应该显示一条消息,并让用户输入一些新的输入。每项任务完成后,您应该会看到菜单并能够制作新的coice。

我可以打印消息,但无法接受新的输入。我理解这个问题以及为什么会发生,但我不知道如何实际解决它。



import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ObscureLovecraft {

public static void main(String[] args) {
// Looking for files and opening scanner
File file = new File("./lovecraft.txt");
File readObscure = new File("./obscure.txt");
Scanner sc = new Scanner(System.in);

// Printing UI

printMenu();

// Taking input, looping until case 0 is executed
// Printing menu after each iteration
// Switch, prints a new menu after every iteration
try {
int input = sc.nextInt();

while (input != 0) {

switch (input) {
case 1:
writeObscure(file);
printMenu();
input = sc.nextInt();
break;

case 2:
translateObscure(readObscure);
printMenu();
input = sc.nextInt();
break;

case 3:
printObscure();
printMenu();
input = sc.nextInt();
break;

case 4:
printTranslated();
printMenu();
input = sc.nextInt();
break;

case 0:
kill();
break;

}
}
} catch (InputMismatchException e) {
System.out.println("You must use numbers.");
sc.nextInt();
}

sc.close();

System.out.println(":(");

}

public static void switches() {

}

/**
* Just a method printing our menu
*/
public static void printMenu() {
System.out.println();
System.out.println("Obscure");
System.out.println("=======");
System.out.println("1. Make obscure");
System.out.println("2. Make readable");
System.out.println("3. Print obscure");
System.out.println("4. Print readable");
System.out.println("0. Exit");
}

/**
* Method which will print the "obscured" version of the text
*
* @param file path to the file
*/
public static void writeObscure(File file) {
// Create new output file, creating PrintWriter, StringBuilder and placeholder
// for the text
File outFile = new File("./obscure.txt");
StringBuilder buf = new StringBuilder();
try {
PrintWriter pw = new PrintWriter(outFile);
Scanner fileSc = new Scanner(file);
String text = "";

// Placeholder for chars, looping through the file, adding it to placeholder
// text, with new line
char c;
while (fileSc.hasNextLine()) {
text = (fileSc.nextLine() + "\n");
// Looping through the text, taking each character, pushing it 3 steps in the
// alphabet
// Taking care of z/Z pushing it to a/A, should maybe be c/C? Oh well.
// Appending it to the stringbuilder in the new order
// Casting char to int, to get ascii value
for (int i = 0; i < text.length(); i++) {
c = text.charAt(i);
if (c == 'z') {
buf.append('a');
} else if (c == 'Z') {
buf.append('A');
} else {
int ascii = (int) c;
char nextChar = (char) (ascii + 3);
buf.append(nextChar);
}

}
}
fileSc.close();

// Printing and closing
pw.print(buf);
pw.close();
System.out.println("Done");
} catch (FileNotFoundException e) {
System.out.println("Seems like a file gone missing, check error message: " + e.getMessage());
System.out.println("Printing stacktrace: ");
e.printStackTrace();
}
}

/**
* Method which will translate the obscured text to normal, Basically same as
* the method above, but in reversed order
*
* @param file
*/
public static void translateObscure(File file) {
// Read the file:
StringBuilder obsBuf = new StringBuilder();
try {
Scanner readSC = new Scanner(file);
// Print the file
File printReadable = new File("./readableLovecraft.txt");
PrintWriter pwRead = new PrintWriter(printReadable);

// Text
String readableText = "";
char readC;

// Loop thru
// Something weird happend with empty lines,
// Had to manually skip them
while (readSC.hasNextLine()) {
readableText = (readSC.nextLine() + "\n");

for (int i = 0; i < readableText.length(); i++) {
readC = readableText.charAt(i);
if (readC == '\n') {
obsBuf.append(readC);
} else if (readC == 'z') {
obsBuf.append('a');
} else if (readC == 'Z') {
obsBuf.append('z');
} else {
int ascii = (int) readC;
char nextChar = (char) (ascii - 3);
obsBuf.append(nextChar);
}
}

}
readSC.close();

// Printing and closing
pwRead.print(obsBuf);
pwRead.close();
System.out.println("Done");
} catch (FileNotFoundException e) {
System.out.println("Seems like a file gone missing, check error message: " + e.getMessage());
System.out.println("Printing stacktrace: ");
e.printStackTrace();
}
}

/**
* Simple method which loops through the text and prints it
*/
public static void printObscure() {
File readOutFile = new File("./obscure.txt");
try {

Scanner fileSc = new Scanner(readOutFile);
String readText = "";
while (fileSc.hasNextLine()) {
readText = (fileSc.nextLine());
System.out.println(readText);
}
fileSc.close();
} catch (FileNotFoundException e) {
System.out.println("Seems like a file gone missing, check error message: " + e.getMessage());
System.out.println("Printing stacktrace: ");
e.printStackTrace();

}
}

/**
* Simple method which loops through the text and prints it
*/
public static void printTranslated() {
File readReadOutFile = new File("./readableLovecraft.txt");
String readReadText = "";
Scanner fileSc;
try {
fileSc = new Scanner(readReadOutFile);

while (fileSc.hasNextLine()) {
readReadText = (fileSc.nextLine() + "\n");
System.out.println(readReadText);

}
fileSc.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Seems like a file gone missing, check error message: " + e.getMessage());
System.out.println("Printing stacktrace: ");
e.printStackTrace();
}
}

/**
* Method which kills the program
*/
public static void kill() {
System.exit(1);
}
}

最佳答案

1- Create a new method and put your logic into it

2- Delete while from your code

3- Throws InputMismatchException in default block

4- Call the newMethod() in cases instead of input = sc.nextInt. (recursive)

5- Call the newMethod() in catch block (recursive)

public static void newMethod() {

Scanner sc = new Scanner(System.in);

try {

int input = sc.nextInt();

switch (input) {
case 1:
writeObscure(file);
printMenu();
newMethod();
break;

case 2:
translateObscure(readObscure);
printMenu();
newMethod();
break;

case 3:
printObscure();
printMenu();
newMethod();
break;

case 4:
printTranslated();
printMenu();
newMethod();
break;

case 0:
kill();
break;

default:
throw new InputMismatchException();
}

} catch (InputMismatchException e) {
System.out.println("You must use numbers.");
newMethod();
}

sc.close();

}

Change your main method like this

 public static void main(String[] args) {

File file = new File("./lovecraft.txt");
File readObscure = new File("./obscure.txt");
printMenu();

//Call new Method
newMethod();

}

关于java - 无法让我的 MismatchException 与 switch 一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59803115/

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