• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ARRAYS AND STRINGS

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am trying to do a question with no luck. This is the question that I am trying to do.
A) Write a program that displays a multiple choice quiz of 10 questions. Each question has one correct answer and three possible answers. Verify that the user enters only A, B, or C. as the answer. Store the correct answers in an array. Store the user's answers in a second array. If the user responds to a question correctly, display "Correct"; otherwise display "The correct answer is" and the letter of the correct answer. Determine what to display by comparing the response to the array of correct answers.
B) Modify the quiz class so that it displays the number of correct answers after the user answers 10 questions. Determine the score by comparing the two arrays.
This is what I have done so far:

/*package assignment4;

public class Quiz
{
public static void main(String[] args)throws Exception
{
char [] correctAns= {'A','B','C','D','A','B','C','D','A','B'};
char []stuAns = new char [10];
int x, tot = 0;
char ans;
System.out.println ("The following test has 10 multiple choice questions");
do
{ tot = 0;
for (x = 0; x< stuAns.length; ++x)
{
System.out.print ("Enter your answer for question " + (x+1)+ " ");
stuAns[x] = (char)System.in.read();
System.in.skip(System.in.available());
stuAns[x] = Character.toUpperCase(stuAns[x]);
if ( stuAns[x] == correctAns[x])
tot = tot + 1;
}
System.out.println ("You got " + tot +" right and " + (10 - tot) + " wrong");
System.out.println ("Do you want to try again ? ( 'Y' or 'N') ");
ans = (char)System.in.read();
System.in.skip(System.in.available());
} while (ans == 'y' || ans =='Y') ;




}
}*/

Am I on the right track here.
I don't know how to get my question in there any where.
Any help that you could give me would be greatly appreciated.
thanks
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd store each question and answers set in 10 diferent String arrays of 4, for example

String[] one = {"What is your favorite color?", "red", "blue", "yellow"};

Store the correct answers and user answers in a int arrays, for example,

int[] correct = {1, 2, 3,...}; // 1=A, 2=B, 3=C
int[] user_thinks = new int[10];
If the user chooses answer B for question 2 => user_thinks[1] = 2;
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi J, welcome to JavaRanch.

Unfortunately, you have posted your Java In General questions in a Servlets forum.

This forum is for Servlets questions, and since you question is not about Servlets, I am going to move this to the Java In General (Beginner) forum.

Thanks

Mark
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic