What's new
  • Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

Help Java Program

Status
Not open for further replies.

Hanna1664

Leecher
Heto po yung gawa ko kaso String sya pano ko po gagawing na 2d array salamat po.
Code:
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);
    }
}
 

fireclouu

Addict
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"}
}
 

Hanna1664

Leecher
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"}
}
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]);
            }      
        }
    }
}
 

fireclouu

Addict
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]);
            }      
        }
    }
}
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"}
...
 

Hanna1664

Leecher
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"}
...
Pano po kuya pa sample namn po. Salamat.
 

fireclouu

Addict
, Last edited:
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 :)
 

Hanna1664

Leecher
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 pano yung kada isa tanong lang yung ma priprint saka what do you mean by hardcoding yung "0" at "1"?
 

Hanna1664

Leecher
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.
 

fireclouu

Addict
, Last edited:
Kuya pano yung kada isa tanong lang yung ma priprint saka what do you mean by hardcoding yung "0" at "1"?
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

ps. hardcoding means direct placing of "value" instead of variable.
 

Hanna1664

Leecher
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
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 hahaha
 

Hanna1664

Leecher
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
Kada need ko ng var[0][0] mag priprint ako lagi?
 

Hanna1664

Leecher
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.
 

fireclouu

Addict
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.
oo, di pwede ipasa sa "String" class 'yung "Question" class mo kasi magkaibang objects sila.
 

fireclouu

Addict
So anong solution dyan kuya fireclouu
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 paano

ps. lock mo na 'yung thread kung okay na yung original question mo, saka medyo di na ata scope ng original question yung mga follow ups
 
Status
Not open for further replies.

Online statistics

Members online
2,600
Guests online
2,657
Total visitors
5,257

Forum statistics

Threads
850,962
Messages
15,076,626
Members
1,372,343
Top