java练习题、编程题

上传人:第*** 文档编号:32820549 上传时间:2018-02-12 格式:DOC 页数:14 大小:113KB
返回 下载 相关 举报
java练习题、编程题_第1页
第1页 / 共14页
java练习题、编程题_第2页
第2页 / 共14页
java练习题、编程题_第3页
第3页 / 共14页
java练习题、编程题_第4页
第4页 / 共14页
java练习题、编程题_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《java练习题、编程题》由会员分享,可在线阅读,更多相关《java练习题、编程题(14页珍藏版)》请在金锄头文库上搜索。

1、第二章注意问题: 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.prin

2、tln(您本月薪资为 $+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;

3、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(

4、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_3public static void main(String args)throws IOExce

5、ptionint 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(Wednesda

6、y); 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; i0) re

7、sult=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_1public static void main( String args ) System

8、.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 resul

9、t=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

10、 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()whil

11、e( 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(

12、i=t.number)t.available=false;4,线程不断显示递增整数,按下回车键则停止执行import java.io.*;public class Ex8_12public static void main(String args) throws IOExceptionTestThread t=new TestThread();t.start();new BufferedReader(new InputStreamReader(System.in).readLine(); /等待键盘输入t.stopme(); /调用 stopme 方法结束 t 线程class TestThread extends Threadprivate boolean flag=true;public void stopme() /在此方法中控制循环条件flag=false;public void run() int i=0;while(flag) System.out.println(i+); /如果 flag 为真则一直显示递增整数

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

当前位置:首页 > 建筑/环境 > 工程造价

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