在AIX Version 5.3中使用Java和PHP技术进行开发(2)
2008-11-13 08:13:58 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁惧墽鎳撻—鍐偓锝庝簻椤掋垺銇勯幇顖毿撻柟渚垮妼椤粓宕卞Δ鈧獮濠勭磽閸屾艾鈧懓顫濋妸鈺佺疅缂佸顑欓崥瀣煕椤愵偅绶氱紓鍐╂礋濮婂宕掑▎鎴М濠电姭鍋撻梺顒€绉甸幆鐐哄箹濞n剙濡肩紒鎰殜閺屸€愁吋鎼粹€茬敖婵炴垶鎸哥粔鐢稿Φ閸曨垰鍐€妞ゆ劦婢€濞岊亪姊虹紒妯诲蔼闁稿海鏁诲濠氭晲婢跺﹤宓嗛梺缁樺姈缁佹挳宕戦幘璇叉嵍妞ゆ挻绋戞禍鐐叏濡厧浜鹃悗姘炬嫹

ask_specific() 是基类 ask_specific() 上的一个重载类,它在 SurveyQuestion 中基本上不进行任何操作。您可以通过调用继承的 getresponse() 获得输入,这个方法接受来自标准输入的输入,然后将所接收到的字符串直接添加到对象 question_response 集合。
这个基类仅接受单个字符串作为输入,但是只需要通过进行循环并调用 getresponse() 方法,当输入为空或者输入了某个特定的关键字时中止循环,您就可以接受多个字符串,并将它们连接起来,或者顺序地将它们添加到集合中。
另外,通过在其中使用集合,您可以很容易地将回答添加到列表中。当您处理单选按钮或者复选框输入时,能够向集合中添加多项回答是非常有用的。让我们来看一个示例,通过研究 SurveyQuestionRadio 类来分析这些集合如何进行工作。
SurveyQuestionRadio 类
SurveyQuestionRadio 类是更复杂的问题的一个示例。这个类支持提供多个选项的列表,并且允许用户仅从列表中选择一个选项。对于这个处理过程来说,有三个关键的要素:
在创建该对象的一个实例时,您需要填充可用选项的列表。
在输出该问题时,您需要输出可用选项的列表以及每个选项的编号,以便用户能够输入相应的编号以进行选择。
在读取输入时,您需要对输入进行验证,以便确保它位于可能的回答的最小值和最大值之间,如果所提供的信息不正确,则请求再次输入。
要实现这一点,您同样需要读取来自用户的输入,但是您可以在此过程中对输入进行验证。清单 4 是这个类定义的全部内容。接下来,您将更深入地研究一些特定的领域。
清单 4. SurveyQuestionRadio 类源代码import java.io.*;
import java.util.*;
public class SurveyQuestionRadio extends SurveyQuestion {
public SurveyQuestionRadio(String qtext,
String qhelp,
String[] options) {
this.question_text = qtext;
this.question_help = qhelp;
this.question_type = "radio";
for(int i=0;i<options.length;i++) {
this.question_options.add(options[i]);
}
}
public void askspecific() {
Boolean responsestate = false;
Integer opt = 1;
String responsebuffer;
Integer optselect = -1;
System.out.println("Choose from:");
for(Iterator<String> i = this.question_options.iterator(); i.hasNext(); ) {
System.out.println("(" + opt + ") " + i.next());
opt++;
}
while(responsestate == false) {
responsebuffer = this.getresponse();
try {
optselect = Integer.parseInt(responsebuffer);
if ((optselect <= 0) ||
(optselect > opt)) {
System.err.println("Invalid Selection - please try again");
responsestate = false;
}
else {
responsestate = true;
String[] optionstext = (String[]) this.question_options.toArray(new String[0]);
this.question_response.add(optionstext[optselect-1]);
}
} catch (NumberFormatException e) {
System.err.println("Error parsing " + responsebuffer);
responsestate = false;
}
}
}
}
更多精彩
赞助商链接