當前位置:首頁 » 編程語言 » 經典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;

}

}

  • 結果如下:

熱點內容
如何開張一個租賃伺服器 發布:2024-10-18 11:46:13 瀏覽:825
python解析json文件 發布:2024-10-18 11:29:34 瀏覽:310
編譯程序的生成程序 發布:2024-10-18 11:29:27 瀏覽:403
軌跡處理演算法 發布:2024-10-18 11:22:25 瀏覽:782
支付密碼怎麼破解 發布:2024-10-18 11:09:19 瀏覽:144
線性鏈表c語言 發布:2024-10-18 11:09:17 瀏覽:784
淘寶賣的腳本可靠嗎 發布:2024-10-18 10:54:04 瀏覽:119
數質數演算法 發布:2024-10-18 10:53:26 瀏覽:281
安卓11有的地方怎麼那麼卡 發布:2024-10-18 10:53:21 瀏覽:478
蘋果怎麼設置程序加密 發布:2024-10-18 10:52:41 瀏覽:101