资源描述
习题2
3.使用“= =”对相同内容的字符串进行比较,看会产生什么样的结果。
答:首先创建一个字符串变量有两种方式:
String str = new String("abc");
String str = "abc";
使用“= =”会因为创建的形式不同而产生不同的结果:
String str1 = "abc";
String str2 = "abc";
System.out.println(str1= =str2); //true
String str1 = new String("abc");
String str2 = "abc";
System.out.println(str1= =str2); //false
String str1 = new String("abc");
String str2 = new String("abc");
System.out.println(str1= =str2); //false
因此自符串如果是对内容进行比较,使用equals方法比较可靠。
String str1 = "abc";
String str2 = "abc";
System.out.println(str1= =str2); //true
String str1 = new String("abc");
String str2 = "abc";
System.out.println(str1.equals(str2)); //true
String str1 = new String("abc");
String str2 = new String("abc");
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=n%1000;
hundred=count/100;
count=count%100;
decimal=count/10;
count=count%10;
unit=count;
System.out.println("1678包含的数字分别是:"+thousand+','+hundred+','+decimal+','+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%10;
hundred=n/100%10;
decimal=n/10%10;
unit=n%10;
System.out.println("1678包含的数字分别是:"+thousand+','+hundred+','+decimal+','+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%10;
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天");break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: System.out.println(n+"月份31天");break;
case 4:
case 6:
case 9:
case 11: System.out.println(n+"月份30天");break;
}
} }
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<n;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<N;i++){
System.out.print("数组的第"+i+"个元素是:");
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<N;j++){
if(max<intArray[j])
max=intArray[j];
if(min>intArray[j])
min=intArray[j];
}
System.out.println("最大值为:"+max);
System.out.println("最小值为:"+min);
//从小到大排序
int temp;
for(int i=0;i<N;i++){
for(int j=i;j<N;j++){
if(intArray[i]>intArray[j]){
temp=intArray[i];
intArray[i]=intArray[j];
intArray[j]=temp;
}
}
}
//将排序后的结果打印
System.out.println("排序后的数组为:");
for(int i=0;i<N;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={"Java","is","an","object","oriented","programming","language"};
//打印句子
for(int i=0;i<str.length;i++){
System.out.print(str[i]+" ");
}
System.out.println();
int length=0;
double avr=0;
for(int i=0;i<str.length;i++){
length=length+str[i].length();
}
avr=(double)length/str.length;
System.out.println("平均数的大小为:"+avr);
}
}
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("第"+irow+"行所有元素"+"与第"+iline+"列所有元素"+"加上主对角线和副对角线的和为:"+sum);
//求最大值
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("4*4数组的最大值"+max);
}
}
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<input.length();i++){
if (Character.isUpperCase(change[i]))
change[i]=Character.toLowerCase(
change[i]); else
change[i]=Character.toUpperCase(
change[i]);
}
System.out.print("转化后的字符串:");
for(int i=0;i<input.length();
i++){
System.out.print(change[i]);}
System.out.println();
}
}
16.用一段代码检测两个double型的数x和y是否相等。代码应能分辨这两个数是否是无穷大或NaN.如果它们相等,代码能正确显示这个数。
public class Exercise16{
public static void main(String[] args){
double x=2.0/0.0;
double y=0;
if(x==Double.POSITIVE_INFINITY||y==Double.POSITIVE_INFINITY){
System.out.println("x或y无穷大!");
}
if(Double.isInfinite(x)||Double.isInfinite(y)){
System.out.println("x或y无穷大!");
}
//不要用//(x==Double.NaN||y==Double.NaN)
//对于NaN的检测,不能使用Double的NaN
//常量,而必须用Double.isNaN方法
if(Double.isNaN(x)||Double.isNaN(y))
{
System.out.println("x或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<args.length;i++){
try{
new Integer(args[i]).intValue();
}
catch(Exception e){
continue;
}
array[i]=new Integer(args[i]).intValue();
}
for(int i=0;i<args.length;i++){
System.out.print(array[i]+"*");
}
}}
19.创建一个有两个方法的程序。标准的main()方法初始化两个变量,一个是String型,另一个是StringBuffer型,它们将作为第二个方法的输入参数,这个方法将把一个字符串连接在两个变量后面。
public class Exercise19{
public static String connect(String
str,StringBuffer strBuf){
String con=str+strBuf+"123";
return con;
}
public static void main(String[] args){
String str="this is a String! ";
StringBuffer strBuf=new StringBuffer("this is a StringBuffer! ");
String con=connect(str,strBuf);
System.out.println("连接后的字符串为:"+con);
}
}
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("请输入第"+i+"个学生的成绩:");
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<i;j++){
System.out.print(score[j]+"//"
); }
double scoreSum=0;
for(int j=0;j<i;j++){
scoreSum=scoreSum+score[j];
}
System.out.println("输入的"+i+"个同学平均成绩是:"+scoreSum/i);
}
}其中专业理论知识内容包括:保安理论知识、消防业务知识、职业道德、法律常识、保安礼仪、救护知识。作技能训练内容包括:岗位操作指引、勤务技能、消防技能、军事技能。
二.培训的及要求培训目的
安全生产目标责任书
为了进一步落实安全生产责任制,做到“责、权、利”相结合,根据我公司2015年度安全生产目标的内容,现与财务部签订如下安全生产目标:
一、目标值:
1、全年人身死亡事故为零,重伤事故为零,轻伤人数为零。
2、现金安全保管,不发生盗窃事故。
3、每月足额提取安全生产费用,保障安全生产投入资金的到位。
4、安全培训合格率为100%。
二、本单位安全工作上必须做到以下内容:
1、对本单位的安全生产负直接领导责任,必须模范遵守公司的各项安全管理制度,不发布与公司安全管理制度相抵触的指令,严格履行本人的安全职责,确保安全责任制在本单位全面落实,并全力支持安全工作。
2、保证公司各项安全管理制度和管理办法在本单位内全面实施,并自觉接受公司安全部门的监督和管理。
3、在确保安全的前提下组织生产,始终把安全工作放在首位,当“安全与交货期、质量”发生矛盾时,坚持安全第一的原则。
4、参加生产碰头会时,首先汇报本单位的安全生产情况和安全问题落实情况;在安排本单位生产任务时,必须安排安全工作内容,并写入记录。
5、在公司及政府的安全检查中杜绝各类违章现象。
6、组织本部门积极参加安全检查,做到有检查、有整改,记录全。
7、以身作则,不违章指挥、不违章操作。对发现的各类违章现象负有查禁的责任,同时要予以查处。
8、虚心接受员工提出的问题,杜绝不接受或盲目指挥;
9、发生事故,应立即报告主管领导,按照“四不放过”的原则召开事故分析会,提出整改措施和对责任者的处理意见,并填写事故登记表,严禁隐瞒不报或降低对责任者的处罚标准。
10、必须按规定对单位员工进行培训和新员工上岗教育;
11、严格执行公司安全生产十六项禁令,保证本单位所有人员不违章作业。
三、 安全奖惩:
1、对于全年实现安全目标的按照公司生产现场管理规定和工作说明书进行考核奖励;对于未实现安全目标的按照公司规定进行处罚。
2、每月接受主管领导指派人员对安全生产责任状的落
展开阅读全文