+ 1

help please !

hello I am a beginner in java and I want to solve a problem, I want to do a questionnaire question / answer but my program just shows the last entered value! solution or another suggestion thanks import java.util.Scanner; public class projet2 { static Scanner sc= new Scanner(System.in); public static void main(String[] args) { int x=1,n=3, res=0,ress=0,y=2; String Q; String []tab2=new String[x]; String []tab3=new String[y]; String []tab4=new String[1]; question2(tab2,tab3,tab4,n); System.out.println(); afficher(tab2); afficher(tab3); } //-----------------question---------------- public static void question2(String []tab,String[]tab2, String[]tab3,int n){ String Q; int res=0; while(res<n){ for(int i=0;i<tab.length;i++) { System.out.println(" question"); Q=sc.nextLine(); tab[i]=Q; for(int j=0;j<tab2.length;j++){ System.out.println("Answer"); tab2[j]=sc.nextLine(); } BR(tab3); } res++; } } //-----------------quest---------------- public static void question(String []tab){ String Q; for(int i=0;i<tab.length;i++){ System.out.println("Entrez une question"); Q=sc.nextLine(); tab[i]=Q; } } //-----------------afficher---------------- public static void afficher(String []tab){ for(int i=0;i<tab.length;i++){ System.out.println(tab[i]); } } //-----------------la bonne reponse---------------- public static void BR(String[]tab){ String Q; for(int i=0;i<tab.length;i++){ System.out.println("true answer : "); Q=sc.nextLine(); tab[i]=Q; } } }

2nd Jan 2018, 9:58 AM
TARCHID Bouabdellah
TARCHID Bouabdellah - avatar
22 Answers
2nd Jan 2018, 3:52 PM
John Wells
John Wells - avatar
+ 5
You definitely broke it in many places. The only change that appears to worth the cost is putting the numbers on the prompts. Your changes to answers generates an exception anytime you have more than 2 questions. You no longer keep track of your good answers despite asking the user for them two times for each question.
4th Jan 2018, 2:44 AM
John Wells
John Wells - avatar
+ 2
hello, happy new year and thank you for your answer. I want to make a questionaire of several questions / answers and I chose 3 questions and 2 answers just to test my program my goal is to enter questions / answers and reads after tab [] it is to ask the question tab3 [] for the answers and tab4 [] it is to give the good answer among the answers seized
2nd Jan 2018, 11:28 AM
TARCHID Bouabdellah
TARCHID Bouabdellah - avatar
+ 2
thank you, I am a beginner in java and I have not yet arrived at the class and objects.
2nd Jan 2018, 12:03 PM
TARCHID Bouabdellah
TARCHID Bouabdellah - avatar
+ 2
What error and how did you change it?
3rd Jan 2018, 9:05 PM
John Wells
John Wells - avatar
+ 1
From the looks of things, I'd guess you are overwriting your previous questions. You loop 3 times in question2 each time tossing the input from the previous loop to save this loops input.
2nd Jan 2018, 10:51 AM
John Wells
John Wells - avatar
+ 1
How many total questions do you want? How many answers do they each have? Based on your code, I'm guessing 3 questions with 2 answers each. If so, tab2 should have 3 elements and tab3 should have 6. tab4 appears to have another answer per question. Is it meant to be different or same as tab3 values? If same, instead enter number 0/1 indicating which one is right. If different, put in tab3 with 9 elements. To get to answers for question 0 to 2 loop for i = 0 and 1 multiply question number by 2 and add i. So tab2[2] is last question and tab3[4 and 5] are the answers.
2nd Jan 2018, 11:08 AM
John Wells
John Wells - avatar
+ 1
Names matter. If you stay with arrays, name them so everyone knows what it contains. String questions = new String[maxQuestions] helps everyone reading your code.
2nd Jan 2018, 11:41 AM
John Wells
John Wells - avatar
+ 1
You might wish to consider having multiple answer arrays or matrix. answers[maxQuestions][maxAnswerPerQuestion] gives one slot per question for the answers with multiple answers allowed. A empty string "" could allow 2 answers for one question and 6 for another.
2nd Jan 2018, 11:47 AM
John Wells
John Wells - avatar
+ 1
yes I understand I'm trying to change my code and do what you tell me
2nd Jan 2018, 12:34 PM
TARCHID Bouabdellah
TARCHID Bouabdellah - avatar
+ 1
import java.util.Scanner; public class projet2 { public static Scanner sc= new Scanner(System.in); public static void main(String[] args) { int maxQuestion=1,maxAnswerPerQuestion=2, n=3;// n is number of question/answers String[] question=new String[maxQuestion]; String[]answers=new String[maxAnswerPerQuestion]; String[]goodAnswer=new String[1]; question2(question,answers,goodAnswer,n); System.out.println(); afficher(question); afficher(answers); } //---------question---------------- public static void question2(String []tab,String[]tab2, String[]tab3,int n){ String Q; int res=0; while(res<n){ for(int i=0;i<tab.length;i++) { System.out.println(" question"); Q=sc.nextLine(); tab[i]=Q; for(int j=0;j<tab2.length;j++){ System.out.println("Answer"); tab2[j]=sc.nextLine(); } BR(tab3); } res++; } } //---------quest---------------- public static void question(String []tab){ String Q; for(int i=0;i<tab.length;i++){ System.out.println("Entrez une question"); Q=sc.nextLine(); tab[i]=Q; } } //--------display---------------- public static void afficher(String []tab){ for(int i=0;i<tab.length;i++){ System.out.println(tab[i]); } } //-------good answer----------- public static void BR(String[]tab){ String Q; for(int i=0;i<tab.length;i++){ System.out.println("good answer : "); Q=sc.nextLine(); tab[i]=Q; } } } ;
2nd Jan 2018, 12:41 PM
TARCHID Bouabdellah
TARCHID Bouabdellah - avatar
2nd Jan 2018, 2:54 PM
TARCHID Bouabdellah
TARCHID Bouabdellah - avatar
+ 1
thank you for your help, the code works very well and I made some changes. but when I wanted to increase the number of questions, for example 15 or 20 questions he showed me an error !! https://code.sololearn.com/cvUh4AzyrNsr/#java
3rd Jan 2018, 9:00 PM
TARCHID Bouabdellah
TARCHID Bouabdellah - avatar
+ 1
My code followed your example of doing the output. You attempted to do it that way originally. I've updated it.
5th Jan 2018, 10:10 PM
John Wells
John Wells - avatar
0
I suggest posting a link to your code. On app, there is a + insert that allows you to do so. Using web or app, you can paste web site link into post text entry. We can help easier if we don't have to create a copy of your code. Next, give the inputs you are using and the outputs you get so we can duplicate your run. Finally, list the outputs you expected so we know how you wanted the code to work. This post has lots of suggestions for getting help. https://www.sololearn.com/Discuss/333866/?ref=app
2nd Jan 2018, 10:31 AM
John Wells
John Wells - avatar
0
Happy New Year. You're welcome. What do you know of Java so far? Classes would make this easiest.
2nd Jan 2018, 11:36 AM
John Wells
John Wells - avatar
0
Do you understand my matrix answers array?
2nd Jan 2018, 12:05 PM
John Wells
John Wells - avatar
0
Third array int rightAnswer[maxQuestion] picks correct array index of answer.
2nd Jan 2018, 12:41 PM
John Wells
John Wells - avatar
0
please link code
2nd Jan 2018, 12:43 PM
John Wells
John Wells - avatar
0
I made changes on the display method but the code you gave me shows all the questions you entered first and after the answers on my code when I do 2 questions and 2 answers works fine but when I increase the number of questions it shows me an error and it shows me the following message Exception in the "main" thread java.lang.ArrayIndexOutOfBoundsException: 4 to projectQCM.DemiProjectTest.main (DemiProjectTest.java:17)
5th Jan 2018, 9:17 PM
TARCHID Bouabdellah
TARCHID Bouabdellah - avatar