Java练习题

  1. 将学生的成绩按不同的分数段分为优、良、中、差、及格和不及格5个等级,从键盘上输入一个1~100之间的成绩,输出相应的等级。要求用switch语句实现。
    实现代码:

import java.io.IOException;
import java.util.Scanner;

public class ChapterFour {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
int testScore;
String grade;

    Scanner reader = new Scanner(System.in);
    do{
        System.out.print("请输入1~100的分数: ");
        testScore = reader.nextInt();
    }while(testScore >100 || testScore<0);
    switch(testScore/10){
    case 9 :
        grade = "优";
        break;
    case 8 :
        grade = "良";
        break;
    case 7:
        grade = "中";
        break;
    case 6:
    grade = "及格";
    break;
    default:
        grade = "不及格";
    }
    System.out.println("成绩评定: " + grade);
}

}

  1. 设学生的学习成绩按如下的分数段评定为4个等级:85~100为A,70~84为B,60~69为C,0~59为D。从键盘输入一个0~100之间的成绩,要求用switch语句根据成绩,评定并输出相应的等级。

import java.io.IOException;
import java.util.Scanner;
public class ChapterFour {

/**
 * @param args
 */
public static void main(String[] args) throws IOException {
    //P55练习题2
    int testScore;
    char grade;
    Scanner reader = new Scanner(System.in);
    do{
        System.out.print("请输入1~100的分数: ");
        testScore = reader.nextInt();
    }while(testScore >100 || testScore < 0);
    //testScore = testScore+5;
    switch(testScore/10){
    case 10:
    case 9:
        grade ='A';
        break;
    case 7:
        grade = 'B';
        break;
    case 6:
        grade = 'C';
        break;
    default:if(testScore > 84){
        grade ='A';
    }else if(testScore > 70 ){
        grade = 'B';
    }else
        grade = 'D';    
    }
    System.out.println("成绩评定: "+grade); 
}

}

  1. 编写一个Java应用程序,输出1~100之间既可以被3整除,又可以被7整除的数。

public class ChapterFour {

/**
 * @param args
 */
public static void main(String[] args) throws IOException {
    //P55练习题3
    int i =1, n=100;
    System.out.print("1~100中能被3整除,又能被7整除的数: ");
    for(i = 1; i<=n; i++){
        if(i % 3 == 0 &&i % 7 ==0)
            System.out.print(" "+i +" ");
    }       
}

}

  1. 编写一个Java应用程序,在键盘上输入数n, 计算并输出1!+2!+3!+…+n!的结果。

import java.io.IOException;
import java.util.Scanner;

public class ChapterFour {

/**
 * @param args
 */
public static void main(String[] args) throws IOException {

//P55练习题4
int temp = 1, sum = 0, n;
System.out.print(“请输入一个数: “);
Scanner reader = new Scanner(System.in);
n = reader.nextInt();
for(int i=1; i<=n; i++){
for(int j =1; j<= i;j++){
temp *= j;
}
System.out.println(i + “的阶乘” + temp);
sum += temp;
temp = 1;
}
System.out.println(sum);
}
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值