Java考试参考.docVIP

  • 42
  • 0
  • 约1.22万字
  • 约 4页
  • 2017-03-26 发布于江苏
  • 举报
Java考试参考

1.编写一个类,该类创建的对象可以计算等差数列的和。 代码: class leisum { static int qsz,bcz,xs,sum; leisum(int a,int b,int c){qsz=a; bcz=b; xs=c; } int sum(){ return sum=xs*qsz+xs*(xs-1)/2*bcz; }} class example{ public static void main(String args[]){ int sum=0; leisum leisum=new leisum(1,1,100); sum= leisum.sum(); System.out.println(等差数列的和为:+sum);}}class lader{ double a,b; static double c; double x1,x2; boolean boo; public lader(double a,double b,double c){ this.a=a; this.b=b; lader.c=c;} public void getRoots(){ if(a!=0){ System.out.println(一元2次方程:); double d=b*b-4*a*c; if(d=0){x1=(-b+Math.sqrt(d))/(2*a);x2=(-b-Math.sqrt(d))/(2*a); System.out.printf(方程的根:x1=%f,x2=%f\n,x1,x2);}else{ System.out.printf(方程没有实根\n);} }else{ System.out.println(不是一元2次方程);} } }public class liti2_5{ public static void main(String args[ ]){ lader reluctance1=new lader(2,5,1); lader reluctance2=new lader(3,2,2); reluctance1.getRoots(); reluctance2.getRoots(); }} 3.编写一个应用程序,用户从键盘输入一行字符串,程序输出该字符串中与模式”[24680]A[13579]{2}”匹配的子字符串。 代码: import java.util.Scanner; import java.util.regex.*; public class one { public static void main(String[] args){ System.out.println(请输入一行字符串:); Scanner reader=new Scanner(System.in); String str=reader.nextLine(); Pattern p=Ppile([24680]A[13579]{2}); Matcher m=p.matcher(str); while(m.find()){ String s=m.group(); System.out.print(从+m.start()+到+m.end()+匹配模式子序列:);System.out.println(s); } } } 4. 编写一个应用程序,将用户从键盘输入的10行文字存入文件。 代码: import java.io.*; import java.util.*; public class one { public static void main(String args[]){ Scanner reader=new Scanner(System.in); try{ FileOutputStream writefile=new FileOutputStream(hello.txt); int line=1,n=10; System.out.println(输入+n+行文本,并存入磁盘:); while(line=n){ String s=reader.nextLine(); byte buffer[]=s.getBytes(); writefile.write(buffer,0,buffer.length); line++; } writefile.close(); } catch(IOException e){ System.out.println(Error +e);} File f=new File(hello.txt); try{ int i=-1; RandomAccessFile random=new RandomAccessFile(f,rw); random.see

文档评论(0)

1亿VIP精品文档

相关文档