Python 3 A little help please. Ive got my code more or less planned out, but whe
ID: 3856183 • Letter: P
Question
Python 3
A little help please. Ive got my code more or less planned out, but when I run it and enter the number of grades I want to enter it continues to ask for another grade instead of asking for the correct number and then exiting the loop to continue the code to do the calculations. Though my code is not complete, can someone help me out to get this working as needed?
* Purpose: This program asks for input from the user about the bumber of grades they wish to enter.
* The user is then asked to enter the grades.
* The program will then populate the array with the grades and sort the array and display this info.
* The program will then calculate the average of the grades and display.
* */
import java.util.Scanner;
import java.util.Arrays;
public class StuLaLoopArray
{
public static void main(String[] arg)
{
int numOfGrades = 0;
double grades = 0;
double total = 0;
double popGrades = 0;
Scanner in = new Scanner(System.in);
System.out.println("How many grades would you like to enter? ");
numOfGrades = in.nextInt();
double[] myGrades = new double[0];
int count = 0;
do
{
System.out.println("Enter your grades: ");
grades = in.nextInt();
// array that stores grades
double[] scores = new double[] {grades};
}while (count < grades);
}
}
Explanation / Answer
// i have modified your code. please check the below code.
import java.util.Scanner;
import java.util.Arrays;
public class StuLaLoopArray
{
public static void main(String[] arg)
{
int numOfGrades = 0;
double grades = 0;
double total = 0;
double popGrades = 0;
Scanner in = new Scanner(System.in);
System.out.println("How many grades would you like to enter? ");
numOfGrades = in.nextInt();
double[] myGrades = new double[numOfGrades];
int count = 0;
for (int i=0;i<numOfGrades;i++){
System.out.println("Enter your "+(i+1)+" grades: ");
grades= in.nextInt();
myGrades[i]=grades;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.