First look at the use of the IndexOf () method of the String class :
1 Public classFirstDemo1 {2 /** 3 common methods of string in *api4 */ 5 //finds whether the specified string exists6 Public Static voidMain (string[] args) {7String str1 = "ABCDEFGHIJKLMNABC"; 8 //find out if the specified character is present from the beginning9System.out.println (Str1.indexof ("C")); Ten //continue searching from fourth character position onwards OneSystem.out.println (Str1.indexof ("C", 3)); A //The system returns 1 if the character is not in the specified string. -System.out.println (Str1.indexof ("X")); - } the}
Program Run Result:
For a multi-choice, fake with a,b,c,d four options, the correct answer is "ABC",
First, the value of the check box is received using the getparametervalues () method, which is an array: String t3[]=request.getparametervalues ("CheckBox1");
Then, use arrays.tostring (string) to convert the array into a string: string a3=arrays.tostring (T3);
The next step is to make a judgment:
1. Select All right, the string "C" must be in the third digit of the answer string
2. If multiple or wrong, the string "D" must appear in the answer string
3. If not selected, the answer string is empty
4. In addition to the above, the rest of the situation is not complete.
The judging code is as follows:
1String t3[]=request.getparametervalues ("CheckBox1");2String a3=arrays.tostring (T3);3 4 if(t3!=NULL){5 //multiple choice, correct answer ABC6 if(t3.length==4 | | t3.length==0 | | A3.indexof ("D")!=-1) {//Multi-Select or no-choice or wrong-choice7Score=score+0;8 }9 Else if(A3.indexof ("C") ==3) {//All rightTenScore=score+60; One } A Else{ -Score=score+30; - } the } - Else{ -Score=score+0; -}
It seems that the switch (case) method is also possible, but the code is a bit more complicated.
Enigmatic Grading---The indexof () method of the string class for multiple choice questions in a Servlet