Need help with project that uses arrays
I have a project due that I need some help with. I have no idea where to start with this one, so here's a description of the requirements:
-Get 10 grade percentages from user, example: 79,82,85,94,52,71,88,97,56,65
These numbers should be stored in an array.
-The program should then ask for a letter grade input from the user.
-It should then output how many of of that grade there are. Example: B, There are 3 Bs.
Thanks for the help in advanced, I"m really lost on this one.
Re: Need help with project that uses arrays
Pleas post the code you have so far, along with questions and/or errors you are receiving. If you really don't know where to start, see Trail: Learning the Java Language (The Java™ Tutorials). Otherwise, you will get little help...especially if (and I hope not) you expect someone to just do this for you.
Re: Need help with project that uses arrays
I'm not asking for someone to write this for me, I would rather understand how to do this. I just can't get a grasp on how to implement an array.
Code Java:
import java.util.*;
public class Proj4{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int count = 1;
while (count < 10) {
int count++;
System.out.println("Enter score: ");
int score = Integer.parseItn(s.nextLine());
}
}
}
Re: Need help with project that uses arrays
Re: Need help with project that uses arrays
How do I set up the loop gather the 10 numbers and store them in the array?
Re: Need help with project that uses arrays
The tutorial given above shows how to store values into an array. Your code has the loop, but uses an int variable to save the input value. Change that to use an indexed element of the array. Use the variable: count as the index into the array. One change you'll need to make is to start the index (count) at 0 vs 1.
Re: Need help with project that uses arrays
I know I'm doing something(or lack of) wrong, but I don't know what. Please help! Thanks for your time.
Code Java:
import java.util.*;
public class Proj4{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int count = 0;
while (count < 10) {
int count++;
System.out.println("Enter score: ");
anArray = new int[10];
int score = Integer.parseItn(s.nextLine());
}
System.out.println("Which grade would you like to count? ");
char letter = (s.nextLine()).charAt(0);
if (A)
System.out.println("There are " + score + "As");
else if (B)
System.out.println("There are " + score + "Bs");
else if (C)
System.out.println("There are " + score + "Cs");
else if (D)
System.out.println("There are " + score + "Ds");
else if (F)
System.out.println("There are " + score + "Fs");
else
System.out.println("Error, restart program and enter a valid grade.");
}
}
Re: Need help with project that uses arrays
There are quite a few problems in your code! You need to read about Arrays and there are also some small silly mistakes..
This code will help you move forward:
Code Java:
import java.util.*;
/**
* JavaProgrammingForums.com
*/
public class Proj4 {
public static int score;
public static void main(String[] args) {
int count = 0;
// Setup Array
int[] anArray = new int[10];
Scanner s = new Scanner(System.in);
while (count < 10) {
System.out.println("Enter score: ");
score = Integer.parseInt(s.nextLine());
// Store score in Array
anArray[count] = score;
count++;
}
// Print contents of Array
for (int b = 0; b < anArray.length; b++){
System.out.println(anArray[b]);
}
}
}
Re: Need help with project that uses arrays
Quote:
There are quite a few problems in your code!
For example:
you define count in two places: one outside the while loop and the other inside the loop. You should only define count in ONE place and then use it where you need it.
The same for the array: anArray. Define it outside the loop. Then assign values to it inside the loop as the values are received from the user.
The next part of your code needs to be completely redone. Here you need to prompt the user for what to search for: A or B etc and then you need a loop to look at each of the scores one by one to see if it is in the desired range and if it is, to count it. After the loop is done, print out the results.