一、使用方法编写求圆面积和周长的程序,运行时提示输入圆半径,然后输出计算结果。运行效果如下图所示:
import java.util.Scanner;public class Test {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("=======计算圆面积周长=======");System.out.println("请输入圆的半径:");double r =input.nextDouble();System.out.printf("半径为%.2f的圆面积:%.2f\n",r,(3.14*r*r));System.out.printf("半径为%.2f的圆周长:%.2f",r,(2*3.14*r));}}
二、使用方法编写求矩形面积和周长的程序,运行时提示输入矩形的长度和宽度,然后输出计算结果。 运行结果如下图所示:
import java.util.Scanner;public class Test {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("=======计算矩形面积周长=======");System.out.println("请输入矩形的长度:");double l =input.nextDouble();System.out.println("请输入矩形的宽度:");double w =input.nextDouble();System.out.printf("长%.2f,宽%.2f的矩形面积:%.2f\n",l,w,l*w);System.out.printf("长%.2f,宽%.2f的矩形周长:%.2f\n",l,w,2*(l+w));}}
三、编程成绩统计程序,要求运行时提示输入逗号分隔的多个成绩分数,然后对这些分数按从大到小(降序)排序,并找出最高和最低分,统计平均分。运行效果如下图所示:
import java.util.Arrays;
import java.util.Scanner;public class Test04 {public static void main(String[] args) {System.out.println("=======成绩统计=======");Scanner input = new Scanner(System.in);System.out.println("请输入逗号分隔的多个成绩分数(最多1位小数):");String l = input.nextLine();String[] s = l.split(",");double sum = 0;float[] scores = new float[s.length];for (int i = 0; i < s.length; i++) {scores[i] = Integer.parseInt(s[i]);sum+=Integer.parseInt(s[i]);}Arrays.sort(scores);System.out.println("按从大到小(降序)排序后的数据:");for(int i = s.length-1;i >= 0;i--) {System.out.print(scores[i]+" "); }System.out.println();float minScore = scores[scores.length - 1];float maxScore = scores[0];double averageScore = sum/s.length;System.out.println("最低分:" + minScore);System.out.println("最高分:" + maxScore);System.out.printf("平均分:%.2f",averageScore);}
}