16、已知学生成绩以100分为满分,共分5个等级:A,B,C,D,E
90~100为等级A,
80~89为等级B,
70~79为等级C,
60~69为等级D,
0~59为等级E。
要求定义一个成绩变量,当成绩变化时,可直接知道该成绩对应的等级。
例如:当成绩为100时,该学生的等级是A。
import java.util.Scanner;
public class Exer16 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入学生的成绩:");
int score = scan.nextInt();
if(90 <= score && score <= 100){
System.out.println("该学生成绩等级为:A");
}else if(80 <= score && score <90){
System.out.println("该学生成绩等级为:B");
}else if(70 <= score && score <80){
System.out.println("该学生成绩等级为:C");
}else if(60 <= score && score <70){
System.out.println("该学生成绩等级为:D");
}else{
System.out.println("该学生成绩等级为:E");
}
}
}
17、打印自然数的个数
1)打印1~100之间 6的倍数的个数
2)求出1~100之间,既是3又是7的倍数的自然数出现的次数?
public class Exer17 {
public static void main(String[] args){
int count1 = 0;//对6倍数个数计数
int count2 = 0;//对3和7的倍数个数计数
for(int i = 1;i <= 100;i++){
if(i % 6 ==0){
count1 +=1;
}
if(i % 3 == 0 && i % 7 == 0){
count2 +=1;
}
}
System.out.println("6的倍数的个数为:"+ count1);
System.out.println("3和7的倍数的个数为:"+ count2);
}
}
18、求调和级数中从第多少项开始值大于10
调和级数的第n项形式为:1+1/2+1/3+…+1/n
public class Exer18 {
public static void main(String[] args){
double sum = 0.0;//前n项和
int i = 1;//分母值
int count = 0;
//方法一
do{
sum += 1.0 / i;
count += 1;
i++;
}while(sum < 10);
System.out.println("第"+ count +"项开始值大于10");
//方法二
// while(true){
// sum += 1.0 / i;
// if(sum > 10){
// break;
// }
// i++;
// }
//
// System.out.println("第"+ i +"项开始值大于10");
}
}
19、打印如下的图形:三角形
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
public class Exer19 {
public static void main(String[] args){
//方法一:分为两部分就行输出
//上半部(升):注意内外层的关系
for(int i = 1;i <= 4;i++){
for(int j = 1;j < 2 * i;j++){
System.out.print("*");
}
System.out.println();
}
//下半部(降)
for(int i = 1;i <= 3;i++){
for(int j = 1;j <= 7 - 2 * i;j++){
System.out.print("*");
}
System.out.println();
}
System.out.println();
//方法二:用一个for循环输出
for(int i = 0;i < 7;i++){
if(i < 4){
for(int j = 0;j < 2 * i + 1;j++){
System.out.print("*");
}
System.out.println();
}else{
for(int k = 0;k < 13 - 2 * i;k++){
System.out.print("*");
}
System.out.println();
}
}
}
}
20、 打印如下的图形:菱形1
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
public class Exer20 {
public static void main(String[] args){
//上部分
for(int i = 1;i < 6;i++){
for(int j = 1;j < 6 - i;j++){
System.out.print(" ");
}
for(int k = 1;k < i + 1;k++){
System.out.print("* ");
}
System.out.println();
}
//下部分
for(int i = 1;i < 5;i++){
for(int j = 1;j < i + 1;j++){
System.out.print(" ");
}
for(int k = 1;k <= 5 - i;k++){
System.out.print("* ");
}
System.out.println();
}
}
}