我在返回颜色字符串时遇到了麻烦。由于某种原因,它不会返回数字。不确定我是否需要用 else 插入 if 语句的结尾,但我觉得 catch 语句 if 是什么意思。
主班
package edu.computer.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Computer {
public Computer() {
}
public String getProcessor() {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(in);
String num = null;
System.out.println("Type red to print red or blue to print blue");
try {
num = keyboard.readLine();
if (num.equals("red"))
num = "red";
if (num.equals("blue"))
num = "blue";
} catch (IOException e) {
System.out.println("Exception occured!");
}
return num;
}
}
测试班
package edu.computer.test;
public class ComputerTester {
public static void main(String[] args) {
Computer a = new Computer();
a.getProcessor();
}
}