由于 Exception.ToString() 失败,因此无法打印异常字符串。

console程序执行错误时,不显示异常信息. 

解决方法: 在命令行修改显示字符格式  chcp 936

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
java 编写计算器的简单程序//一个较为简洁的巧妙的计算器程序, import java.io.*; import java.awt.event.*; import java.awt.*; //需要解决的问题,数学的运算都有正负号的出现,在点击等号的时候就会有冲突,应该怎样解决,经验:双精度浮点型数据类型是会像后减一位。0.7会显示成0.69999999 public class app74 { static int i=0; static int j=0; static int k=-1; static int l=-1; //static int Data1[]={1}; static String str9=""; static Frame frm1=new Frame("计算器"); static Button btn1=new Button("1"); static Button btn2=new Button("2"); static Button btn3=new Button("3"); static Button btn5=new Button("4"); static Button btn6=new Button("5"); static Button btn7=new Button("6"); static Button btn9=new Button("7"); static Button btn14=new Button("8"); static Button btn16=new Button("9"); static Button btn11=new Button("0");//数字0 static Button btn4=new Button("."); static Button btn8=new Button("+"); static Button btn10=new Button("-"); static Button btn15=new Button("*"); static Button btn13=new Button("/"); static Button btn20=new Button("="); static Button btn18=new Button("清零"); static Button btn19=new Button("后退"); static Button btn12=new Button("π"); static Button btn17=new Button("颜色"); static TextField TF1=new TextField(); static Label Lb1=new Label(".0",Label.RIGHT); //标签 static Label Lb2=new Label(".0",Label.RIGHT); //BS字符长度 static Label Lb3=new Label(".0",Label.RIGHT); //对象str22字符长度 static Label Lb4=new Label(".0",Label.RIGHT); //BS剩余字符长度字符长度 static Label Lb5=new Label("无",Label.LEFT); //执行错误抛出 static Label Lb6=new Label("对象BS字符长度 : ",Label.LEFT); static Label Lb7=new Label("对象str22字符长度 :",Label.LEFT); static Label Lb8=new Label("对象BS剩余字符长度:",Label.LEFT); static Label Lb9=new Label("错误:",Label.LEFT); static Label Lb10=new Label(".0",Label.RIGHT); //计算器下面的标签 static Label Lb11=new Label("余数:",Label.RIGHT); //余数标签 static Label Lb12=new Label(".0",Label.RIGHT); static TextArea TA=new TextArea("说明:由于软件不够完善,请规范运算的输入:1.可在里面按键输入",1000,TextArea.SCROLLBARS_VERTICAL_ONLY); static TextField TF=new TextField(); static GridLayout GL1=new GridLayout(5,4); //流动布局 static Panel Pl=new Panel(); //面板 static BorderLayout BL=new BorderLayout(); static shijian app1=new shijian(); //实现监听器的类对象 static char Data2[]=new char[5]; static Font fnt=new Font("Serief",Font.ITALIC+Font.BOLD,18); static Font fnt2=new Font("Serief",Font.BOLD,15); static String str22; static StringBuffer BS=new StringBuffer(""); //可创建空的字符串对象,这个很重要 static String str23=str22; public static void main(String args[]) throws IOException { frm1.setLayout(null); Pl.setLayout(GL1);//面板加入布局 frm1.setSize(253,450);//没有设置窗口大小 frm1.setLocation(250,450); Lb1.setBounds(0,55,250,25);//计算器数字输入显示框之一上边框 Pl.setBounds(0,80,250,290); Lb2.setBounds(370,60,50,25); Lb3.setBounds(370,90,50,25); Lb4.setBounds(370,120,50,25); Lb5.setBounds(300,150,320,25); Lb6.setBounds(250,60,120,25); Lb7.setBounds(250,90,120,25); Lb8.setBounds(250,120,120,25); Lb9.setBounds(250,150,30,25); Lb10.setBounds(0,365,250,30);//计算器数字输入显示框之一下边框 Lb11.setBounds(250,30,120,25); Lb12.setBounds(370,30,50,25); TF.setBounds(0,30,250,25); TA.setBounds(0,395,250,75); TA.setEditable(false); Pl.add(btn1);//面板 Pl.add(btn2); Pl.add(btn3); Pl.add(btn4); Pl.add(btn5); Pl.add(btn6); Pl.add(btn7); Pl.add(btn8); Pl.add(btn9);//加入按钮 Pl.add(btn14); Pl.add(btn16); Pl.add(btn17); Pl.add(btn10);//加 Pl.add(btn11);//减 Pl.add(btn12);//乘 Pl.add(btn13);//除 Pl.add(btn15);//这里是等号注意事件的监听器在不同的类上 Pl.add(btn17); Pl.add(btn18); Pl.add(btn19); Pl.add(btn20); frm1.add(Pl);//加入面板 frm1.addWindowListener(new shijian2()); frm1.add(Lb1); frm1.add(Lb2); frm1.add(Lb3); frm1.add(Lb4); frm1.add(Lb5); frm1.add(Lb6); frm1.add(Lb7); frm1.add(Lb8); frm1.add(Lb9); frm1.add(Lb10); frm1.add(Lb11); frm1.add(Lb12); frm1.add(TA); frm1.add(TF); //Lb2.setBackground(Color.pink); //Lb3.setBackground(Color.pink); //Lb4.setBackground(Color.pink); //Lb5.setBackground(Color.white); Lb6.setBackground(Color.white); Lb7.setBackground(Color.white); Lb8.setBackground(Color.white); Lb9.setBackground(Color.white); Lb10.setBackground(Color.gray); Lb11.setBackground(Color.white); TF.setBackground(Color.pink); //TA.setBackground(Color.pink); //Lb12.setBackground(Color.gray); Lb1.setFont(fnt); Lb2.setFont(fnt); Lb3.setFont(fnt); Lb4.setFont(fnt); Lb10.setFont(fnt); Lb11.setFont(fnt2); Lb12.setFont(fnt); Lb1.setBackground(Color.gray); btn1.addActionListener(app1); btn2.addActionListener(app1); btn3.addActionListener(app1); btn4.addActionListener(app1); btn5.addActionListener(app1); btn6.addActionListener(app1); btn7.addActionListener(app1); btn8.addActionListener(app1); btn9.addActionListener(app1); btn10.addActionListener(app1); btn11.addActionListener(app1); btn12.addActionListener(app1); //btn12.addActionListener(app1);//写两个这个则会被触发两次事件 btn13.addActionListener(app1); btn14.addActionListener(app1); btn15.addActionListener(app1); btn16.addActionListener(app1); btn17.addActionListener(app1); btn18.addActionListener(app1); btn19.addActionListener(app1); btn20.addActionListener(app1); TF.addTextListener(new shijian3()); frm1.setBackground(Color.white); frm1.setResizable(false); frm1.setVisible(true);//可见 } static class shijian implements ActionListener//接口的实现 { public void actionPerformed(ActionEvent e) { Button BT=(Button)e.getSource(); //取得事件的对象,用于判断事件所属 String str1=BT.getLabel(); //取得按钮的名字,也就是相应的数字 if(str1=="0"||str1=="1"||str1=="2"||str1=="3"||str1=="4"||str1=="5"||str1=="6"||str1=="7"||str1=="8"||str1=="9"||str1=="+"||str1=="-"||str1=="*"||str1=="/"||str1=="."||str1=="π") { if(str1=="π"){str1=Double.toString(Math.PI);} BS.append(str1);//将得到的字符串加入到原有字符串 String str22=BS.toString();//转换成字符串 String str56=Integer.toString(str22.length());//将整形转换成字符串 Lb10.setText(str22); Lb2.setText(str56); Lb3.setText(str56); Lb4.setText(str56); Lb5.setText("无"); Lb12.setText(".0"); Lb1.setText(str22); try{ FileWriter FQ=new FileWriter("D:\\java\\10.txt");FQ.write(str22);FQ.flush();} catch(Exception u) {System.out.println("出错了");}} if(str1=="=")//如果等于等号if里面包含多个if或者while { // System.out.println("str22="+str22); char Data[]=new char[BS.length()]; try{ FileReader FV=new FileReader("D:\\java\\10.txt");//写入所需数据 FV.read(Data);} //这是我们写入的数据, catch(Exception y){System.out.println("出错了");} while(k<Data.length) {k++; if(Data[k+1]=='/'||Data[k+1]=='*'||Data[k+1]=='+'||Data[k+1]=='-')//这个地方基本是整个程序的关键,包括识别使用者使用的计算符号, //还有就是为解决符号多个共存,又没有冲突,不能访问数组的第一位,如果第一位就有符号,那就会跳出,出现错误,只要不访问数组的第一位就好啦, //到了非一号的运算符时,会直接跳出,执行相应的动作 break;}; char ch2=Data[k+1]; if(ch2=='*') {Lb1.setText(""); //清空标签内的内容 String str90=BS.toString(); //转换并取得字符串的内容.字符串内容已经确定,只是调用 Lb10.setText(str90); // Lb10标签的显示 if(str90.indexOf(".")!=-1) //不等于1那么就是有点号,使用浮点数,利用的是系统抛出异常时赋的默认值 { int num1=str90.indexOf('*'); //取得*号的位置 String str2=str90.substring(0,num1); //取得数字字符窜 String str3=str90.substring(num1+1,BS.length()); //取得数字字符窜 // System.out.println("str2="+str2); // System.out.println("str3="+str3); float num2=Float.parseFloat(str2); //数据类型转换,使用长整形 float num3=Float.parseFloat(str3); //数据类型转换 float sum=(float)num2*num3; String str6=Float.toString(sum); //转换回字符串 Lb1.setText(str6); k=-1; BS=new StringBuffer(""); BS.append(str6);} if(str90.indexOf(".")==-1) //等于1那么就是没有有点号,使用非浮点数 { Lb1.setText(""); //清空标签内的内容 String str91=BS.toString(); //转换并取得字符串的内容.字符串内容已经确定,只是调用 Lb10.setText(str90); // Lb10标签的显示 int num1=str91.indexOf('*'); //取得*号的位置 String str2=str91.substring(0,num1); //取得数字字符窜 String str3=str91.substring(num1+1,BS.length()); //取得数字字符窜 // System.out.println("str2="+str2); //System.out.println("str3="+str3); long num2=Long.parseLong(str2); //数据类型转换,使用长整形 long num3=Long.parseLong(str3); //数据类型转换 long sum=num2*num3; String str6=Long.toString(sum); //转换回字符串 Lb1.setText(str6); k=-1; BS=new StringBuffer(""); BS.append(str6); } } if(ch2=='/') { Lb1.setText(""); //清空标签内的内容 String str91=BS.toString(); //转换并取得字符串的内容 Lb10.setText(str91); // Lb10标签的显示 //System.out.println("BS长度="+BS.length()); String str1000=BS.toString(); // System.out.println("str1000="+str1000); if(str91.indexOf(".")==-1){ int num1000=str1000.indexOf('/'); //取得*号的位置 // System.out.println("num1000="+num1000); String str2000=str1000.substring(0,num1000); String str3000=str1000.substring(num1000+1,BS.length()); int num2000=Integer.parseInt(str2000); int num3000=Integer.parseInt(str3000); int sum20=num2000/num3000; //整除数 int sum21=num2000%num3000; //余数 String str56=Integer.toString(sum20); String str57=Integer.toString(sum21); //System.out.println("sum="+sum20); Lb1.setText(str56); Lb12.setText(str57); BS=new StringBuffer("");k=-1; BS.append(Float.toString((float)num2000/(float)num3000));}//因为整形运算会有余数的求法,所以得转换成单精度型的数据类型进行计算,以免丢失数据 if(str91.indexOf(".")!=-1){ int num1000=str1000.indexOf('/'); //取得*号的位置 // System.out.println("num1000="+num1000); String str2000=str1000.substring(0,num1000); String str3000=str1000.substring(num1000+1,BS.length()); float num2000=Float.parseFloat(str2000); float num3000=Float.parseFloat(str3000); float sum20=(float)num2000/num3000; String str56=Float.toString(sum20); // System.out.println("sum="+sum20); Lb1.setText(str56); BS=new StringBuffer("");k=-1; BS.append(str56);} } if(ch2=='+'){ Lb1.setText(""); //清空标签内的内容 String str97=BS.toString(); //转换并取得字符串的内容 Lb10.setText(str97); // Lb10标签的显示 // System.out.println("BS长度="+BS.length()); String str1001=BS.toString(); // System.out.println("str1001="+str1001); if(str97.indexOf(".")==-1){ int num1001=str1001.indexOf('+');//取得*号的位置 // System.out.println("num1001="+num1001); if(num1001>0){ String str2001=str1001.substring(0,num1001); String str3002=str1001.substring(num1001+1,BS.length()); int num2001=Integer.parseInt(str2001); //如果是由小数点的结果则要将数据类型转换成float int num3001=Integer.parseInt(str3002); int sum21=num2001+num3001; String str51=Integer.toString(sum21); // System.out.println("sum="+sum21); Lb1.setText(str51); BS=new StringBuffer("");k=-1; BS.append(str51);} if(num1001==0) { BS.deleteCharAt(0);//删除第一个减号 (BS.toString()).indexOf("+");//取得第二个减号的位置 //StringBuffer str69=new StringBuffer(BS.toString()); //这里必须引入其他的字符串变量来储存,关键在于获得不是第一个减号的位置 //str69.insert(0,'+'); String str2001=(BS.toString()).substring(0,((BS.toString()).indexOf("+"))); String str3002=(BS.toString()).substring(((BS.toString()).indexOf("+")+1),(BS.length())); int num2001=Integer.parseInt(str2001); //如果是由小数点的结果则要将数据类型转换成float int num3001=Integer.parseInt(str3002); int sum21=num2001+num3001; String str51=Integer.toString(sum21); // System.out.println("sum="+sum21); Lb1.setText(str51); BS=new StringBuffer("");k=-1; BS.append(str51); } } if(str97.indexOf(".")!=-1) { //利用抛出异常的数字是-1的规律,设定if语句 int num1001=str1001.indexOf('+');//取得*号的位置 // System.out.println("num1001="+num1001); if(num1001>0){ String str2001=str1001.substring(0,num1001); String str3002=str1001.substring(num1001+1,BS.length()); float num2001=Float.parseFloat(str2001); //如果是由小数点的结果则要将数据类型转换成float float num3001=Float.parseFloat(str3002); float sum21=(float)num2001+num3001; String str51=Float.toString(sum21); //System.out.println("sum="+sum21); Lb1.setText(str51); BS=new StringBuffer("");k=-1; BS.append(str51);} if(num1001==0) { BS.deleteCharAt(0);//删除第一个减号 (BS.toString()).indexOf("+");//取得第二个减号的位置 String str2001=(BS.toString()).substring(0,((BS.toString()).indexOf("+"))); String str3002=(BS.toString()).substring(((BS.toString()).indexOf("+")+1),(BS.length())); float num2001=Float.parseFloat(str2001); //如果是由小数点的结果则要将数据类型转换成float float num3001=Float.parseFloat(str3002); float sum21=(float)num2001+num3001; String str51=Float.toString(sum21); //System.out.println("sum="+sum21); Lb1.setText(str51); BS=new StringBuffer("");k=-1; BS.append(str51); } } } //k值已经变化,为使上面的语句的循环有用,的赋回原值 if(ch2=='-') { Lb1.setText(""); //清空标签内的内容 String str102=BS.toString(); //转换并取得字符串的内容 Lb10.setText(str102); // Lb10标签的显示 // System.out.println("BS长度="+BS.length()); String str1002=BS.toString(); // System.out.println("str1002="+str1002); if(str1002.indexOf(".")==-1) //利用抛出异常的数字是-1的规律,设定if语句 {int num1002=str1002.indexOf('-');//取得*号的位置 // System.out.println("num1002="+num1002); if(num1002>0){ String str2002=str1002.substring(0,num1002); String str3003=str1002.substring(num1002+1,BS.length()); int num2002=Integer.parseInt(str2002); int num3002=Integer.parseInt(str3003); int sum22=num2002-num3002; String str53=Integer.toString(sum22); // System.out.println("sum="+sum22); Lb1.setText(str53); BS=new StringBuffer("");k=-1; BS.append(str53);} if(num1002==0){//由于会有两个减号存在的情况,应该要分情况,比如负数加负数的时候 BS.deleteCharAt(0);//删除第一个减号 (BS.toString()).indexOf("-");//取得第二个减号的位置 StringBuffer str67=new StringBuffer(BS.toString()); //这里必须引入其他的字符串变量来储存,关键在于获得不是第一个减号的位置 str67.insert(0,'-'); String str2002=(str67.toString()).substring(0,((BS.toString()).indexOf("-")+1)); String str3003=(str67.toString()).substring(((BS.toString()).indexOf("-")+2),(BS.length()+1)); int num2002=Integer.parseInt(str2002); int num3002=Integer.parseInt(str3003); int sum22=num2002-num3002; String str53=Integer.toString(sum22); // System.out.println("sum="+sum22); Lb1.setText(str53); BS=new StringBuffer("");k=-1; BS.append(str53); }} if(str1002.indexOf(".")!=-1) { int num1002=str1002.indexOf('-');//取得*号的位置 StringBuffer str66=new StringBuffer(str1002); // System.out.println("num1002="+num1002); if(num1002>0){ String str2002=str1002.substring(0,num1002); String str3003=str1002.substring(num1002+1,BS.length()); float num2002=Float.parseFloat(str2002); float num3002=Float.parseFloat(str3003); float sum22=(float)(num2002-num3002); String str53=Float.toString(sum22); // System.out.println("sum="+sum22); Lb1.setText(str53); BS=new StringBuffer("");k=-1; BS.append(str53);} if(num1002==0) { BS.deleteCharAt(0);//删除第一个减号 (BS.toString()).indexOf("-");//取得第二个减号的位置 StringBuffer str68=new StringBuffer(BS.toString()); //这里必须引入其他的字符串变量来储存,关键在于获得不是第一个减号的位置 str68.insert(0,'-'); String str2002=(str68.toString()).substring(0,((BS.toString()).indexOf("-")+1)); String str3003=(str68.toString()).substring(((BS.toString()).indexOf("-")+2),(BS.length()+1)); float num2002=Float.parseFloat(str2002); float num3002=Float.parseFloat(str3003); float sum22=(float)(num2002-num3002); String str53=Float.toString(sum22); // System.out.println("sum="+sum22); Lb1.setText(str53); BS=new StringBuffer("");k=-1; BS.append(str53); } } }} if(str1=="清零")//删除键 { TF.setText(""); Lb12.setText(".0"); Lb2.setText(".0"); Lb3.setText(".0"); Lb4.setText(".0"); Lb1.setText(".0"); Lb5.setText("无"); Lb10.setText(".0"); BS=new StringBuffer(""); //System.out.println("str22="+str22);//测试 //归无,即使是把对想的内容给删除了,也就是数组的大小事零,但是调用的那个文本并没有归零, //System.out.println("str22="+str22); Lb2.setText(".0"); Lb3.setText(".0");//在清除BS的同时Lb3也会跟着归零 try{FileWriter FP=new FileWriter("D:\\java\\10.txt"); FP.write(str9);//不能写整形数组,只能写字符类型的数组 FP.flush(); FP.close(); k=-1; } //在这里给k赋值,若果计算执行,那么k没有办法在=号哪里进行设置,这里可以设置一下原值的赋值,错误那么可以通过归原值进行调节} //所以每个语句执行完之后都得注意把值给赋回去 catch(Exception o) {Lb5.setText("已经没有数据可删除!");} } try{ if(str1=="后退") //在这里拦住的思路,没按一下“后退”按钮就删最后一个,从BS.length(); //得到的长度是不能直接用来做字符串索引值的,因为达到的长度是整数,要应用于删除字符上,应该减一,不然会出现超出异常 { i++; Lb12.setText(".0");//余数清零上面的标签 if(i<BS.length()+1) //注意这里的设值,应该是 { BS.deleteCharAt(BS.length()-1); //由于BS是static,哪里改变它它就按哪里的标准进行处理,像赋值 String str67=Integer.toString(BS.length());//剩余字符数 Lb2.setText(str67); //应该减一 // System.out.println("剩下字符串数目:"+BS.length()); Lb4.setText(str67); TF.setText(BS.toString()); Lb1.setText(BS.toString()); Lb10.setText(BS.toString());//显示剩下的字符串下方标签 Lb3.setText(str67); } if(BS.length()==0) { Lb1.setText(".0");} i=-1; //要加上这个,因为没按一次后退都会加上一,总有一次会超过字符长度,执行完相应的 语句之后将它赋回原值 }} catch(Exception p) { Lb5.setText("已经没有数据可删除!"); } if(str1=="颜色") { l++; if(l==0) {frm1.setBackground(Color.yellow); Lb2.setBackground(Color.yellow); Lb3.setBackground(Color.yellow); Lb4.setBackground(Color.yellow); Lb5.setBackground(Color.yellow); Lb6.setBackground(Color.yellow); Lb7.setBackground(Color.yellow); Lb8.setBackground(Color.yellow); Lb9.setBackground(Color.yellow); Lb11.setBackground(Color.yellow); Lb12.setBackground(Color.yellow); } if(l==1) {frm1.setBackground(Color.pink); Lb2.setBackground(Color.pink); Lb3.setBackground(Color.pink); Lb4.setBackground(Color.pink); Lb5.setBackground(Color.pink); Lb6.setBackground(Color.pink); Lb7.setBackground(Color.pink); Lb8.setBackground(Color.pink); Lb9.setBackground(Color.pink); Lb11.setBackground(Color.pink); Lb12.setBackground(Color.pink); } if(l==2) {frm1.setBackground(Color.red);Lb2.setBackground(Color.red); Lb3.setBackground(Color.red); Lb4.setBackground(Color.red); Lb5.setBackground(Color.red); Lb6.setBackground(Color.red); Lb7.setBackground(Color.red); Lb8.setBackground(Color.red); Lb9.setBackground(Color.red); Lb11.setBackground(Color.red); Lb12.setBackground(Color.red); } if(l==3) {frm1.setBackground(Color.gray);Lb2.setBackground(Color.gray); Lb3.setBackground(Color.gray); Lb4.setBackground(Color.gray); Lb5.setBackground(Color.gray); Lb6.setBackground(Color.gray); Lb7.setBackground(Color.gray); Lb8.setBackground(Color.gray); Lb9.setBackground(Color.gray); Lb11.setBackground(Color.gray); Lb12.setBackground(Color.gray); } if(l==4) {frm1.setBackground(Color.orange);Lb2.setBackground(Color.pink); Lb3.setBackground(Color.orange); Lb4.setBackground(Color.orange); Lb5.setBackground(Color.orange); Lb6.setBackground(Color.orange); Lb7.setBackground(Color.orange); Lb8.setBackground(Color.orange); Lb9.setBackground(Color.orange); Lb11.setBackground(Color.orange); Lb12.setBackground(Color.orange); } if(l==5) {frm1.setBackground(Color.white);Lb2.setBackground(Color.white); Lb3.setBackground(Color.white); Lb4.setBackground(Color.white); Lb5.setBackground(Color.white); Lb6.setBackground(Color.white); Lb7.setBackground(Color.white); Lb8.setBackground(Color.white); Lb9.setBackground(Color.white); Lb11.setBackground(Color.white); Lb12.setBackground(Color.white); } if(l==6) {frm1.setBackground(Color.blue);l=-1;Lb2.setBackground(Color.blue); Lb3.setBackground(Color.blue); Lb4.setBackground(Color.blue); Lb5.setBackground(Color.blue); Lb6.setBackground(Color.blue); Lb7.setBackground(Color.blue); Lb8.setBackground(Color.blue); Lb9.setBackground(Color.blue); Lb11.setBackground(Color.blue); Lb12.setBackground(Color.blue); } } }//内部类的函数 }//内部类 public static class shijian2 extends WindowAdapter { public void windowClosing(WindowEvent o) { System.exit(0); } } static class shijian3 implements TextListener { public void textValueChanged(TextEvent q) { Lb12.setText(".0"); try{BS=new StringBuffer(TF.getText());//写入BS,改变BS的值 FileWriter FY=new FileWriter("D:\\java\\10.txt");FY.write(TF.getText());FY.flush();}//同时写入同一个文本内相当于拥有两份运算的数据,当按面板是也会将文本覆盖,不清空 catch(Exception u) {System.out.println("出错了");} Lb2.setText(Integer.toString(BS.length())); Lb3.setText(Integer.toString(BS.length())); Lb4.setText(Integer.toString(BS.length())); Lb5.setText(Integer.toString(BS.length())); Lb1.setText(BS.toString()); //让数字输入的同时取得BS字符串BS也是整个程序的关键,整个程序围绕BS进行,包括加入修改,新添 Lb10.setText(BS.toString()); } } }//主类 Ps:QQ:550119320
《JAVA精华》,共47页,20多万字,推荐下载! 1 JAVA SE 1.1 深入JAVA API 1.1.1 Lang包 1.1.1.1 String类和StringBuffer类 位于java.lang包中,这个包中的类使用时不用导入 String类一旦初始化就不可以改变,而stringbuffer则可以。它用于封装内容可变的字符串。它可以使用tostring()转换成string字符串。 String x=”a” 4 ”c”编译时等效于String x=new StringBuffer().append(“a”).append(4).append(“c”).toString(); 字符串常量是一种特殊的匿名对象,String s1=”hello”;String s2=”hello”;则s1==s2;因为他们指向同一个匿名对象。 如果String s1=new String(“hello”);String s2=new String(“hello”);则s1!=s2; /*逐行读取键盘输入,直到输入为“bye”时,结束程序 注:对于回车换行,在windows下面,有'\r'和'\n'两个,而unix下面只有'\n',但是写程序的时候都要把他区分开*/ public class readline { public static void main(String args[]) { String strInfo=null; int pos=0; byte[] buf=new byte[1024];//定义一个数组,存放换行前的各个字符 int ch=0; //存放读入的字符 system.out.println(“Please input a string:”); while(true) { try { ch=System.in.read(); //该方法每次读入一个字节的内容到ch变量中。 } catch(Exception e) { } switch(ch) { case '\r': //回车时,不进行处理 break; case '\n': //换行时,将数组总的内容放进字符串中 strInfo=new String(buf,0,pos); //该方法将数组中从第0个开始,到第pos个结束存入字符串。 if(strInfo.equals("bye")) //如果该字符串内容为bye,则退出程序。 { return; } else //如果不为bye,则输出,并且竟pos置为0,准备下次存入。 { System.out.println(strInfo); pos=0; break; } default: buf[pos ]=(byte)ch; //如果不是回车,换行,则将读取的数据存入数组中。 } } } } String类的常用成员方法 1、 构造方法: String(byte[] byte,int offset,int length);这个在上面已经用到。 2、 equalsIgnoreCase:忽略大小写的比较,上例中如果您输入的是BYE,则不会退出,因为大小写不同,但是如果使用这个方法,则会退出。 3、 indexOf(int ch);返回字符ch在字符串中首次出现的位置 4、 substring(int benginIndex); 5、 substring(int beginIndex,int endIndex); 返回字符串的子字符串,4返回从benginindex位置开始到结束的子字符串,5返回beginindex和endindex-1之间的子字符串。 基本数据类型包装类的作用是:将基本的数据类型包装成对象。因为有些方法不可以直接处理基本数据类型,只能处理对象,例如vector的add方法,参数就只能是对象。这时就需要使用他们的包装类将他们包装成对象。 ......

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值