Thursday, October 22, 2009

Quiz code in java

public class code

{
public static void main(String args[])throws Exception
{

  int numberofcorrect = 0;

  for(int x = 0; x<10; x++)
  {
   String[]questions = {"1. 5+5=?","2. 10+15=?","3. 4+8=?",

                        "4. 51+52=?","5. 10+91=?","6. 13+12=?",
                        "7. 8*2=?","8. 5*2=?","9. 2*7=?",
                        "10.8*9=?"};

   String[]answers = {"a. 10 b. 45 c. 65" , "a.45 b. 25 c.74",
                      "a.5 b.12 c.89" , "a.103 b.145 c.147",
                      "a.125 b.104 c.101" , "a.48 b.25 c.87",
                      "a.92 b.61 c.16" , "a.10 b.47 c.40",
                      "a.13 b.17 c.14" , "a.72 b.82 c.42",};

   char[]trueanswers = {'a','b','b','a','c','b','c','a','c','a'};


  System.out.println(questions[x]);
  System.out.println(answers[x]);


   char input;
   input =(char)System.in.read();System.in.read();System.in.read();

  if(input == trueanswers[x])
  {
   System.out.println("you’re correct!!");
   numberofcorrect++;
  }
  else
  {
   System.out.println("sorry!, you’re wrong!!");
   System.out.println("the correct answer is letter " + trueanswers[x]);
  }

}
  System.out.println("\n You had " + numberofcorrect + " correct answers out of 10 items.\n ");

  if(numberofcorrect>=5)
  {
   System.out.println("Good job,. You've passed the quiz!!!");
  }
  else
  {
   System.out.println("sorry,.  you did'nt passed the quiz!!");}
  }

}
} 




Guessing Game code

public class GuessingGame{

    public static void main(String[]args)throws Exception{


String a="Plan with us";
String b="p**n w*t* u*";

    System.out.println(b);
    while(b.indexOf('*')!=-1)
    {
    char e;
    e=(char)System.in.read();System.in.read();System.in.read();
    int position=a.indexOf(e);
   
    if (position==-1)
    {
    System.out.println("Try Again");
    }
    else
    {
    b=b.substring(0,position)+e+b.substring(position+1,b.length());

    System.out.println(b);

}
}
}
}

Array code sample

public class pizza{

    public static void main(String[]args)throws Exception{
   
        double itemprice;
        char pizza;
        char[]size = {'s','m','l','x'};
        double[]price = {16.99,8.99,12.50,15.00};
                                                                                                 
    pizza = (char)System.in.read();
    System.in.read();System.in.read();

    itemprice = (double)System.in.read();
   
    for(int x=0;x<4;x++)
    {
    if (pizza==size[x])
    {
    itemprice=price[x];
    }
    }
    System.out.print(itemprice);

    }
}