如何在Java中填写空白?

问题描述 投票:-1回答:1

关于这个问题。我在为FillInQuestion类获取所需的输出时遇到麻烦。到目前为止,这就是我所拥有的。如何在下面的代码中实现replaceAll()方法?谁能帮我吗?

我的输出:

The second question is using FillInQuestion.
_T_h_e_ _i_n_v_e_n_t_o_r_ _o_f_ _J_a_v_a_ _w_a_s_ ___J_a_m_e_s_ _G_o_s_l_i_n_g___._
The inventor of Java was _______________.    WAS EXPECTED.
Niklaus Wirth?
false
false WAS EXPECTED.
James Gosling?
false
true WAS EXPECTED.

我的代码:

class FillInQuestion extends Question
{
public FillInQuestion(String questionText)
   {
       char underscore = '_' ;
       //-----------Start below here. To do: approximate lines of code = 7
       //-----------Start below here. To do: approximate lines of code = 7
       // 1. find position of first underscore, hint indexOf(char)
       //Note, indexOf(char) and indexOf(char, from) can be used here
       int underscore1 = questionText.indexOf(underscore);
       //2. second underscore, hint indexOf(char, from)
       int underscore2 = questionText.indexOf(underscore, underscore1);
       //3. extract the answer string Hint: use substring 
       String answer = questionText.substring(underscore1,underscore2);
       //4. setAnswer (see superclass Question - remember you inherit all variables and methods
       setAnswer(answer);
       //5. String underscores = ... underscores string as long as answer string is
       //replaceAll() method of class String helps, or you can do it in a loop
       String underscores = answer.replaceAll(answer, "_");
       //6. replace answer embedded in questionText with the underscores before setting text string
       String text = questionText.replace(answer,underscores);
       //7. setQuestion to set text variable
       setQuestion(text);
       //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
       //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
   }
}
java inheritance
1个回答
0
投票

您是否要从问题第二行中给出的字符串中提取詹姆斯·高斯林

© www.soinside.com 2019 - 2024. All rights reserved.