• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

1D arrays list of numbers from user input

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got an assignment the other day and I have no idea how to even go about starting it, I know, sad times. If someone could send me in the right direction that would be great.

Write a program to maintain a list of the high scores obtained in a game. The program should first ask the user how many scores they want to maintain and then repeatedly accept new scores from the user and should add the score to the list of high scores (in the appropriate position) if it is higher than any of the existing high scores. You must include the following functions:
-initialiseHighScores () which sets all high scores to zero.
-printHighScores() which prints the high scores in the format: “The high scores are 345, 300, 234”, for all exisiting high scores in the list (remember that sometimes it won’t be full).
-higherThan() which takes the high scores and a new score and returns whether the passed score is higher than any of those in the high score list.
-insertScore() which takes the current high score list and a new score and updates it by inserting the new score at the appropriate position in the list
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you know how to start? have you installed a JDK? can you compile a simple "hello world" program?
 
Sarah Candor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I've done a good few assignments, including functions, if and while loops and for statements etc. I have just no idea how to go about this. We use Eclipse
 
Sarah Candor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As in I'm not sure about the arrays side of it, and how from user input I can make sure the numbers go in the right order etc
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start with turning off your computer and thinking about how you'd do it on paper. If you were the program, you'd ask me how many scores I wanted to keep track of. I'd say 8. So you'd need to keep a list of 8 numbers. I'd start by saying 100. Etc etcetc.... then we get to score number 9. I give you a number that's not higher than any numbers..... score 10 is higher than some of them....... score 11 is higher than them all..... Tell stories, and figure out how you'd do it by hand.

Also review this handy dandy tutorial on arrays: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Candor wrote:As in I'm not sure about the arrays side of it, and how from user input I can make sure the numbers go in the right order etc


Well, there's a wonderful class called Arrays (java.util.Arrays), which contains all sorts of useful utilities. I think you may find a method that fits the bill in there.

Also: don't get overwhelmed by thinking you have to know how to do everything right now. It's perfectly possible to write a class without having the first clue what code you're actually going to put in it, as this page shows you.

However, I wholeheartedly second Janeice's advice: Don't start off by coding; start off by thinking - and that'll involve a lot of paper and pencils (and probably an eraser) ... and NO computer.

Winston
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I see you are calling your arrays “1D”. That is unnecessary because Java® only ever supports 1D arrays. You might see arrays declared like this:-
Foo[][] matrix = ...;
but that isn't a 2D array. It is a 1D array of 1D arrays. The type of each element is Foo[].
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you could try something like




This is Un-Tested
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see two problems in that, I am afraid. One is a minor inefficiency, but the other will prevent you reading any numbers at all. You also have some style things, e.g. starting the name of a class with a small letter.
I would suggest you print out the entire array with something like:-
System.out.println(Arrays.toString(myArray));
Then you can confirm you have filled it correctly.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see another problem, I am afraid. Again you will never be able to fill that array. I think you are going to have to try that method and see what happens.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I can see another problem, I am afraid. Again you will never be able to fill that array. I think you are going to have to try that method and see what happens.



it was a two second job.. to point Sarah Candor in the right direction.. what you pointed out should help her with her project.
reply
    Bookmark Topic Watch Topic
  • New Topic