- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在替换字符串时遇到问题。
这是 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/
我在替换字符串时遇到问题。 这是 Question 类方法。 public class Question { private String text; private String answer; /
我已尝试找到执行此操作的方法并在此处在线搜索,但找不到可帮助我解决此问题的示例。 我正在读取大型 csv 中的行并将每一行更改为列表。问题是数据源不是很干净。有时它有空字符串或错误数据,发生这种情况时
我正在制作一个小文字游戏,其中缺少一个单词,如果您在输入字段中填写正确的答案,它就会变成绿色 我想向此代码添加功能,但我不知道如何添加我想编辑它,所以如果你输入错误的答案,它就会变成红色 此刻,它只会
我正在将 TExtView、edittext 和 TExtView 膨胀到 listView 的适配器,以解决如下不同的问题: 你叫什么名字______。泰姬陵在 _______ 国家。_______
所以我有这一行: ArrayList operations(); 现在,这是我的 ArrayList 类和我的操作结构: typedef struct Operation { char key
我正在准备 Java 期末考试。我遇到一道题,需要填空。 需要填空的相关代码 ____ ____ C {} ____ B extends ____ , C {} 问题是:我可以插入所有这些空白来编译我
我正在尝试在 Objective C 中为 iOS 实现类似的功能。 填空(UITextField)应该是内联的,并且应该能够有自己的inputType。 此外,每个 View 都是 UITableV
这是一个有趣的问题。 Fill in the blanks of while loop without using any extra variables or functions of what i
我是一名优秀的程序员,十分优秀!