
这个规则就是设计一个方法,该方法最后值为一个票价,参数应该是身高,所以有如下代码
public class PriceTest {public static void main(String[] args) {
int childPrice = getPrice(1.2)
int adultPrice = getPrice(1.5)
System.out.println("1.2米身高的票价为:"+childPrice)
System.out.println("1.5米身高的成人票价为:"+adultPrice)
}
private static int getPrice(double height) {
return height > 1.2?10:5
}
}
最后结果如下:
将Java程序打包成exe可执行文件第一步:将Java程序通过Eclipse或者JRE导出成Jar包;
第二步:通过exe4j将Jar包程序生成exe可执行文件。
作为毕业设计,要求你交执行程序肯定是有办法实现的啊,当然你也可以用脚本执行,不过既然要交源码,都到这里了打包成exe也就用不了什么时间了
public class TicketsSystem {public static void main(String[] args) {
SellThread st = new SellThread()
Thread th1 = new Thread(st)
th1.start()
Thread th2 = new Thread(st)
th2.start()
Thread th3 = new Thread(st)
th3.start()
}
}
class SellThread implements Runnable {
private int number=10
String s = new String()
public void run() {
while (number >0) {
synchronized (s) {
System.out.println("第" + number + "个人在"
+ Thread.currentThread().getName() + "买票")
}
number--
try {
Thread.sleep(10)
} catch (InterruptedException e) {
e.printStackTrace()
}
}
}
}
synchronized (s)的s是synchronized 的参数,synchronized 的参数可以是任意对象,我定义了一个String类型的对象s,方便看程序而已。
但是要注意,s的定义一定要在run方法之外,不然还会出现负数。因为你启动了3个线程,每个线程都调用了run方法,在每个线程就会在run方法里边产生自己的s对象,一共会产生3个,达不到同步的目的。
如果还有不明白的,可以继续问。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)