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;jintArray[j]) min=intArray[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(\输入的\个同学平均成绩是:\ } }

本文来源:https://www.bwwdw.com/article/5rew.html

Top