习题六参考答案
6.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));
}
}
6.2编程实现统计50名学生的百分制成绩中各分数段的学生人数,即:分别统计出100分、
90-99分、80-89分、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] <= 89)
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-89分的个数:" + c);
System.out.println("成绩为70-79分的个数:" + d);
System.out.println("成绩为60-69分的个数:" + e);
System.out.println("成绩为不及格的个数:" + f);
}
}
6.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]);
}
}
6.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);
generatedif ((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);
}
}
6.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.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 = Int();
input.close();
int a[][] = new int[r][];
for (int i = 0; i < r; i++)
a[i] = new int[i + 1];
yanghui(a, r);
}
}
}
6.7从键盘录入10个数据,并计算其中最大值、最小值。
import java.util.Scanner;
public class XT_6_7 {
public static void main(String[] args) {
{
int count=10;
float array[] = new float[10];
System.out.println("请输入数组的元素(用空格隔开):");
Scanner in = new Scanner(System.in);
for (int i = 0; i < count; i++) {
array[i] = in.nextFloat();
}
float max=array[0];
float min=array[0];
for (int i=0;i<count;i++){
if (max<array[i])
max=array[i];
if (min>array[i])
min=array[i];
}
System.out.println("这"+count+"个数中,最大的是"+max+",最小的是"+min);
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。