• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner is not working  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
So this is my first post , cause i couldn't find solution on this.
How can i put here an array answers[][] index to correct answer?
Also input = scan.nextLine() ; is not working for some resson.

I wanted to do so when people enters "correct answer" it +1 to total and show up at the end.
For example:

  • 3+1 = four





  • Thanks in advance!
     
    Ranch Hand
    Posts: 64
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You need to create Scanner object before as

    and in this line it should be you have written capital 'Answers' while you have declared small 'answers'
     
    Kristers Drozds
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sankalp Bhagat wrote:You need to create Scanner object before as

    and in this line it should be you have written capital 'Answers' while you have declared small 'answers'

    Thanks buddy, got everything going now with scanner.
    How can i make it so IF take a correct answer from array?




     
    Kristers Drozds
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    delete this message please!
     
    Marshal
    Posts: 56610
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch

    Please use the code button rather than writing code tags by hand; I have corrected that. Please don't quote the whole of a post; there is no need to say the same thing twice.
     
    Campbell Ritchie
    Marshal
    Posts: 56610
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We don't delete messages unless there is something wrong with them. They might be of interest to other readers.
     
    Kristers Drozds
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So i found solution to all this, I made another array with correct answers and took the same index of question so it compares the answer of user . If it makes sense to someone
    Anyone could maybe help with my last issue of my course work.
    1) I got 5 questions in array, how can i take randomly 3 to display?
     
    Campbell Ritchie
    Marshal
    Posts: 56610
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Move 99% of that code out of the main method. Your code style is poor. Put some spaces between things. And }s should be on a line by themselves.
    Yes, that suggestion of the array of arrays will work.

    There are several ways you can choose three out of five “at random”. You can shuffle the array and choose the first three. You can create a Set<Integer> and add “random” numbers to it until its size is 3. You can create a Question class with a boolean alreadyUsed field and use “random” numbers until you have asked 3 questions.
     
    Kristers Drozds
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey, i did clean up my code a bit with { } putting them into their own lanes and made some spaces between codes.
    So in school this semester we don't have anything around random, so i got no idea how to get more than 1 randomize from array.
    Does it matters if code is in main or not?
    Could you please help me?

    Thanks in advance , Kristers!
     
    Campbell Ritchie
    Marshal
    Posts: 56610
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You appear to know how to use a Random object already.Yes, it does matter where the code is; did you read the link I gave you?

    Iterate the array; for each position choose a “random” number. Swap the array element at the current position with that at the “random” position. That is how you can shuffle an array. It is not very secure but it will do nicely for what you need.
     
    Kristers Drozds
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks so much Ritchie.
    Thous links you put in, are so helpful.
    Did clear my code up as well moved everything out of main...
    But still didn't understand how to make decent random...
    I tried to output:
    * 3 questions with the right answers, but didn't succeed .


     
    Campbell Ritchie
    Marshal
    Posts: 56610
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    At this stage, a Random object is near enough to random.

    You have a problem; you have parallel arrays. If you swap elements in one array, you must do the same in the other. You will soon come to hate parallel arrays (I hope). If you have a Question class which includes the answer (sorry I forgot to mention that earlier) you will find things a lot easier. You can create a Question[] array.
     
    Kristers Drozds
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    did try my best , even were watching youtube videos... nop
     
    Kristers Drozds
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So i got random working now, but sometimes it displays 2 the same questions, how to deal with it?


     
    Campbell Ritchie
    Marshal
    Posts: 56610
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have already made some suggestions. Random numbers are allowed to be repeated; they do not retain a memory of the previous value.
    One thing that is supposed to be random is tossing a coin. I once won enough money for several pints of beer by betting that a coin would continue to come up heads. People said, “What about the law of averages?” but I knew better. I took no notice and won the £10.

    I had read an article in Scientific American which says there is no such thing as the law of averages. You can ask question 3 thrice running just by random selection. That will happen once in 5³ tries. But it is not predictable when it will happen.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!