經典java代碼-ag真人国际官网
『壹』 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;
}
}
結果如下: