码迷,mamicode.com
首页 > 其他好文 > 详细

3.1.1

时间:2018-06-08 12:13:22      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:print   number   from   imp   lis   public   table   res   creates   

question:

Write a client that creates a symbol table mapping letter grades to numerical scores, as in the table below, then reads from standard input a list of letter grades and computes and prints the GPA (the average of the numbers corresponding to the grades).

answer:

 

import edu.princeton.cs.algs4.*;

public class GPA
{
    public static void main(String[] args)
    {
        ST<String, Double> st = new ST<String, Double>();
        st.put("A+", 4.33);
        st.put("A", 4.00);
        st.put("A-", 3.67);
        st.put("B+", 3.33);
        st.put("B", 3.00);
        st.put("B-", 2.67);
        st.put("C+", 2.33);
        st.put("C", 2.00); 
        st.put("C-", 1.67);
        st.put("D", 1.00);
        st.put("F", 0.00);
        double sum = 0;
        int n = 0;
        while(!StdIn.isEmpty())
        {
            String grade = StdIn.readString();
            sum += st.get(grade);
            n++;
        }
        StdOut.println(sum/n);
    }
}

 

3.1.1

标签:print   number   from   imp   lis   public   table   res   creates   

原文地址:https://www.cnblogs.com/w-j-c/p/9154422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!