Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

[HELP] Sa pag input ng Loop java.

VirusExcelL

Proficient
Advanced Member
Messages
221
Reaction score
1
Points
28
Morning guys. Pahelp naman papaano mag lagay ng loop dito sa program ko. Always kasi unreachable statement nagagawa ko pag nasa hulian nilalagay ko yung do-while. I messed up bigtime. Hirap ma remedyohan. Need ko lang advice or saan ilalagay yung loop. nagawa ko na kasi yung 40 items na quiz. bale 2 item lang to post ko. hope may tutulong. Tenk yu :D
import java.util.*;
public class Defence {

public static void main(String[] args) {

int scoremo = 0;
int Finalresult = 40;

String[] Multiplechoice1;
Scanner input = new Scanner(System.in);
System.out.println ("Welcome to the Quiz Bee.\n");
System.out.println ("The First Stage of the Quiz is Easy\n");
Multiplechoice1 = new String[6];
Multiplechoice1[0] = "1.Who is the Philippine Natonal Hero?\n";
Multiplechoice1[1] = "A:Jose Rizal";
Multiplechoice1[2] = "B.Lapu,Lapu";
Multiplechoice1[3] = "C.Apolinario Mabini";
Multiplechoice1[4] = "D.Engeng";

String userSelect1 = Multiplechoice1[0];

if(userSelect1==Multiplechoice1[0]){
System.out.println (Multiplechoice1[0]);
System.out.println ("A.Jose Rizal");
System.out.println ("B.Lapu,Lapu");
System.out.println ("C.Apolinario Mabini");
System.out.println ("D.Engeng");
System.out.print ("Your Answer: ");

userSelect1 = input.next();

switch (userSelect1.toUpperCase()){
case "A":
userSelect1 = "Jose Rizal";
scoremo++;

System.out.println ("Correct!\n\n");
System.out.println ("Your total score is: "+scoremo);
break;

case "B":
userSelect1 = "Lapu,Lapu";
System.out.println ("Icorrect!\n\n");
break;

case "C":
userSelect1 = "Apolinario Mabini";
System.out.println ("Incorrect!\n\n");
break;

case "D":
userSelect1 = "Engeng";
System.out.println ("Incorrect!\n\n");

default:
userSelect1 = "incorrect, choice.\n\n";
System.out.println ("Incorrect, choice.\n\n");
break;
}

}
String[] Multiplechoice2;


Multiplechoice2 = new String[5];
Multiplechoice2[0] = "2.Who is the first president?\n\n";
Multiplechoice2[1] = "A.Juan Luna";
Multiplechoice2[2] = "B.Emlio Aguinaldo";
Multiplechoice2[3] = "C.George W. Bush";
Multiplechoice2[4] = "Ferdinand Marcos";

String userSelect2 = Multiplechoice2[0];

if(userSelect2==Multiplechoice2[0]){
System.out.println (Multiplechoice2[0]);
System.out.println ("A.Juan Luna");
System.out.println ("B.Emilio Aguinaldo");
System.out.println ("C.George W. Bush");
System.out.println ("D.Ferdinand Marcos");
System.out.println ();
System.out.print ("Your Answer: ");

userSelect2 = input.next();

switch (userSelect2.toUpperCase()){
case "A":
userSelect2 = "A.Juan Luna";
System.out.println ("Incorrect!\n\n");
break;

case "B":
userSelect2 = "B.Emilio Aguinaldo";
scoremo++;
System.out.println ("Correct!\n\n");
System.out.println ("Your total score is: "+scoremo);
break;

case "C":
userSelect2 = "C.George W. Bush";
System.out.println ("Incorrect!\n\n");
break;

case "D":
userSelect2 = "D.Ferdinand Marcos";
System.out.println ("Incorrect!\n\n");
break;

default:
userSelect2 = "incorrect, choice.\n\n";
System.out.println ("Incorrect, choice.\n\n");
break;
}
}
String[] Multiplechoice3;

Multiplechoice3 = new String[5];
Multiplechoice3[0] = "3. Is a Japanese imperial soldier who commit crimes in the Philippines\n";
Multiplechoice3[1] = "A.Samurai";
Multiplechoice3[2] = "B.Ninja";
Multiplechoice3[3] = "C.Kempetai";
Multiplechoice3[4] = "D.Nihon";

String userSelect3 = Multiplechoice3[0];

if(userSelect3==Multiplechoice3[0]){
System.out.println (Multiplechoice3[0]);
System.out.println ("A.Samurai");
System.out.println ("B.Ninja");
System.out.println ("C.Kempetai");
System.out.println ("D.Nihon");
System.out.println ();
System.out.print ("Your Answer: ");
userSelect3 = input.next();

switch (userSelect3.toUpperCase()){
case "A":
userSelect3 = "A.Samurai";
System.out.println ("Incorrect!\n");
break;

case "B":
userSelect3 = "B.Ninja";
System.out.println ("Incorrect!\n");
break;

case "C":
userSelect3 = "C.Kempetai";
scoremo++;
System.out.println ("Correct!\n");
System.out.println ("Your total score is: " +scoremo);
break;

case "D":
userSelect3 = "D.Nihon";
System.out.println ("Incorrect\n");
break;

default:
userSelect3 = "Incorrect, choice.\n\n";
System.out.println ("Incorrect, choice.\n\n");
break;

}
}
}
}
 
ang java ay may 0-based array index. So ang array na may 40 elements ay mag-start sa 0, at matatapos sa 39. Kapag gumawa ka ng for loop ang "condition" part mo should be:

for(x = 0; x < Finalresult ; x++)

or

for(x = 0; x <= (Finalresult - 1); x++)
 
Ask ko lang po kung saan ko iyan sya ilalagay?
kasi pag nasa hulian kasi yung isang item lang ma loloop nya. gusto ko sana 40 item na ma loop ng user if gusto nya pa mag proceed.
unreachable statement kasi pag sa ibabaw ng code nilagay ko.
 
ung code mo sa nakikita ko dire-diretso ung takbo... hard code tawag dito

not sure if pde mo pa maisingit ang loop dito...

ito nakikita mo pde mong gawin pero re-code ito

1. gawa ka ng class na Question, doon mo ilalagay ung question saka answers
2. tapos sa main class doon ka gagawa ng loop (depende na sayo kung for loop, do-while, while+iterator etc).
3. within the loop if tama ung sagot may variable kang sasalo nung score (if else)
 
ung code mo sa nakikita ko dire-diretso ung takbo... hard code tawag dito

not sure if pde mo pa maisingit ang loop dito...

ito nakikita mo pde mong gawin pero re-code ito

1. gawa ka ng class na Question, doon mo ilalagay ung question saka answers
2. tapos sa main class doon ka gagawa ng loop (depende na sayo kung for loop, do-while, while+iterator etc).
3. within the loop if tama ung sagot may variable kang sasalo nung score (if else)

naku, sakit naman sa ulo to. hehehehe.
sige po thanks.
 
gawa ka muna ng array para sa question, choices ng answer, then answer
and variable para sa score (global?)

then initiate random command, so once na nag random ng certain number example is 1-40 then nag random ng 29
then ganito ang logic

show/print arrayquestion[randomnumber(gawangrandom)]
pati choicesnganswer[randomnumber(gawarinngrandomgenerate)]
since madaming choice make sure gumawa ka ng 4 na array,
then sa verification ng tamang sagot

if(arraryofanswer[randomnumber(whichissame sila ng tatlo)] is equal to numberngchoices.or value.or laman ngsagot)
{
score++
reinitiate mo ulit ung random
}
else
{
print wrong answer
reinitiate mo ulit ung random
}

iencapsulate mo para ma reinitiate mo ung function or ma call mo ulit


import java.util.Scanner;
public class MyClass {

static final int QUESTION = 0;
static final int ANSWER = 1;
static final int START_OF_CHOICES = 2;

//init questions, answers, and choices
static String[][] multiChoice = new String[][]{
{"1. Which country currently emits the most greenhouse gases?", "B", "A. United States", "B. China", "C. India", "D. England"},
{"2. Question 2?", "C", "A. Option 1", "B. Option 2", "C. Option 3", "D. Option 4"},
{"3. Question 3?", "A", "A. Option 1", "B. Option 2", "C. Option 3", "D. Option 4"},
{"4. Question 2?", "D", "A. Option 1", "B. Option 2", "C. Option 3", "D. Option 4"}};

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

//loop through each question
for (int questionIndex = 0; questionIndex < multiChoice.length; questionIndex++) {
//print current question as well as its choices
printQuestion(questionIndex);
do {
//display instruction for input. It's good to put hint like A - D so that the user will now what to enter
System.out.print("\nYour Answer [A - D]: ");

} while (!isCorrectAnswer(questionIndex, input.next().charAt(0))); //continue asking for answer if the user entered an incorrect one
}
}

//method that checks whether the user's answer is correct for a particular question
static boolean isCorrectAnswer(int questionNum, char userAnswer) {
//true if matched, false otherwise
boolean rightAnswer = (userAnswer + "").equalsIgnoreCase(multiChoice[questionNum][ANSWER]);
//equivalent to if rightAnswer is true then display "Correct", else, display "Incorrect"
System.out.println(rightAnswer ? "Correct!\n" : "Incorrect!\n");

return rightAnswer;
}

//method that prints a specific question and its choices
static void printQuestion(int questionNum) {
System.out.println(multiChoice[questionNum][QUESTION]);
int lastColumn = multiChoice[questionNum].length;
for (int x = START_OF_CHOICES; x < lastColumn; x++) {
System.out.println("\t" + multiChoice[questionNum][x]);

}
}
}
 
Last edited:
gawa ka muna ng array para sa question, choices ng answer, then answer
and variable para sa score (global?)

then initiate random command, so once na nag random ng certain number example is 1-40 then nag random ng 29
then ganito ang logic

show/print arrayquestion[randomnumber(gawangrandom)]
pati choicesnganswer[randomnumber(gawarinngrandomgenerate)]
since madaming choice make sure gumawa ka ng 4 na array,
then sa verification ng tamang sagot

if(arraryofanswer[randomnumber(whichissame sila ng tatlo)] is equal to numberngchoices.or value.or laman ngsagot)
{
score++
reinitiate mo ulit ung random
}
else
{
print wrong answer
reinitiate mo ulit ung random
}

iencapsulate mo para ma reinitiate mo ung function or ma call mo ulit

Salamat po dito sir. :D
and also na nag advice sakin. God bless u all :)
 
Salamat po dito sir. :D
and also na nag advice sakin. God bless u all :)

para mas madali ka magawa ng isang system, aralin mo mabuti ung process, step by step kasama na ung logic or pano gumagana ung isang process, saka mo lagyan ng code na naangkop, once na master mo yan basic nlng lahat ,syntax or code nlng aaralin mo which same nman sa lahat ng language nag kaiba lang sa structure. good luck have fun
 
Back
Top Bottom