查找成绩并折算后输出

来源:互联网 发布:张卫健眼睛知乎 编辑:程序博客网 时间:2024/04/28 15:26

编写程序从键盘输入文件中的姓名和成绩,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其成绩,并按照21%折算后输出。

输入样例:

Smith  67Anderson  75Lewis  83Cook  58David  96nonameLewis

输出样例:

17.43


方法一:

import java.text.DecimalFormat;import java.util.HashMap;import java.util.Scanner;public class Score{public static void main(String[] args){DecimalFormat df=new DecimalFormat("#.##");Scanner in=new Scanner(System.in);//泛型不能使用基本数据类型,可以使用基本数据类型的包装类来表示基本数据类型HashMap<String,Integer> student=new HashMap<String,Integer>();while(in.hasNext()){String name=in.next();if(name.equals("noname")){break;}int score=in.nextInt();student.put(name, score);}String find=in.next();if(student.containsKey(find)){int score1=student.get(find);System.out.println(df.format(score1*0.21));}else{System.out.println("Not found.");}}}

方法二:

import java.text.DecimalFormat;import java.util.HashMap;import java.util.Scanner;public class Score {public static void main(String[] args) {HashMap<String,String> hp=new HashMap<String,String>();Scanner in=new Scanner(System.in);DecimalFormat df=new DecimalFormat("#.##");String name;String score;while(true){name=in.next();if(name.equals("noname")){break;}score=in.next();hp.put(name, score);}String find=in.next();if(hp.containsKey(find)){int f=Integer.parseInt(hp.get(find)); //将字符串类型转换为int型System.out.println(df.format(f*0.21));}else{System.out.println("Not found.");}}}




0 0