ZZULIOJ-1097: calculate the grade point average (function topic) (Java)

Subject description:

Enter certain students in each course of the performance, output grade point average. Results are five input system performance, system performance is converted to five percent performance rules are as follows: 'A' is converted into a score of 95 points per cent, 'B' corresponds to 85 points, C corresponds to 75 points, ' D 'corresponds to 65 points,' E 'corresponds to 40 points. The average score for the output of a real number, retains a decimal. 

A program defined in claim getScore () function, and a main () function, getScore () function returns a level corresponding fraction, the remaining functions implemented in the main () function.
getScore int (char g)

// g converted to the corresponding level and returns the fractional score. 
}

For the submission of C / C ++ code, this problem must be achieved by defining the requirements getScore function and main function, otherwise, to submit a compilation error, to submit a complete program.

 Input:

Input line containing only 'E' letter 'A' ~, each letter represents the course of a performance,  

Output: 

Output grade point average, is a real number, to one decimal place.  

Sample input: 

AABB 

Sample output: 

90.0 

code: 

Do not function writes: ↓↓↓ 

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		Scanner input=new Scanner(System.in);
		String str=input.nextLine();
		char [] a=new char[10];
		for(int i=0;i<str.length();i++)
			a[i]=str.charAt(i);
		double sum=0.0;
		for(int i=0;i<str.length();i++)
		{
			if(a[i]=='A')
				sum+=95;
			if(a[i]=='B')
				sum+=85;
			if(a[i]=='C')
				sum+=75;
			if(a[i]=='D')
				sum+=65;
			if(a[i]=='E')
				sum+=40;
		}
		System.out.printf("%.1f\n",sum*1.0/(str.length()));
		input.close();
	}
}

With function writes: ↓↓↓ 

import java.util.*;
public class Main
{
	public static char getScore(char ch)
	{
		if(ch=='A')
			return 95;
		else if(ch=='B')
			return 85;
		else if(ch=='C')
			return 75;
		else if(ch=='D')
			return 65;
		else if(ch=='E')
			return 40;
		else
			return 0;
	}
	public static void main(String[] args)
	{
		Scanner input=new Scanner(System.in);
		String str=input.nextLine();
		char [] a=new char[10];
		for(int i=0;i<str.length();i++)
			a[i]=str.charAt(i);
		double sum=0;
		for(int i=0;i<str.length();i++)
			sum+=Main.getScore(a[i]);
		System.out.printf("%.1f\n",sum*1.0/(str.length()));
		input.close();
	}
}

 

 

Published 260 original articles · won praise 267 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43823808/article/details/103737701