当前位置:首页 » 编程语言 » 经典java代码

经典java代码

发布时间: 2024-07-15 17:24:44

‘壹’ java线程的经典代码

package threadgroup;

class ThreadDemo3 extends Thread {
private String name;
private int delay;

public ThreadDemo3(String sname, int i_delay) {
name = sname;
delay = i_delay;

}

public void run() {
try {
sleep(delay);
} catch (InterruptedException e) {

}
System.out.println("多线程测试!\n" + name + "\n" + delay);
}

}

public class testMyThread {

public static void main(String[] args) {
ThreadDemo3 th1,th2,th3;

th1 = new ThreadDemo3("线程1", (int) (Math.random() * 900));
th2 = new ThreadDemo3("线程2", (int) (Math.random() * 900));
th3 = new ThreadDemo3("线程3", (int) (Math.random() * 900));
th1.start();
th2.start();
th3.start();

}
}

package threadgroup;

public class threadDemo {
public static void main(String[] args) {
Thread t = Thread.currentThread();
t.setName("你好吗?");
System.out.println("正在进行的Thread是:" + t);
try {
for (int i = 0; i < 5; i++) {
System.out.println("我不叫穆继超" + i);
Thread.sleep(3000);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}

}

}

package threadgroup;

public class threadDemo2 implements Runnable {
public threadDemo2() {
Thread t1 = Thread.currentThread();
t1.setName("第一个主进程");
System.out.println("正在运行" + t1);
Thread t2 = new Thread(this, "");
System.out.println("在创建一个进程");
t2.start();
try {
System.out.println("使他进入第一个睡眠状态");
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第一个进程");
}

public void run() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("进程" + i);
Thread.sleep(3000);
}
} catch (InterruptedException e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第二个进程");

}

public static void main(String[] args) {
new threadDemo2();
}
}

‘贰’ 求一个简单java程序代码,谢谢

public class TestStar {
public static void main(String[] args) {
String star = "*";
for (int i = 0; i < 5; i++) {
if (i == 0) {
System.out.print(" " + star);
System.out.println();
}
if (i == 1) {
for (int z = 0; z < 4; z++) {
System.out.print(" " + star);
}
System.out.println();
}
if (i == 2) {
System.out.print(" ");
for (int x = 0; x < 3; x++) {
System.out.print(" " + star);
}
System.out.println();
}
if (i == 3) {
for (int y = 0; y < 2; y++) {
System.out.print(" " + star + " ");
}
}
}
}
}

是好使的 但是我没找到画五角星有什么规律(五角星好象不是正规图形吧?)如果还有什么要求的话 补充问题(如果是用*填充所有的东西 不包括 “ ”的话 我可以重新再给你写一个)

‘叁’ 求一个简单的java代码

public class Message {
public static void main(String[] args){
String name;
int age;

System.out.println("请输入姓名,回车结束:"); //提示输入
Scanner sc = new Scanner(System.in);
name = sc.nextLine(); //为变量赋值
System.out.println("请输入年龄,回车结束:");
age = sc.nextInt();

System.out.println("姓名:"+name+"\n年龄:"+age); //打印姓名及年龄
}
}
//不知道这样行么?

‘肆’ Java100行以上源代码,至少五个class以及一个interface,可以简单点

下面是一个可能的Java源代码,它包含了一个接口租冲薯(Shape)和五个类(Circle, Rectangle, Triangle, Square 和 Main)。它的功能是计算不同形状的面积和周长。
//定义一个接口Shape,有两判指个抽象方法:getArea()和getPerimeter()interface Shape { double getArea(); double getPerimeter();
}//定义一个类Circle,实现Shape接口class Circle implements Shape { //定义一个私有属性radius,表示圆的半径
private double radius; //定义一个公有构造方法,用于初始化radius
public Circle(double radius) { this.radius = radius;
} //实现getArea()方法,返回圆的面积
public double getArea() { return Math.PI * radius * radius;
} //实现getPerimeter()方法,返回圆的周长
public double getPerimeter() { return Math.PI * radius * 2;
}
}//定义一个类Rectangle,实现Shape接口class Rectangle implements Shape { //定义两个私有属性width和height,表示矩形的宽度和高度
private double width; private double height; //定义一个公有构造方法,用于初始化width和height
public Rectangle(double width, double height) { this.width = width; this.height = height;
} //实现getArea()方法,返回矩形的面积
public double getArea() { return width * height;
} //实现getPerimeter()方法,返回矩形的周长
public double getPerimeter() { return (width + height) *2;
}
}//定义一个类Triangle,实现Shape接口class Triangle implements Shape { //定义三个私有属性a,b,c表示三角形的三条边长
private double a; private double b; private double c; //定义一个公有构造方法,用于初始化a,b,c,并检查是否满足三角形条件(任意两边之和大于第三边)
public Triangle(double a, double b, double c) throws Exception{ if (a + b > c && a + c > b && b + c > a) {
this.a = a; this.b = b;
this.c = c;
} else {
throw new Exception("Invalid triangle");
}
} //实现getArea()方法,返回三角形的面积(使用海伦公式)
public double getArea() { //计算半周长p
double p = (a + b + c) /2; //计算并返回面积s(使用Math.sqrt()函数求平方根)
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
} //实现getPerimeter()方法,返回三角形的周长
public double getPerimeter(){ return a + b + c;
}
}//定义一个类Square,继承Rectangle类,并重写构造方法和toString()方法class Square extends Rectangle { //重写构造方法,在调用父类构造方法时传入相弊者同的参数side作为width和height
public Square(double side){ super(side, side);
} //重写toString()方法,在原来基础上加上"Square:"前缀,并只显示side属性而不显示width和height属性(使用String.format()函数格式化字符串)
@Override
public String toString(){ return String.format("Square: side=%.2f", super.width); /* 或者直接使用super.getPerimeter()/4作为side */
/* return String.format("Square: side=%.2f", super.getPerimeter()/4); */

/* 注意:不能直接访问super.side属性,

‘伍’ 给段最简单的java代码 让我新手看一下

最简单的java代码肯定就是这个了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!

‘陆’ 一个简单的Java程序代码

public double GetCost(int minutes)
{
//整数时间所花的费用
int aa = minutes / 60; //未满1小时处理
if (minutes < 60)
return 2;

//超出小时部分
int bb = minutes % 60; //其实你还有必要做一些其他处理。比如说超过30分钟了该怎么样算等等...... return aa * 2 + bb * 0.01d;
}

‘柒’ 求java经典小程序代码

  • 代码如下:

public class HelloWorld {

public static void main(String []args) {

int a = 3, b = 7 ;

System.out.println("Hello World!");

}

public static int f(int a, int b){

return a*a + a*b + b*b;

}

}

  • 结果如下:

热点内容
qq系统头像文件夹 发布:2024-10-18 14:14:55 浏览:234
安卓手机请输入密码在哪里 发布:2024-10-18 14:13:28 浏览:645
设计编译程序注意的问题 发布:2024-10-18 14:08:43 浏览:254
传智播客android视频 发布:2024-10-18 14:04:42 浏览:904
手机版安卓吃鸡哪个好 发布:2024-10-18 14:01:40 浏览:491
编程自学入门教程 发布:2024-10-18 13:50:58 浏览:141
伊迪阿明访问中国 发布:2024-10-18 13:49:54 浏览:10
人三琳外传脚本 发布:2024-10-18 13:38:16 浏览:839
电脑发件箱服务器错误怎么弄 发布:2024-10-18 13:30:36 浏览:914
evm部署solc编译文件 发布:2024-10-18 13:29:47 浏览:835