java的经典代码(java线程的经典代码)

本文目录
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,th3,th3;
th1 = new ThreadDemo3("线程1", (int) (Math.random() * 900));
th3 = new ThreadDemo3("线程2", (int) (Math.random() * 900));
th3 = new ThreadDemo3("线程3", (int) (Math.random() * 900));
th1.start();
th3.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的代码
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\\a.txt")), "GBK"));
List《String》 list = new ArrayList《》();
String str;
while((str = br.readLine()) != null){
list.add(str);
}
br.close();
System.out.println(list);
Collections.sort(list);
System.out.println(list);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("E:\\b.txt")),"GBK"));
for(String s : list){
writer.append(s);
writer.append("\r\n");
}
writer.close();
}
}
有关Java的代码
1. 你在do循环体内重复定义了answer变量,这不需要
2. 你用==比较字符串内容,是不行的。需要用equals来比较
==是用来比较两引用是否相同,而不是两个引用的对象内容是否相同的。
import java.util.Scanner;
public class Test3 {
public static void main(String args) {
String answer="";
do{
Scanner input=new Scanner(System.in);
answer=input.next();
System.out.println("你输入的是:" + answer);
if(answer.equals("你好")){
System.out.println("你好!我是小P,你是谁啊?");
}else if(answer.equals("我是令狐冲")){
System.out.println("你终于回来啦!好想你哦!");
}else if(answer.equals("再见")){
System.out.println("再见!下次还找我来玩哦!");
}else {
System.out.println("对不起,我不懂你在说什么");
}
} while(!answer.equals("再见"));
System.out.println("程序结束");
}
}
Java代码编程 经典的兔子问题
这是斐波那契数列的问题
可以用递归,也可以用循环
递归:
public class Demo3 {
// 使用递归方法
private static int getFibo(int i) {
if (i == 1 || i == 2)
return 1;
else
return getFibo(i - 1) + getFibo(i - 2);
}
public static void main(String args) {
System.out.println("斐波那契数列的前20项为:");
for (int j = 1; j 《= 20; j++) {
System.out.print(getFibo(j) + "\t");
if (j % 5 == 0)
System.out.println();
}
}
}
循环:
public class Demo2 {
// 定义数组方法
public static void main(String args) {
int arr;
arr = 1;
for (int i = 2; i 《 arr.length; i++) {
arr;
}
System.out.println("斐波那契数列的前20项如下所示:");
for (int i = 0; i 《 arr.length; i++) {
if (i % 5 == 0)
System.out.println();
System.out.print(arr + "\t");
}
}
}

更多文章:
计算机网络协议指的是tcp ip协议(TCP/IP协议是什么)
2025年10月21日 07:15
oracle数据库备份文件后缀(ORACLE 数据库导出的数据文件 什么后缀)
2026年4月7日 10:45
新加坡marina square风景(新加坡Marina Bay Sands空中花园设计欣赏)
2026年4月3日 10:45
datagridview设置表头(Datagridview表头样式设置)
2026年9月3日 13:45
mysql workbench查看表结构(在使用mysqlworkbench6.3怎么查看mysql数据库的关系图)
2025年7月31日 04:30
curl访问地址(php curl访问本地测试地址 链接超时,什么原因.公网可以)
2026年2月15日 19:15
object后面接什么(高中英语 object 意思为“拒绝“时的用法 后面跟什么)
2026年6月30日 19:00
win10系统java环境变量设置(win10怎么配置java环境变量path)
2025年6月16日 13:45
jquerytrigger参数(怎么给jquery方法传值 就是点击第2个button后执行第一个button的方式 怎么给s传值)
2025年11月5日 00:15
selective tool教程(selective tool能卸载吗)
2025年5月30日 06:30












