Enigmatic Grading---The indexof () method of the string class for multiple choice questions in a Servlet

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.