java语言程序设计课后习题答案
更新时间:2024-01-29 12:24:01 阅读量: 教育文库 文档下载
习题2
3.使用“= =”对相同内容的字符串进行比较,看会产生什么样的结果。
答:首先创建一个字符串变量有两种方式: String str = new String(\String str = \;
使用“= =”会因为创建的形式不同而产生不同的结果: String str1 = \String str2 = \
System.out.println(str1= =str2); //true
String str1 = new String(\String str2 = \
System.out.println(str1= =str2); //false
String str1 = new String(\String str2 = new String(\
System.out.println(str1= =str2); //false
因此自符串如果是对内容进行比较,使用equals方法比较可靠。 String str1 = \String str2 = \
System.out.println(str1= =str2); //true
String str1 = new String(\String str2 = \
System.out.println(str1.equals(str2)); //true
String str1 = new String(\String str2 = new String(\
System.out.println(str1.equals(str2)); //true
5.编写一个程序,把变量n的初始值设置为1678,然后利用除法运算和取余运算把变量的每位数字都提出来并打印,输出结果为:n=1678。n的每位数字是1,6,7,8。若n为任意值呢? 法一:
public class Exercise5{
public static void main(String[] args){
int n=1678;
int unit; int decimal; int hundred; int thousand; int count;
thousand=n/1000; count=n00;
hundred=count/100; count=count0;
decimal=count/10; count=count;
unit=count;
System.out.println(\包含的数字分别是:
\'+unit); } }
//如果n为任意值 import java.io.*;
public class Exercise51{
public static void main(String[] args) throws IOException{
System.out.print(\请输入一个整数:\
InputStreamReader isStream=new InputStreamReader(System.in); BufferedReader bfReader=new BufferedReader(isStream); String input=bfReader.readLine(); int length=input.length()-1; int n=new
Integer(input).intValue();
while(length>=0){ int divisor=(int)
Math.pow(10,length);
length=length-1;
int output=n/divisor;
n=n%divisor;
System.out.print(output+\ } } }
法二:(建议使用)
public class Exercise5{
public static void main(String[] args){
int n=1678; int unit; int decimal; int hundred; int thousand;
thousand=n/1000; hundred=n/100; decimal=n/10; unit=n;
System.out.println(\包含的数字分别是:
\'+unit); } }
//如果n为任意值 import java.io.*;
public class Exercise51{
public static void main(String[] args) throws IOException{
System.out.print(\请输入一个整数:\
InputStreamReader isStream=new InputStreamReader(System.in); BufferedReader bfReader=new BufferedReader(isStream); String input=bfReader.readLine(); int length=input.length()-1; int n=new
Integer(input).intValue();
while(length>=0){ int divisor=(int)
Math.pow(10,length); length=length-1;
int output=n/divisor; System.out.print(output+\ } } }
6.编写Java程序,接受用户输入的1-12之间的整数,若不符合条件则重新输入,利用switch语句输出对应月份的天数。 import java.io.*;
public class Exercise6{
public static void main(String[] args) throws IOException{ int n; do{
System.out.print(\请输入1-12之间的整数:\
InputStreamReader isStream=new InputStreamReader(System.in); BufferedReader bfReader=new BufferedReader(isStream); String input=bfReader.readLine(); n=new Integer(input).intValue(); }while(n>12||n<1);
switch(n){
case 2: System.out.println(n+\月份29天\ case 1: case 3: case 5: case 7: case 8: case 10:
case 12: System.out.println(n+\月份31天\ case 4: case 6: case 9:
case 11: System.out.println(n+\月份30天\ } } }
7.编写Java程序计算小于一个整数的全部素数并输出。
import java.io.*;
public class Exercise7{
public static void main(String[] args) throws IOException{
System.out.print(\请输入一个整数:\
InputStreamReader isStream=new InputStreamReader(System.in); BufferedReader bfReader=new BufferedReader(isStream); String input=bfReader.readLine(); int n=new
Integer(input).intValue();
int i;
System.out.println(2); for(i=2;i for(int j=2;j<=i/2+1;j++){ if (i%j==0) break; if (j==i/2+1) System.out.println(i); } } }} 9.编写Java程序实现:输入一组整数存放在数组中,比较并输出其中的最大值和最小值;在将数组元素从小到大排序并输出。 import java.io.*; public class Exercise9{ public static void main(String[] args) throws IOException{ final int N=5; int[] intArray=new int[N]; //数组的赋值 for(int i=0;i System.out.print(\数组的第\个元素是:\ InputStreamReader isStream=new InputStreamReader(System.in); BufferedReader bfReader=new BufferedReader(isStream); String input=bfReader.readLine(); int n=new Integer(input).intValue(); intArray[i]=n; } //求最大最小值 int max=intArray[0]; int min=intArray[0]; for(int j=0;j System.out.println(\最大值为:\ System.out.println(\最小值为:\ //从小到大排序 int temp; for(int i=0;i if(intArray[i]>intArray[j]){ temp=intArray[i]; intArray[i]=intArray[j]; intArray[j]=temp; } } } //将排序后的结果打印 System.out.println(\排序后的数组为:\ for(int i=0;i System.out.print(intArray[i]+\ } } } 10.编写一个方法来计算正方形的面积和周长。 import java.io.*; public class Exercise10{ System.out.print(“请输入正方形的边长:”) InputStreamReader isStream=new InputStreamReader(System.in); BufferedReader bfReader=new BufferedReader(isStream); String input=bfReader.readLine(); int bianChang=Integer.parseInt(input); System.out.println(\周长为:\bianChang*4); System.out.println(\面积为:\bianChang*bianChang); } 11.编写一个方法来计算1000以内的素数之和并输出。 public class Exercise11{ public static void main(String[] args){ int sum=2; for(int i=2;i<=100;i++){ for(int j=2;j<=i/2+1;j++){ if(i%j==0) break; if(j==i/2+1) System.out.println(i); } } } } 12.使用数组存储一个英文句子“Java is an object oriented programming language.”显示该句子,并算出每个单词的平均字母数。 import java.io.*; public class Exercise12{ public static void main(String[] args){ String[] str={\ted\ //打印句子 for(int i=0;i System.out.println(); int length=0; double avr=0; for(int i=0;i avr=(double)length/str.length; System.out.println(\平均数的大小为:\ } } 14.定义一个4行4列的double型二维数组,并创建一个方法显示数组,计算任意给定的行,给定的列,主对角线和副对角线的和以及数组中的最大值。 import java.io.*; public class Exercise14{ public static void main(String[] args) throws IOException{ double[][] array={ {2,4,5,7}, {6,3,5,7}, {6,7,4,4}, {2,2,3,3}}; //显示4×4的二维数组 for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ System.out.print(array[i][j] +\ System.out.println(); } System.out.print(\请输入选择的行(0-3):\ InputStreamReader input=new InputStreamReader(System.in); BufferedReader reader=new BufferedReader(input); String row=reader.readLine(); int irow=new Integer(row).intValue(); System.out.println(); System.out.print(\请输入选择的列(0-3):\ InputStreamReader input2=new InputStreamReader(System.in); BufferedReader reader2=new BufferedReader(input2); String line=reader2.readLine(); int iline=new Integer(line).intValue(); double sum=0; for(int i=0;i<4;i++){ sum=sum+array[irow][i]; } for(int j=0;j<4;j++){ sum=sum+array[j][iline]; } //主对角线 for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(i==j) sum=sum+array[i][j]; } } //副对角线 for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(i==(3-j)) sum=sum+array[i][j]; } } System.out.println(\第\行所有元素\与第\列所有元素\加上主对角线和副对角线的和为:\ //求最大值 double max=array[0][0]; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(array[i][j]>max) max=array[i][j]; }} System.out.println(\数组的最大值\ } } 15.创建一个程序把输入字符串的大小互换。 import java.io.*; public class Exercise15{ public static void main(String[] args) throws IOException{ System.out.print(\请输入字符串:\ InputStreamReader isStream=new InputStreamReader(System.in); BufferedReader bfReader=new BufferedReader(isStream); String input=bfReader.readLine(); char[] change=input.toCharArray(); for(int i=0;i change[i]=Character.toUpperCase( if(x==Double.POSITIVE_INFINITY||y==Double.POSITIVE_INFINITY){ System.out.println(\或y无穷大!\ } if(Double.isInfinite(x)||Double.isInfinite(y)){ System.out.println(\或y无穷大!\ } //不要用 //(x==Double.NaN||y==Double.NaN) //对于NaN的检测,不能使用Double的NaN //常量,而必须用Double.isNaN方法 if(Double.isNaN(x)||Double.isNaN(y)) { System.out.println(\或y非法!\ } } } 17.编写一个方法,把命令行输入的字符串转化为相应的int值。思考:如果字符串不能代表整数时该怎么办? import java.io.*; public class Exercise17{ public static void main(String[] args) throws NumberFormatException{ int[] array=new int[args.length]; for(int i=0;i Integer(args[i]).intValue(); } catch(Exception e){ continue; } array[i]=new Integer(args[i]).intValue(); } for(int i=0;i String con=str+strBuf+\ return con; } public static void main(String[] args){ String str=\ StringBuffer strBuf=new StringBuffer(\\ String con=connect(str,strBuf); System.out.println(\连接后的字符串为:\ } } 20.创建一个简单的成绩单程序,帮助老师评估学生的表现。该程序用double数组存放成绩来计算平均成绩。 //输入1-20个学生成绩,以“#”号结束 import java.io.*; public class Exercise20{ public static void main(String[] args) throws IOException{ int i=0; double[] score=new double[20]; String str=\ do{ System.out.print(\请输入第\个学生的成绩:\ InputStreamReader input=new InputStreamReader(System.in); BufferedReader reader=new BufferedReader(input); str=reader.readLine(); try{ double scoreTemp=new Double(str).doubleValue(); score[i]=scoreTemp; i++; } catch(Exception e) {continue;} } while(!str.equals(\ for(int j=0;j System.out.print(score[j]+\ ); } double scoreSum=0; for(int j=0;j scoreSum=scoreSum+score[j]; } System.out.println(\输入的\个同学平均成绩是:\ } }
正在阅读:
java语言程序设计课后习题答案01-29
反比例函数与四边形综合题01-29
消防池护栏安装合同12-02
碗扣式钢管脚手架安全技术规范试题04-16
第四部分 控制测量练习(7 椭球面计算)10-14
定点药店年度考核自评表04-24
分批供货合同 11-02
电脑开机提示invalid,system,disk的解决方法02-10
2016急诊试题及答案05-20
2022届高考政治一轮复习计划04-21
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 课后
- 习题
- 程序设计
- 答案
- 语言
- java
- 超星尔雅2018《学术基本要素:专业论文写作》期末考试答案100分
- 雷达避碰
- 2010年下半年软件设计师上午题及答案解析 - 图文
- 2018年中国输配电市场深度调查研究分析报告目录
- 开荒者刘文闻
- 七年级政治第一单元测试练习题及答案
- 日照市重点支柱产业
- 基于单片机的超速报警器课程设计 - 图文
- 2012年国家司法考试大纲 - 刑事诉讼法
- 电除尘器基本参数的计算
- 教师公需科目1
- 实验报告 范文
- 单相锁相环总结
- 应收账款管理会计岗位培训教材(分享)
- 淮北市岱河矿开采的环境问题及对策
- 第二章第5节 细胞中的无机物
- 中国0—6岁儿童营养发展报告(2012)全文
- 危险废物贮存环节记录表
- 自学考试基础会计学复习重点归纳
- C++语言程序设计实验答案 - 类与对象