|
|
| Methods in Java |
Expert:Danny
Hello. Currently i am involved in a group project and we have each been given a specific part of code to create our joint programI however have been given the job to create a method for storing and recalling 5 questions, which each are lalabeledith 4 answers, i was hoping it would end up looking up something similar to this.
Question 1:
Question will be here?
1) This will be possible answer 1 2) This will be possible answer 2 3) This will be possible answer 3 4) This will be possible answer 4
Your answer: Correct answer: The correct answer will be displayed here Your score: 0/10
I however, have no idea on how to create or run a method, we have begun to learn how to do this in class, but i'm not doing so well on it atm, so i was wondering if someone would be kind enuf to give me a guideline on how i would go about coding this, or if someone could show me or do it for me or whatever, you know what i mean, could someone help me please?
Thanks!
|
| Answers |
Hi Friend,
Try the following code:
import java.io.*; import java.util.*;
class Test{ String question; String ans; String op1; String op2; String op3; String op4; Test(String question,String ans,String op1,String op2,String op3,String op4){ this.question = question; this.ans = ans; this.op1=op1; this.op2=op2; this.op3=op3; this.op4=op4; } public String getQuestion(){ return question; } public String getAns(){ return ans; } public String getOp1(){ return op1; } public String getOp2(){ return op2; } public String getOp3(){ return op3; } public String getOp4(){ return op4; } } public class AskQuestions{ public static void main(String[]args){ Scanner input=new Scanner(System.in); int count=0; int i=0; ArrayList<Test> list = new ArrayList<Test>(); list.add(new Test("Where is Taj Mahal", "Agra", "Mumbai","Delhi","Mathura","Agra")); list.add(new Test("Where is Red Fort", "Delhi", "Mumbai","Delhi","Amritsar","Agra")); list.add(new Test("Where is Golden Temple", "Amritsar", "Mumbai","Delhi","Amritsar","Agra")); list.add(new Test("Where is Eiffel Tower", "Paris", "New York","Sydney","Washington","Paris")); list.add(new Test("Where is Statue of Liberty", "New York", "New York","Sydney","Washington","Paris")); for (Test s : list){ i=i+1; System.out.println("Question"+i+" "+s.getQuestion()); System.out.println(); System.out.println("Option 1 "+s.getOp1()); System.out.println("Option 2 "+s.getOp2()); System.out.println("Option 3 "+s.getOp3()); System.out.println("Option 4 "+s.getOp4()); System.out.println(); System.out.print("Your Answer: "); String answer=input.nextLine(); if(answer.equals(s.getAns())){ count=count+2; } System.out.println("Correct Answer: "+s.getAns()); System.out.println(); } System.out.println("Your result is: "+count); } }
Thanks
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|