JAVA编程基础实例.doc

上传人:鲁** 文档编号:558059079 上传时间:2023-02-15 格式:DOC 页数:11 大小:213KB
返回 下载 相关 举报
JAVA编程基础实例.doc_第1页
第1页 / 共11页
JAVA编程基础实例.doc_第2页
第2页 / 共11页
JAVA编程基础实例.doc_第3页
第3页 / 共11页
JAVA编程基础实例.doc_第4页
第4页 / 共11页
JAVA编程基础实例.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《JAVA编程基础实例.doc》由会员分享,可在线阅读,更多相关《JAVA编程基础实例.doc(11页珍藏版)》请在金锄头文库上搜索。

1、A simple JAVA program: import java.io.*;public class Li2_01 / Main class begin public static void(String args) System.out.print(This is my first Java Application program!); System.out.print(Well done!); /单个字符输入与输出import java.io.*;public class IOTest public static void main(String args) char c=a; Sys

2、tem.out.print(Enter a character please:); try c=(char)System.in.read(); System.out.print(The character youve entered is +c); catch(IOException e) /字符串输入与输出import java.io.*;public class IOTest public static void main(String args) String c=; BufferedReader buf=new BufferedReader(new InputStreamReader(

3、System.in); System.out.print(Enter a string:); try c=buf.readLine(); System.out.print(The string youve entered is +c+ ); catch(IOException e)/接收一个整数并输出。import java.io.*;public class Int public static void main(String args) String intNo; int i; BufferedReader buf=new BufferedReader(new InputStreamRea

4、der(System.in); try System.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 sho

5、rt_num=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、关系运算符 = = = !=5、逻辑运算符& x&y 当x为false时,不运行y;& x&y| x|y 当x为true时,不运

6、行y;| x|y! !x x是false时结果为true,当x是true时结果为false; xy x和y均为false或者true时结果为false;6、位运算符- 按位取反& 按位与| 按位或 按位异或 右移 xy 将x向右移y位,左边的高位添符号位 不带符号右移 xy 将x右移y位,左边高位添零 7、条件赋值运算符x?y:z 8、广义赋值运算符例:+= -= 9、对象运算符X instenceof Y 判断X对象是否是类Y所创建。10、括号与方括号运算符()、 /If Else 选择语句,流程控制import java.io.*;public class IfElse public st

7、atic void main(String args) throws IOException Double x;/Students mark String str; BufferedReader buf = new BufferedReader(new InputStreamReader(System.in); System.out.print(Please input the student mark:); x=Double.parseDouble(buf.readLine(); System.out.println(); if(x100) System.out.println(Error!

8、); System.exit(0); if(x=90) str=优秀; else if(x=80) str=良好; else if(x=60) str=及格; else str=不及格; System.out.println(学生的这门课程的得分等级是:+str); /Switch的用法import java.io.*;public class SwitchTest public static void main(String args) throws IOException int i=0; double result=0,a=0,b=0; String s=+; BufferedReade

9、r buf = new BufferedReader(new InputStreamReader(System.in); System.out.println(The first:); a=Double.parseDouble(buf.readLine(); System.out.println(The operater:); s=buf.readLine(); System.out.println(The second:); b=Double.parseDouble(buf.readLine(); Judge ju=new Judge(); i=ju.fuhao(s); switch(i)

10、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 is:+result); class Judge public int fuhao(String s) if(s.equals(+) return 1; else if(s.equals(-) return 2; else if(s.equals(*) return 3

11、; else if(s.equals(/) return 4; else return 0; /for循环import java.io.*;public class Forxh static int lianJia=0; public static void main(String args) System.out.println(1+.+100=+lj(100); static int lj(int m) for(int i=0;i=m;i+) lianJia+=i; return lianJia; /转移语句/求1100的所有奇数之和import java.io.*;public class Js public static void main(String args) int sum=0,j; for(j=0;j=100;j+) if(j%2=0) continue; sum+=j;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号