您好,欢迎来到测品娱乐。
搜索
您的当前位置:首页第4章 习题参

第4章 习题参

来源:测品娱乐


第4章 习题解答

1. 声明一个数组,保存一个学生的数学、语文、英语、物理、化学等课程的成绩,编写一个程序,计算5门课程的平均成绩,精确到0.1分,成绩值从键盘录入。

import java.util.Scanner;

public class XT_1_score {

public static void main(String[] args) {

// TODO Auto-generated method stub

double score[] = new double[5];

System.out.println(\"请分别输入数学、语文、英语、物理、化学的成绩(数字之间用空格格开):\");

double sum = 0, average = 0;

Scanner in = new Scanner(System.in);

int i;

for (i = 0; i < 5; i++)

score[i] = in.nextDouble();

for (i = 0; i < 5; i++)

sum += score[i];

average = sum / 5;

System.out.println(\"平均成绩为:\" + String.format(\"%.1f\", average));}

2. 编程实现统计50名学生的百分制成绩中各分数段的学生人数,即:分别统计出100分、90-99分、80-分、70-79分、60-69分、不及格的学生人数。

import java.util.Scanner;

public class XT_2_score_sore {

public static void main(String[] args) {

// TODO Auto-generated method stub

double score[] = new double[50];

int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, i;

System.out.println(\"请依次输入50名学生的成绩(用空格隔开):\");

Scanner br = new Scanner(System.in);

for (i = 0; i < 50; i++)

score[i] = br.nextDouble();

for (i = 0; i < 50; i++) {

if (score[i] == 100)a++;

if (score[i] >= 90 && score[i] <= 99)b++;

if (score[i] >= 80 && score[i] <= )c++;

if (score[i] >= 70 && score[i] <= 79)d++;

if (score[i] >= 60 && score[i] <= 69)e++;

if (score[i] < 60)f++;}

System.out.println(\"成绩为100分的个数:\" + a);

System.out.println(\"成绩为90-99分的个数:\" + b);

System.out.println(\"成绩为80-分的个数:\" + c);

System.out.println(\"成绩为70-79分的个数:\" + d);

System.out.println(\"成绩为60-69分的个数:\" + e);

System.out.println(\"成绩为不及格的个数:\" + f);}}

3. 编写一个程序,实现打印输出字符串数组中最大值和最小值。提示:按照字典顺序决定字符串的最大值和最小值,字典中排在后面的大于前面的。

import java.util.Arrays;

import java.util.Scanner;

class XT_3_string {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(\"请输入字符串数组中的字符:\");

Scanner in = new Scanner(System.in);

String str = in.next();

char array[] = new char[str.length()];

for (int i = 0; i < str.length(); i++) {

array[i] = str.charAt(i);}

Arrays.sort(array);

System.out.println(\"最大值為:\" + array[str.length() - 1]);

System.out.println(\"最小值為:\" + array[0]);}}

4. 使用键盘输入一个字符串,编写程序统计这个字符串中的字母、空格和数字的个数。

import java.util.Scanner;

class XT_4_string {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a = 0, b = 0, c = 0;

System.out.println(\"请输入一段字符串:\");

Scanner in = new Scanner(System.in);

String str = in.nextLine();

for (int i = 0; i < str.length(); i++) {

char array = str.charAt(i);

if ((array >= 'a' && array <= 'z')

|| (array >= 'A' && array <= 'Z')) a++;

if (array >= '0' && array <= '9') b++;

if (array == ' ') c++;}

System.out.println(\"字母的个数:\" + a);

System.out.println(\"数字的个数:\" + b);

System.out.println(\"空格的个数:\" + c);}}

5. 编程实现将数组中的值按逆序重新存放,例如:原来顺序是9、7、4、6,要求改为6、4、7、9。

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Scanner;

public class XT_5_nixu {

public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

System.out.println(\"请输入数组的长度:\");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String m;

m = br.readLine();

int array[] = new int[Integer.parseInt(m)];

System.out.println(\"请输入数组的元素(用空格隔开):\");

Scanner in = new Scanner(System.in);

for (int i = 0; i < Integer.parseInt(m); i++) {

array[i] = in.nextInt();}

System.out.println(\"逆序输出:\");

for (int i = Integer.parseInt(m) - 1; i >= 0; i--)

System.out.print(array[i] + \" \");}}

6. 编写程序,完成打印输出杨辉三角形(要求输出的格式应在屏幕的居中位置)。杨辉三角形的格式如下: import java.util.Scanner;

public class XT_6_YangHSJ {

public static void yanghui(int a[][], int r) {

for (int i = 0; i < r; i++) {

for (int j = 0; j < a[i].length; j++) {

if (i == 0 || j == 0 || j == a[i].length - 1)

a[i][j] = 1;

else

a[i][j] = a[i - 1][j - 1] + a[i - 1][j];}}

System.out.println(r + \"行杨辉三角为:\");

// 输出杨辉三角

for (int i = 0; i < r; i++) {

for (int k = r-i; k >0; k--)

System.out.print(\" \");

for (int j = 0; j < a[i].length; j++) {

System.out.print(a[i][j] + \" \");}

System.out.println();}}

public static void main(String[] args) {

// TODO Auto-generated method stub

{

System.out.println(\"请输入杨辉三角的行数:\");

Scanner input = new Scanner(System.in);

int r = input.nextInt();

input.close();

int a[][] = new int[r][];

for (int i = 0; i < r; i++)

a[i] = new int[i + 1];

yanghui(a, r); }}}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- cepb.cn 版权所有 湘ICP备2022005869号-7

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务