פתרתי טוב (java basics) מה אתם אומרים ?
בס"ד
יש כאן תרגיל שניסיתי לפתור- מי שבא לו לעיין ולהגיד מה הוא חושב - סבבה:
זה התרגיל :
4. Exercises on Array
Exercise GradesAverage (Array): Write a program called GradesAverage, which prompts user for the number of students, reads it from the keyboard, and saves it in an int variable called numStudents. It then prompts user for the grades of each of the students and saves them in an int array called grades. Your program shall check that the grade is between 0 and 100. A sample session is as follow:
Enter the number of students: 3
Enter the grade for student 1: 55
Enter the grade for student 2: 108
Invalid grade, try again... Enter the grade for student 2: 56
Enter the grade for student 3: 57
The average is 56.0
קישור : http://www.ntu.edu.sg/home/ehchua/programming/java/J2a_BasicsExercises.html
זה הפיתרון שלי :
package Exercise1;
import java.util.Scanner;
public class GradeAverage {
Scanner input = new Scanner(System.in);
int numStudents ;
public GradeAverage(){
getStudentsNumber();
//saveGrades();
}
public void getStudentsNumber(){
System.out.print("Enter the of number of students: ");
int numStudents = input.nextInt();
int totalGrades=0;
int[] grades = new int[numStudents];
double average = 0;
for(int i = 0;i< numStudents; i ++){
int studentIndex = i +1;
boolean b = false;
while( b ==false ){
System.out.print("Enter the Grade for student "+ studentIndex +":");
grades = input.nextInt();
if (grades >= 0 && grades <=100){
totalGrades = totalGrades + grades;
b = true;
}
else{
System.out.println("invalide grade, try again please...");
}
}
//System.out.println(totalGrades + " - total");
average = (double)totalGrades / numStudents;
}
System.out.println("The average is: "+average);
}
public static void main(String[] args){
GradeAverage a = new GradeAverage();
}
}
בס"ד
יש כאן תרגיל שניסיתי לפתור- מי שבא לו לעיין ולהגיד מה הוא חושב - סבבה:
זה התרגיל :
4. Exercises on Array
Exercise GradesAverage (Array): Write a program called GradesAverage, which prompts user for the number of students, reads it from the keyboard, and saves it in an int variable called numStudents. It then prompts user for the grades of each of the students and saves them in an int array called grades. Your program shall check that the grade is between 0 and 100. A sample session is as follow:
Enter the number of students: 3
Enter the grade for student 1: 55
Enter the grade for student 2: 108
Invalid grade, try again... Enter the grade for student 2: 56
Enter the grade for student 3: 57
The average is 56.0
קישור : http://www.ntu.edu.sg/home/ehchua/programming/java/J2a_BasicsExercises.html
זה הפיתרון שלי :
package Exercise1;
import java.util.Scanner;
public class GradeAverage {
Scanner input = new Scanner(System.in);
int numStudents ;
public GradeAverage(){
getStudentsNumber();
//saveGrades();
}
public void getStudentsNumber(){
System.out.print("Enter the of number of students: ");
int numStudents = input.nextInt();
int totalGrades=0;
int[] grades = new int[numStudents];
double average = 0;
for(int i = 0;i< numStudents; i ++){
int studentIndex = i +1;
boolean b = false;
while( b ==false ){
System.out.print("Enter the Grade for student "+ studentIndex +":");
grades = input.nextInt();
if (grades >= 0 && grades <=100){
totalGrades = totalGrades + grades;
b = true;
}
else{
System.out.println("invalide grade, try again please...");
}
}
//System.out.println(totalGrades + " - total");
average = (double)totalGrades / numStudents;
}
System.out.println("The average is: "+average);
}
public static void main(String[] args){
GradeAverage a = new GradeAverage();
}
}