将此段代码package interExam; public class interClass { public static void main(String args[]) { ComputerExam exam=new ComputerExam(2); exam.comMethod(); exam.displayResult(); } } interface computor{ void add(); void subtract(); void multiply(); void divid(); void display(); } class IntComputer implements computor { int results[]; int num1,num2; IntComputer() { results=new int[4]; } IntComputer(int num1,int num2) { this(); this.num1=num1; this.num2=num2; } public void add() { this.results[0]=this.num1+this.num2; } public void subtract() { this.results[1]=this.num1-this.num2; } public void multiply() { this.results[2]=this.num1*this.num2; } public void divid() { if(this.num2!=0) this.results[3]=this.num1/this.num2; else this.results[3]=-1; } public void display() { System.out.printf("%10d%10d%10d%10d\t", this.results[0],this.results[1],this.results[2],this.results[3]); } } class FloatComputer implements computor { float results[]; float num1,num2; FloatComputer() { results=new float[4]; } FloatComputer(float num1,float num2) { this(); this.num1=num1; this.num2=num2; } public void add() { this.results[0]=this.num1+this.num2; } public void subtract() { this.results[1]=this.num1-this.num2; } public void multiply() { this.results[2]=this.num1*this.num2; } public void divid() { if(this.num2!=0) this.results[3]=this.num1/this.num2; else this.results[3]=-1; } public void display() { System.out.printf("%10.2f%10.2f%10.2f%10.2f\t", this.results[0],this.results[1],this.results[2],this.results[3]); } } class ComputerExam { computor pairNum[]; int N; ComputerExam() { this.N=3; pairNum=new computor[this.N]; } ComputerExam(int N) { this.N=N; pairNum=new computor[this.N]; } void init() { int k; for(k=0;k<this.N;k++) { if(k%2==0) this.pairNum[k]=new IntComputer(k+1,k+20); else this.pairNum[k]=new FloatComputer((float)(k+3*0.8),(float)(k-2+10*0.4)); } } void comMethod() { int k; if(this.pairNum[0]==null) init(); for(k=0;k<this.N;k++) { this.pairNum[k].add(); this.pairNum[k].subtract(); this.pairNum[k].multiply(); this.pairNum[k].divid(); } } void displayResult() { int k; System.out.printf("%10s%10s%10s%10s\n", "add","sub","Mul","did"); for(k=0;k<this.N;k++) { this.pairNum[k].display(); System.out.println(); } } }的输入改为“18+32=”方式,用正则表达式判断输入是否正确,如果正确给出相应结果。
时间: 2024-01-26 16:02:40 浏览: 106
很抱歉,作为AI语言模型,我不能直接运行代码。但是我可以为您解释如何实现将输入改为“18+32=”方式,并用正则表达式判断输入是否正确,如果正确给出相应结果。
首先,在`IntComputer`和`FloatComputer`类中,将`num1`和`num2`的初始化改为从用户输入中获取,例如:
```
IntComputer() {
results = new int[4];
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the first number: ");
num1 = scanner.nextInt();
System.out.print("Please enter the second number: ");
num2 = scanner.nextInt();
}
```
接下来,可以在`ComputerExam`类中添加一个方法,用正则表达式判断用户输入是否符合要求,并返回相应结果,例如:
```
public String getResult(String input) {
String pattern = "(\\d+)\\+(\\d+)=?";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
if (m.find()) {
int num1 = Integer.parseInt(m.group(1));
int num2 = Integer.parseInt(m.group(2));
IntComputer computer = new IntComputer(num1, num2);
computer.add();
computer.display();
return "";
} else {
return "Invalid input";
}
}
```
最后,在`main`方法中调用`getResult`方法来获取用户输入并输出结果,例如:
```
public static void main(String args[]) {
ComputerExam exam = new ComputerExam();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Please enter the equation: ");
String input = scanner.nextLine();
if (input.equals("exit")) {
break;
}
String result = exam.getResult(input);
System.out.println(result);
}
}
```
这样就可以实现将输入改为“18+32=”方式,并用正则表达式判断输入是否正确,如果正确给出相应结果。
阅读全文