Jump to content

Array Help

- - - - -

  • Please log in to reply
2 replies to this topic

#1
red

red

    Newbie

  • Members
  • Pip
  • 4 posts
Hello,
I am trying to write code to store integers (scores), print them out, and then display the number of scores entered. However, if you enter 3 integers it displays the 3 you entered, 2 zero's, and then tells you you entered 4 scores?! Im very confused here and have tried various if statements, loops, decrementors, etc and I am still at a loss for a solution. This is the first part of a larger assignment.

Any help would be appreciated!!





import javax.swing.*;


public class Scores {


  private final static int SIZE = 25;



  public static void main(String[] args)  {


    int [] scores = new int[SIZE];

    int count = 0;

    double average = 0.0;


    count = loadArray(scores);

    average = determineAverage(scores, count);

    displayResults(scores, count, average);

    System.exit(0);

  }



//returns the count of the scores entered

  private static int loadArray(int[]n)  {

  	int s = 0;

  	int i;

s = Integer.parseInt(JOptionPane.showInputDialog(null, "Input test score"));

if(s == -1){

	break ;

} else {

	n[i] = s;

	i++;

}


	//FYI returns the count plus one so that we include the zero position...

	return(i+1);  // so file compiles

  }



//displays all of the results

 private static void displayResults(int[] n, int cnt, double ave)  {




  	for(int i = 0; i<=cnt; i++){


  		System.out.println(n[i]);

  		if(n[i]%10 == 0){

  			System.out.println();

  		}

  	}


	System.out.println("The total number of test scores" + cnt);

//	int outstanding = determineOutstanding(0);

	//System.out.println("The number of Outstanding Scores" + outstanding);

}



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Try returning i-1.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I didn't try this and it's 1 am so it could be wrong


import javax.swing.*;

public class Scores {

  private final static int SIZE = 25;


  public static void main(String[] args)  {

    int [] scores = new int[SIZE];
    int count;
    double average = 0.0;

    count = loadArray(scores);
    average = determineAverage(scores, count);
    displayResults(scores, count, average);

  }


//returns the count of the scores entered
  private static int loadArray(int[]n)  {
  	int s = 0;
  	int i=0;
s = Integer.parseInt(JOptionPane.showInputDialog(null, "Input test score"));
    while(s!=-1){
	n[i] = s;
	i++;
        try{
            s = Integer.parseInt(JOptionPane.showInputDialog(null, "Input test score"));
        } catch(NumberFormatException e){
            break;
        }
    }

	return i;
  }


//displays all of the results
 private static void displayResults(int[] n, int cnt, double ave)  {
  	for(int i = 0; i<cnt; i++){

  		System.out.println(n[i]);
  		if(n[i]%10 == 0){
  			System.out.println();
  		}
  	}

	System.out.println("The total number of test scores" + cnt);
//	int outstanding = determineOutstanding(0);
	//System.out.println("The number of Outstanding Scores" + outstanding);
}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users