importjava.util.Scanner;/*** @Author liguo
* @Description 从键盘上读入一个百分制成绩x(0 < = x < = 100),
* 将其转换为等级制成绩输出。本题在C语言和Java语言中要求使用switch分支实现。
* 等级制成绩(百分制成绩)
* A(90<=x<=100)
* B(80<=x<90)
* C(70<=x<80)
* D(60<=x<70)
* E(0<=x<60)
* @Data 2018-04-03*/
public classMain {static void judge(intmark) {char degree = 'A';int temp = mark / 10;if (temp >= 0 && temp < 6)
degree= 'E';if (temp == 6)
degree= 'D';if (temp == 7)
degree= 'C';if (temp == 8)
degree= 'B';if (temp == 9 || temp == 10)
degree= 'A';
System.out.println( mark+ "--" +degree );
}public static voidmain(String[] args) {inttemp;
Scanner in= newScanner( System.in );int x =in.nextInt();
temp= x / 10;if (temp >= 0 && temp < 6)
temp= 5;switch(temp) {case 5:
judge( x );break;case 6:
judge( x );break;case 7:
judge( x );break;case 9:
judge( x );break;case 10:
judge( x );break;
}
}
}
用if语句求解分段函数 得分:10 / 10
importjava.util.Scanner;/*** @Author liguo
* @Description分段函数求解:输入 x ,计算并输出 y 的值:
* y=x+100 ( 当 x < 20)
* y= x ( 当 2 0 ≤ x ≤ 100)
* y=x-100 ( 当 x > 100)
* @Data 2018-04-03*/
public classMain {public static voidmain(String[] args) {doublex, y;
Scanner in= newScanner( System.in );
x=in.nextDouble();if (x < 20)
y= x + 100;else if (x >= 20 && x <= 100)
y=x;elsey= x - 100;
System.out.printf("x=%.2f,y=%.2f", x, y );
}
}2-2 混合类型数据格式化输入 得分:10 / 10
importjava.util.Scanner;/*** @Author liguo
* @Description 输入在一行中顺序给出浮点数1、整数、字符、浮点数2,其间以1个空格分隔。
* 输入描述
* 输入在一行中顺序给出浮点数1、整数、字符、浮点数2,其间以1个空格分隔。
* 输出描述
* 在一行中按照字符、整数、浮点数1、浮点数2的顺序输出,其中浮点数保留小数点后2位。
* @Data 2018-04-03*/
public classMain {public static voidmain(String[] args) {
Scanner in= newScanner( System.in );double d1 =in.nextDouble();int i =in.nextInt();
String s=in.next();char c = s.charAt( 0);double d2 =in.nextDouble();
System.out.printf("%c %d %.2f %.2f", c, i, d1, d2 );
}
}