Java练习题、编程题.doc_第1页
Java练习题、编程题.doc_第2页
Java练习题、编程题.doc_第3页
Java练习题、编程题.doc_第4页
Java练习题、编程题.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第二章注意问题: main函数是Java应用程序执行的入口。(int)23.7结果为23(int)-45.89f结果为-45+”除字符串相加功能外,还能把非字符串转换成字符串 ,如: “x”+123;的结果是“x123” 。 如果对负数取模,可以把模数负号忽略不记,如:5%-2=1。但被模数是负数就另当别论了1, 三元运算符 class TernaryOp public static void main(String args) int salary,daysPresent = 30; salary = daysPresent = 20 ? 2000 : 3000; System.out.println(您本月薪资为 $+salary); ,编写一个程序,这个程序从键盘上输入2个数,然后求这2个数的和,程序结束。提示:调用System.in.read();可以读取到一个从键盘上输入字符对应的整数。通过这个程序,你一定能体会到在什么 import java.io.*;public class Ch1 public static void main(String args) throws IOExceptionBufferedReader ke=new BufferedReader(new InputStreamReader(System.in);String x;int p,q,m;System.out.print(请输入第一个数);x=ke.readLine();p=Integer.parseInt(x);System.out.print(请输入第二个数);x=ke.readLine();q=Integer.parseInt(x);m=p+q;System.out.println(两个数的和为:+m); ,数组public class Lo public static void main(String args) int aa=32,55,69;for(int i=0;iy) System.out.println(xy); else System.out.println(x= 90) grade = A; else if (testscore = 80) grade = B; else if (testscore = 70) grade = C; else if (testscore = 60) grade = D; else grade = F; System.out.println(Grade = + grade); ,输入06之间的某一个整数,然后把它转换成星期 输出。(0对应星期日)import java.io.*;public class ex3_3 public static void main(String args)throws IOException int day; BufferedReader in =new BufferedReader( new InputStreamReader(System.in); day=(new Integer(in.readLine().intValue();switch (day) case 0: System.out.println(Sunday); break; case 1: System.out.println(Monday); break;case 2: System.out.println(Tuesday); break; case 3: System.out.println(Wednesday); break; case 4: System.out.println(Thursday); break; case 5: System.out.println(Friday); break; case 6: System.out.println(Saturday); break; default: System.out.println(Day out of range Sunday .Saturday ); break; ,打印九九乘数表public class MultiTable public static void main(String args) for (int i=1; i=9;i+) for (int j=1; j=i;j+) System.out.print( +i+*+j+=+i*j); System.out.println(); ,输入一个整数,输出它所有的因数import java.io.*;public class ex3_7 public static void main(String args) throws IOException int n,k; BufferedReader in =new BufferedReader( new InputStreamReader(System.in); System.out.println(Enter a positive integer: ); n=(new Integer(in.readLine().intValue(); System.out.print(Number +n+ Factors ); for (k=1; k = n; k+) if (n % k = 0) System.out.print(k + ); System.out.println(); ,计算数列1,2,10 的和。public class ex3_4 public static void main(String args) int i=1, sum=0; while(i=10) sum+=i; i+; System.out.println(sum=+sum); ,计算存款收益:假设银行中存款10000元,按11.25%的利率,一年后连本带利将变为11125元。你若将此款继续存入银行,试问多长时间就会连本带利翻一番import java.text.*;public class Examp3_4 public static void main(String args) double original,money,interest; int years = 0; original = money= 10000; interest = 11.25 / 100; System.out.println(year money);while (money 2 * original) years = years + 1; money = money + (interest * money); System.out.println( + years + + new DecimalFormat(0.00).format(money); System.out.println(); System.out.println( 第 + years + 年存款额达到 + new DecimalFormat(0.00).format(money) + 元。); 编写一个程序用选择法对数组a=20,10,50,40,30,70,60,80,90,100进行由大到小的排序。import java.io.*;public class SelectSortpublic static void main(String args)int a=20,10,50,40,30,70,60,80,90,100;int temp;for (int i=0; ia.length-1;i+)for (int j=i+1; ja.length ; j+)if (aiaj) temp=ai;ai=aj;aj=temp;for (int k=0;k0) result=result*i; i=i-1; System.out.println(The factorial of +num+ is +result); System.out.println(new thread ends); 运行结果main thread startsmain thread endsnew thread startedThe factorial of 10 is 3628800new thread ends2,使用Runnable接口实现上述程序功能public class Ex8_1 public static void main( String args ) System.out.println(main thread starts); FactorialThread t=new FactorialThread(10); new Thread(t).start(); System.out.println(new thread started,main thread ends ); class FactorialThread implements Runnable private int num; public FactorialThread( int num ) this.num=num; public void run() int i=num; int result=1; while(i0) result=result*i; i=i-1; System.out.println(The factorial of +num+ is +result); System.out.println(new thread ends); 3,用两个线程模拟存票、售票过程 假定开始售票处并没有票,一个线程往里存票,另外一个线程则往出卖票我们新建一个票类对象,让存票和售票线程都访问它。本例采用两个线程共享同一个数据对象来实现对同一份数据的操作public class Ex8_7 public static void main(String args) Tickets t=new Tickets(10); new Consumer(t).start();new Producer(t).start(); class Tickets int number=0; /票号int size; /总票数boolean available=false; /表示目前是否有票可售public Tickets(int size) /构造函数,传入总票数参数this.size=size; class Producer extends ThreadTickets t=null;public Producer(Tickets t)this.t=t; public void run()while( t.number t.size) System.out.println(Producer puts ticket +(+t.number); t.available=true; class Consumer extends Thread /售票线程Tickets t=null;int i=0;public Consumer(Tickets t) this.t=t; public void run()while(it.size) if(t.available=true & i=t.number) System.out.println(Consumer buys ticket +(+i); if(i=t.number) t.available=false;4,线程不断显示递增整数,按下回车键则停止执行import java.io.*;public class Ex8_12public static void main(String args) throws IOException TestThread t=new TestThread(); t.start(); new BufferedReader(n

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论