代码改变世界

Java小程序1(2015-8-10)

2015-08-10 13:42  马尔代夫_珍  阅读(232)  评论(0编辑  收藏  举报

从键盘输入一个百分制分数,将其转化为等级分输出。 90以上,输出:等级为A 80-90,输出:等级为B, 70-80,输出:等级为C, 60-70,输出:等级为D, 60分以下,输出:等级为E

1、

import java.util.Scanner;

  public class Test1{  

      public static void main(String[] args){  

            Scanner scan = new Scanner(System.in);  

            int score = scan.nextInt();  

            int number = score/10;   

           switch(number){    

                         case 10:    

                         case 9:System.out.println("等级为A");break;    

                         case 8:System.out.println("等级为B");break;    

                         case 7:System.out.println("等级为C");break;    

                         case 6:System.out.println("等级为D");break;    

                         default:     

             System.out.println("等级为E");   }

 } }

2、

import java.util.Scanner;

public class Test11{     

          public static void main(String [] args){              

                      /*  Scanner sc=new Scanner(System.in);           

                          System.out.print("请输入一个百分制分数:");

                          double a=sc.nextDouble();                      

                         if(a >= 90){                

                                        System.out.print("等级为A");}           

                         else if(80 <= a && 90 > a){                

                                       System.out.print("等级为B");}           

                        else if(70 <= a && a < 80){                

                                       System.out.print("等级为C");}           

                        else if(60 <= a && a < 70){                

                                      System.out.print("等级为D");}           

                        else if(a<60&&a>0){                

                                      System.out.print("等级为E");}           

                       else{                

                                      System.out.print("输入错误!");}  */

 

       /*         Scanner input = new java.util.Scanner(System.in);   

                   System.out.print("请输入成绩:");   

                   String s = input.next();             

                   double d = Integer.parseInt(s);             

                   if(d>=90)                   

                                   System.out.println("A");            

                  else if (d>=80)                  

                                      System.out.println("B");             

                   else if (d>=70)                   

                                       System.out.println("C");     

                  else if (d>=60)           

                                    System.out.println("D");     

                  else           

                                    System.out.println("E");   */           

 

         Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个分数:");

        int t = 0;

        try {

            t = sc.nextInt();

        } catch (Exception e) {

            System.out.println("分数错误!");

            return;

        }

        System.out.println(change(t));         }

       public static String change(int t) {

        if (t >= 0 && t < 60) {

            return "E";

        } else if (t >= 60 && t < 70) {

            return "D";

        } else if (t >= 70 && t < 80) {

            return "C";

        } else if (t >= 80 && t < 90) {

            return "B";

        } else if (t >= 90 && t <= 100) {

            return "A";

        } else {

            return "分数错误!";

        }

    }

 }