Pano po gawin to mga kuya in 2d array?
Attachments
-
You do not have permission to view the full content of this post. Log in or register now.
public class TestQuestion
{
public static void main(String [] args){
String question1 = "Which one of these is not a primitive data type?"
"\n[A] Integer", "\n Char", "\n[C] String\n"
String question2 = "Java source code is stored in files with what extension?",
+"\n[A].class", "\n.java", "\n[C].jav\n"
Question [] questions = {new Question (question1, "c"),
new Question (question2, "b")
};
takeTest(questions);
}
public static void takeTest(Question [] questions){
int score = 0;
Scanner userInput = new Scanner(System.in);
for(int i = 0; i < questions.length; i++){
System.out.println(questions.questionPrompt);
String anwser = userInput.nextLine();
if(anwser.equals(questions.anwserPrompt)){
score++;
}
}
System.out.println("You Got "+score+ "/" +questions.length);
}
}
Ganito nga po yung nagawa ko kuya.gagawing 2d array 'yung arguments ng Question function, o ilalagay sa 2d array mismo yung variables ng q1, q2? kung sa argument , parang eto ba balak mo?
Java:String var[][] = { {"This is question 1", "This is question 2"}, {"answer to 1", "answer to 2"} }
static String question [][] = new String[][]{
{"Which one of these is not a primitive data type?", "\n[A] Integer", "[B] Char", "[C] String\n"},
{"Java source code is stored in files with what extension?", "\n[A].class", "[B].java", "[C].jav\n"},
public static void main (String [] args){
for(int i = 0; i < 2; i++){
for(int j =0; j< 5; j++){
System.out.println(question[i][j]);
}
}
}
}
Ganito nga po yung nagawa ko kuya.
Code:static String question [][] = new String[][]{ {"Which one of these is not a primitive data type?", "\n[A] Integer", "[B] Char", "[C] String\n"}, {"Java source code is stored in files with what extension?", "\n[A].class", "[B].java", "[C].jav\n"}, public static void main (String [] args){ for(int i = 0; i < 2; i++){ for(int j =0; j< 5; j++){ System.out.println(question[i][j]); } } } }
static String question [][] = new String[][]{
{"Which one of these is not a primitive data type?" + "\n[A] Integer" + "[B] Char" + "[C] String\n"},
{"Java source code is stored in files with what extension?"+ "\n[A].class"+ "[B].java"+ "[C].jav\n"}
...
Pano po kuya pa sample namn po. Salamat.nasagot mo na tanong mo okay na yan, ipasa mo na lang sa Question class mo tapps modify mo 'yung parameter mo as 2d array, pero matanong ko lang ganito ba talaga mo balak gamitin 'yung array? mas okay kasi kung i-concat mo na lang sila sa iisang string kung ipi-print lang 'yung arrays ng magkakasabay, more efficient saka less complicated gaya nito:
Code:static String question [][] = new String[][]{ {"Which one of these is not a primitive data type?" + "\n[A] Integer" + "[B] Char" + "[C] String\n"}, {"Java source code is stored in files with what extension?"+ "\n[A].class"+ "[B].java"+ "[C].jav\n"} ...
static String var[][] = {
// Questions
{
"What is the extension of java source code?",
"Which is a c/cpp compiler?",
},
// Choices
{
"[a] jav\n" +
"[b] java\n" +
"[c] class\n" +
"[d] cls",
"[a] gcc\n" +
"[b] rustc\n" +
"[c] ecj\n" +
"[d] none of the above",
}
};
for (int i = 0; i < var[0].length; i++)
{
System.out.println(var[0][i]); // print question
System.out.println(var[1][i]); // print choices
}
Kuya pano yung kada isa tanong lang yung ma priprint saka what do you mean by hardcoding yung "0" at "1"?ganito gagawin ko kung ako magpa-plano:
Code:static String var[][] = { // Questions { "What is the extension of java source code?", "Which is a c/cpp compiler?", }, // Choices { "[a] jav\n" + "[b] java\n" + "[c] class\n" + "[d] cls", "[a] gcc\n" + "[b] rustc\n" + "[c] ecj\n" + "[d] none of the above", } };
less compilcated 'yung setup
para i-print:
Code:for (int i = 0; i < var[0].length; i++) { System.out.println(var[0][i]); // print question System.out.println(var[1][i]); // print choices }
dahil determined naman kung ilan 'yung ginamit ko na array, ihinardcode ko na yung "0" at "1"
i-modify mo na lang kung paano mo iha-handle yan , okay na yan or kung may iba ka pang formatting na gustong gawin, mas madali pati
Kuya pwedr paki explain po medyo nalilito po kasi ako salamat po.ganito gagawin ko kung ako magpa-plano:
Code:static String var[][] = { // Questions { "What is the extension of java source code?", "Which is a c/cpp compiler?", }, // Choices { "[a] jav\n" + "[b] java\n" + "[c] class\n" + "[d] cls", "[a] gcc\n" + "[b] rustc\n" + "[c] ecj\n" + "[d] none of the above", } };
less compilcated 'yung setup
para i-print:
Code:for (int i = 0; i < var[0].length; i++) { System.out.println(var[0][i]); // print question System.out.println(var[1][i]); // print choices }
dahil determined naman kung ilan 'yung ginamit ko na array, ihinardcode ko na yung "0" at "1"
i-modify mo na lang kung paano mo iha-handle yan , okay na yan or kung may iba ka pang formatting na gustong gawin, mas madali pati
position of array, yung index 0 mga questions, index 1 naman mga choices, kung ipiprint mo lang kada isang tanong, then call their index number base sa kailangan mo , for example need ko 'yung question number 1, then need ko yung index var[0][0], tapos kung kailangan ko rin iprint 'yung choices then I'll call for var[1][0], base sa structure ng array ko. pwede ka mag experiment, ikaw bahala . kung gusto mo may user interaction , search for System.in or ScannerKuya pano yung kada isa tanong lang yung ma priprint saka what do you mean by hardcoding yung "0" at "1"?
Yun nga po yung my user interaction sample pumili ako ng letter a tapus next question nman yung ma priprint tapus pipili ulet ako ng letter tapus mag priprint ulet ako ng ilang nakuha ko tama pa tulong kuya medyo nalilito talaga ako my gosh hahahaposition of array, yung index 0 mga questions, index 1 naman mga choices, kung ipiprint mo lang kada isang tanong, then call their index number base sa kailangan mo , for example need ko 'yung question number 1, then need ko yung index var[0][0], tapos kung kailangan ko rin iprint 'yung choices then I'll call for var[1][0], base sa structure ng array ko. pwede ka mag experiment, ikaw bahala . kung gusto mo may user interaction , search for System.in or Scanner
Kada need ko ng var[0][0] mag priprint ako lagi?position of array, yung index 0 mga questions, index 1 naman mga choices, kung ipiprint mo lang kada isang tanong, then call their index number base sa kailangan mo , for example need ko 'yung question number 1, then need ko yung index var[0][0], tapos kung kailangan ko rin iprint 'yung choices then I'll call for var[1][0], base sa structure ng array ko. pwede ka mag experiment, ikaw bahala . kung gusto mo may user interaction , search for System.in or Scanner
hmm tingin ko di mo pa kuha yung 2d array, try mo muna basahin to kahit sandali,Kada need ko ng var[0][0] mag priprint ako lagi?
hmm tingin ko di mo pa kuha yung 2d array, try mo muna basahin to kahit sandali,
You do not have permission to view the full content of this post. Log in or register now. , saka mo balikan yung thread
its just your preference, kung need iprint, then print.Kada need ko ng var[0][0] mag priprint ako lagi?
Salamat po kuya.kailangan mo muna intindihin ts , sa tanong mo na:
its just your preference, kung need iprint, then print.
Question [] question kinuha ko para e random
String rand = questions[random.nextInt(questions.length];
Kasi nga class yung question ko from different class diba kaya nag eerror tama po ba yung pag kakaunawa ko.
oo, di pwede ipasa sa "String" class 'yung "Question" class mo kasi magkaibang objects sila.Kuya fireclouu nag try po ako mag random my error sya alam ko pinag mulan ng error kaso hindi ko alam kung pano ayusin yung class kasi na
Code:Question [] question kinuha ko para e random String rand = questions[random.nextInt(questions.length]; Kasi nga class yung question ko from different class diba kaya nag eerror tama po ba yung pag kakaunawa ko.
create variable inside your Question class, with String as a data type tapos 'yun ang gamitin mo, search for "classes in java" kasi broad scope nito kung itatanong mo kung paanoSo anong solution dyan kuya fireclouu