gpt4 book ai didi

java - 填空

转载 作者:行者123 更新时间:2023-12-02 07:03:19 26 4
gpt4 key购买 nike

我在替换字符串时遇到问题。

这是 Question 类方法。

public class Question
{
private String text;
private String answer;

/**
Constructs a question with empty question and answer.
*/
public Question(String qText)
{
text = qText;
answer = "";
}

/**
Sets the answer for this question.
@param correctResponse the answer
*/
public void setAnswer(String correctResponse)
{
answer = correctResponse;
}

/**
Checks a given response for correctness.
@param response the response to check
@return true if the response was correct, false otherwise
*/
public boolean checkAnswer(String response)
{
return response.equals(answer);
}

/**
Displays this question.
*/
public void display()
{
System.out.println(text);
}
}

这就是我的方法

This is my blankQuestions class

import java.util.ArrayList;


public class BlankQuestion extends Question {

public BlankQuestion(String qText) {
return qText.replaceAll("_+\\d+_+", "_____");


String tempSplit[] = questionText.split("_");
setAnswer(tempSplit[1]);

}
public void setAnswer(String correctChoice){
super.setAnswer( correctChoice );

}

@Override
public boolean checkAnswer (String response){
return super.checkAnswer(response);

}

public String toString(){
return super.toString();
}


}

这是我的主课

import java.util.Scanner;  

public class QuestionDemo
{
public static void main(String[] args)
{
Question[] quiz = new Question[2];

BlankQuestion question0 = new BlankQuestion(
"2 + 2 = _4_");
quiz[0] = question0;

BlankQuestion question1 = new BlankQuestion(
"The color of the sky is _blue_.");
quiz[1] = question1;

Scanner in = new Scanner(System.in);
for (Question q : quiz)
{
q.display();
System.out.println("Your answer: ");
String response = in.nextLine();
System.out.println(q.checkAnswer(response));
}
}
}

据我了解,我将[下划线]4[下划线]替换为5x[下划线],这类似于填空,我存储4.并将字符串的部分替换为 _____。不幸的是,这就是我的结果。我认为我的逻辑是对的,但我不知道为什么我的返回不是我期望的。

最佳答案

一次性完成所有操作:

return qText.replaceAll("_+\\d+_+", "_____");

它用五个下划线替换一个或多个下划线,后跟一个或多个数字,后跟一个或多个下划线。

关于java - 填空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16381880/

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