1、java applet程序appletcode=hi.classwidth=250height=250name=MyApplet/applet/html实验4/InitVar.javapublic class InitVar byte a=10; short b=20; int c=30; long d=40L; float e=50F; double f=60D; char g=A; boolean h=true; double pi=3.14; System.out.println(字符型,a=+a);短整型,b=+b);整数型,c=+c);长整型,d=+d);单精度型,e=+e);双精度
2、型,f=+f);字符型,g=+g);布尔型,h=+h);圆周率,pi=+pi);实验5/Arethmetic1.javapublic class Arithmetic1public static void main(String args) int a=10; int b=6; int s=0; s=a%b;a%b=+s); +a; -b; s=+a+(-b);a=b=s=实验6浮点型: double a1=0,b1=0,c1=0; a1=5/6;5/6=+a1); b1=5.0/6.0;5.0/6.0=+b1); c1=5/6.0;5/6.0=+c1);整数型: int a2=0; a2=5
3、/6;+a2); double b2=5.0/6.0;5.0/6.0+b2); double c2=5/6.0;5/6.0+c2);实验7/Logic.javapublic class Logic boolean a,x,y,z; if(56)a=true; else a=false; x=!a; y=a&x; z=x|y;x=+x);y=+y);z=+z);实验8/Conver.javapublic class Conver byte a=0; short b=0; int c=0; long d=0; float e=0F; double f=0D; String s=10 a=Byte.
4、parseByte(s); b=Short.parseShort(s); c=Integer.parseInt(s); d=Long.parseLong(s); e=Float.parseFloat(s); f=Double.parseDouble(s); int MyInt=1234; String MyString=+MyInt;如果有字符串含有非数字格式字符,转换时会报告“数字格式异常”,例如: int a=0;abcd a=Integer.parseInt(s);编译成功通过,运行时显示如下图所示错误:实验9/InputNum.javaimport java.util.*;public
5、 class InputNum float b=0F; double c=0D; Scanner Reader=new Scanner(System.in);从键盘输入一串字符: s=Reader.nextLine();你输入的字符串是从键盘输入一个整数: a=Reader.nextInt();你输入的整数是从键盘输入一个单精度浮点数: b=Reader.nextFloat();你输入的单精度浮点数是:从键盘输入一个双精度浮点数: c=Reader.nextDouble();你输入的双精度浮点数是:实验13用do-while语句求100+99+98+.+2+1的值。/DoWhileSenten
6、ce.javapublic class DoWhileSentence int i=100,s=0; do s=s+i; i-; while(i0); System.out.println(100+99+98+.+2+1=实验14编一个java程序,让用户通过键盘输入一个06的整数,根据输入显示今天是星期几。/SwitchSentence.javapublic class SwitchSentence int a=-1;请输入一个06的整数: a=Reader.nextInt(); while(a6); switch(a) case 0:System.out.println(今天是星期日 br
7、eak; case 1:今天是星期一 case 2:今天是星期二 case 3:今天是星期三 case 4:今天是星期四 case 5:今天是星期五 case 6:今天是星期六 default:输入错误 实验15、编写一个java程序,先显示15各个整数;然后在显示110各个奇数。/JumpSentence.javapublic class JumpSentence for(int i=1;i10;i+) if(i=6) break; System.out.print(+i);t n显示完毕 for(int i=1; if(i%2=0) continue; System.out.print(
8、System.out.print( System.out.println(实验19(2)/StringA.javapublic class StringA String str1=Hello String str2= String str3=new String( String str4=new String(运算符=str1=str2+(str1=str2);str3=str4+(str3=str4);equals方法str1.equals(str2)?+str1.equals(str2);str3.equals(str4?)+str3.equals(str4);实验20 编写一个java程
9、序,对两个字符串进行连接、比较及显示其长度等操作。/StringB.javapublic class StringBWorld! String str=str1+str2;str=+str);str的长度是+str.length();str的第三个字符是+str.charAt(3);str中字符串or第一次出现的位置+str.indexOf(or); System.out.println(str.toLowerCase(); System.out.println(str.toUpperCase();实验21 编写一个java程序,如果没有命令行参数,显示“没有输入参数”;否则,显示用户共输入多少个参数,并显示各个参数内容。/CommandInput.javapublic class CommandInput if(args.length=0)没有输入参数 else共输入了+args.length+个参数 for(String arg:args) System.out.print(arg);
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2