1、A simple JAVA program:import java.io.*;public class Li2_01 / Main class beginpublic static void(String args)System.out.print(“This is my first Java Application program!“);System.out.print(“Well done!“);/单个字符输入与输出import java.io.*;public class IOTestpublic static void main(String args)char c=a;System.
2、out.print(“Enter a character please:“);tryc=(char)System.in.read();System.out.print(“The character youve entered is “+c);catch(IOException e)/字符串输入与输出import java.io.*;public class IOTestpublic static void main(String args)String c=“;BufferedReader buf=new BufferedReader(new InputStreamReader(System.
3、in);System.out.print(“Enter a string:“);tryc=buf.readLine();System.out.print(“The string youve entered is “ “+c+“ “ “);catch(IOException e)/接收一个整数并输出。import java.io.*;public class Intpublic static void main(String args)String intNo;int i;BufferedReader buf=new BufferedReader(new InputStreamReader(Sy
4、stem.in);trySystem.out.print(“Please input an integer:“);intNo=buf.readLine();i=Integer.parseInt(intNo);System.out.print(“The integer youve entered is:“+i+“.“); catch(IOException e)e.printStackTrace();1、字符串转换成各种数据类型的方long long_num=Long.parseLong(str);int int_num=Integer.parseInt(str);short short_num
5、=Short.parseShort(str);byte byte_num=Byte.parseByte(str);double double_num=Double.parseDouble(str);float float_num=Float.parseFloat(str);2、各数据类型所占字节数int 4字节short 2字节long 8字节byte 1字节float 4字节double 8字节3、算术运算符+ - * / % -(取负值) + -4、关系运算符 = 右移 xy 将 x 向右移 y 位,左边的高位添符号位 不带符号右移 xy 将 x 右移 y 位,左边高位添零 7、条件赋值运
6、算符x?y:z8、广义赋值运算符例:+= -= 9、对象运算符X instenceof Y 判断 X 对象是否是类 Y 所创建。10、括号与方括号运算符() 、 /If Else 选择语句,流程控制import java.io.*;public class IfElsepublic static void main(String args) throws IOExceptionDouble x;/Students markString str;BufferedReader buf = new BufferedReader(new InputStreamReader(System.in); Sy
7、stem.out.print(“Please input the student mark:“);x=Double.parseDouble(buf.readLine();System.out.println();if(x100)System.out.println(“Error!“);System.exit(0);if(x=90)str=“优秀“; else if(x=80)str=“良好“;else if(x=60)str=“及格“;elsestr=“不及格“;System.out.println(“学生的这门课程的得分等级是: “+str);/Switch 的用法import java.i
8、o.*;public class SwitchTestpublic static void main(String args) throws IOExceptionint i=0;double result=0,a=0,b=0;String s=“+“;BufferedReader buf = new BufferedReader(new InputStreamReader(System.in);System.out.println(“The first:“);a=Double.parseDouble(buf.readLine();System.out.println(“The operate
9、r:“);s=buf.readLine();System.out.println(“The second:“);b=Double.parseDouble(buf.readLine();Judge ju=new Judge();i=ju.fuhao(s);switch(i)case 1:result=a+b;break;case 2:result=a-b;break;case 3:result=a*b;break;case 4:result=a/b;break;default:System.out.println(“ERROR“);System.out.println(“The result i
10、s:“+result);class Judgepublic int fuhao(String s)if(s.equals(“+“) return 1;else if(s.equals(“-“) return 2;else if(s.equals(“*“) return 3;else if(s.equals(“/“) return 4;else return 0;/for 循环import java.io.*;public class Forxhstatic int lianJia=0;public static void main(String args) System.out.println
11、(“1+.+100=“+lj(100); static int lj(int m)for(int i=0;ij/2)System.out.print(j+“t“);if(m=10)m=0;System.out.println(“);else m+;/逐个输入并计算10个学生的平均成绩import java.io.*;public class Szpublic static void main(String args) throws IOExceptionint k,count=10;float score = new floatcount;float floatSum = 0.0f,float
12、Aver=0.0f;String str;BufferedReader buf = new BufferedReader(new InputStreamReader(System.in);for(k=0;kintkm+1)temp=intkm;intkm=intkm+1;intkm+1=temp;System.out.println(“产生的随机数排序之后如下: “);for(k=1;k=count;k+)System.out.print(intkk+“t“);System.out.println();/3*3矩阵转置import java.io.*;public class Ewszpubl
13、ic static void main(String args)int j,k,temp,aH=3,aL=4,bH,bL;bH=aL;bL=aH;int a=new intaHaL;int b=new intbHbL;System.out.println(“数组 a 各元素如下:“);for(j=0;jaH;j+)for(k=0;kaL;k+)ajk=(int)(Math.random()*10);bkj=ajk;System.out.print(ajk+“t“);System.out.println();System.out.println(“转置后得到数组 b 各元素如下: “);for(
14、j=0;jbH;j+)for(k=0;kbL;k+)System.out.print(bjk+“t“);System.out.println();/求两矩阵相乘的结果import java.io.*;public class Lipublic static void main(String args) throws IOExceptionint j,k,m,aH=2,aL=3,bL=4,a,b,c;BufferedReader buf=new BufferedReader(newInputStreamReader(System.in);String str;a=new intaHaL;b=ne
15、w intaLbL;c=new intaHbL;System.out.println(“请输入 a 矩阵的元素:“);for(j=0;jaH;j+)for(k=0;kaL;k+)str=buf.readLine();ajk=Integer.parseInt(str); System.out.println(“请输入 b 矩阵元素:“);for(j=0;jaL;j+)for(k=0;kbL;k+)str=buf.readLine();bjk=Integer.parseInt(str);System.out.println(“输入 a 矩阵元素如下: “);for(j=0;jaH;j+)for(k=0;kaL;k+)System.out.print(ajk+“t“);System.out.println();System.out.println(“输入 b 矩阵元素如下:“);