java简单代码

//99
for(int i=1;i<=9;i++) {
for(int j=1;j<=i;j++) {
System.out.print(i+"*"+j+"="+i*j+"\t");
    }System.out.println();}}}
//水仙花
int x = 0,y = 0,z = 0;
for(int i = 100;i<1000;i++) {
    x = i/100;
    y = (i%100)/10;
    z = (i%100)%10;
    if(x*x*x + y*y*y +z*z*z == i) {
System.out.print(i+" ");}}}}

//三角形
for(int i=1;i<=5;i++){
   for(int j=5; i<=j; j--)
     System.out.print(" ");//左上角空白
   for(int j=1; j<=i; j++)
     System.out.print("*");//左半三角形
   for(int j=1; j<i; j++)
      System.out.print("*");
      System.out.println();}
//继承
public class jicheng {
public static void main(String[] args) {
student s=new student(); s.eat();
teacher t=new teacher();t.eat();}}
class person{
    String name; void eat() {};}
class student extends person{}
class teacher extends person{}
//手机类
public class phone {
public static void main(String[] args) {
     mobile a=new mobile();
a.name="华为";  a.color="紫色"; a.price="6799";
mobile.call();   mobile.message();}}
class mobile{
static String name; static String color;
    static String price;
    public static void call() {
System.out.println(price+"元的"+color+"的"+name+"正在通话。。");    }
static void message() {
System.out.println(price+"元的"+color+"的"+name+"正在发信息。。");    }}
//封装 getset
public class fengzhung {
    private String name;
    public String getName() {
        return name;}
    public void setName(String name) {
        this.name = name;}
    public void say() {
        System.out.println("我是" + this.name);}
    public static void main(String[] args) {
    fengzhung t = new fengzhung();
    t.setName("东海");
    t.say();
    System.out.println("调用getName" +t.getName());}}
//接口 算面积
public class jiekou {
    public static void main(String[] args) {
    S s= new S();
    System.out.println("边长为2的正方形面积为:"+s.area(2));
    C c=new C();
    System.out.println("半径为3的圆的面积"+c.area(3));}}
interface Shape{
    public abstract double area(double d); }    
class S implements Shape{
    public double area(double d) {
    double S; S=d*d;
        return S;    }}
class C implements Shape{
    public double area(double d) {
    final double PI=3.14; double S;
    S=d*d*PI;  return S;}}
//线程
public static void main(String[] args) {
     Teacher t = new Teacher();
     new Thread(t,"线程1").start();
     new Thread(t,"线程2").start();
     new Thread(t,"线程3").start();}}
class Teacher implements Runnable{
    static int index = 80;
    public void run(){
       while(true){show();
       if(index <= 0){    break; }} }
    public synchronized void show(){
        if(index > 0){
     try{   Thread.sleep(100);
System.out.println(Thread.currentThread().getName()+"发卷"+"剩余"+index--);
            }catch (Exception e){
                e.printStackTrace(); } } }}
//字节流  字符流把InputStream换位Reader,OutputStream换Writer
public static void main(String[] args)throws IOException { 
FileInputStream in=new FileInputStream("E:\\Java\\ch7.txt");
     BufferedInputStream bin=new BufferedInputStream(in);
     FileOutputStream  out=new FileOutputStream("E:\\Java\\java.doc ");
     BufferedOutputStream bout=new BufferedOutputStream(out);
    int len;//String line=null;
double f=System.currentTimeMillis();  
while((len=bin.read())!=-1){//(line=br.readLine())!=nul
bout.write(len);}//bw.write(line);
 double e=System.currentTimeMillis();//获取文件拷贝结束时的系统时间
// //System.out.println("拷贝文件的时间是:"+((e-f)+"毫秒"));
 bin.close();
bout.close();  }
//字节流字符流转换
FIS in=new FIS("E:\\Java\\ch7.txt");
ISR isr=new ISR(in);
BufR br=new BufR(isr);
FOS out=new FOSt("E:\\Java\\java.doc ");
OSW osw=new OSW(out);
BW bw=new BW(osw);
String line=null;。。。
//文本框的事件处理程序
public class ch7 extends JFrame implements ActionListener{
    JPanel jp;  JTextField jt1,jt2;
    JLabel jl;  JButton jb;
  public ch7(){
    this.setTitle("欢迎登录这个系统!");
    jp = new JPanel();
    jl = new JLabel("请输入:");
    jt1 = new JTextField(15);
    jt2 = new JTextField(15);
    jb = new JButton("触发");
    jb.setSize(100,10);
   jp.add(jl);  jp.add(jt1);  jp.add(jb);  jp.add(jt2);
    jb.addActionListener(this);
this.add(jp);  this.setVisible(true);
this.setSize(280,220);}
public void actionPerformed(ActionEvent actionEvent) {
   String str = jt1.getText();
   jt2.setText("欢迎光临"+jt1.getText());}
public static void main(String[] args) {new ch7();}}
//IP
try{InetAddress otheraddr = InetAddress. getByName("www.baidu.com");
InetAddress localaddr=InetAddress.getLocalHost();//获得IP地址
String wName=otheraddr.getHostName();//获得主机名
String IPName=otheraddr.getHostAddress();//获得IP地址
System.out.println("网址名称:"+wName);
System.out.println("网址ip"+IPName);
System.out.println(otheraddr);
System.out.println("本机"+localaddr);}
 catch (UnknownHostException e) {
    e.printStackTrace();}
//tcp客户端
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("TCP 通信成功!");return;}
Socket s = new Socket(InetAddress.getByName(args[0]), Integer.parseInt(args[1]));
InputStream ips = s.getInputStream();
OutputStream ops = s.getOutputStream();
BufferedReader brKey = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos = new DataOutputStream(ops);
BufferedReader brNet = new BufferedReader(new InputStreamReader(ips));
while (true) {
    String strWord = brKey.readLine();
    dos.writeBytes(strWord + System.getProperty("line.separator"));
if ("quit".equalsIgnoreCase(strWord)) {break;} 
else {System.out.println(brNet.readLine());}}
dos.close();brNet.close();brKey.close();s.close();
//tcp服务器
ServerSocket ss = new ServerSocket(8000);
Socket s = ss.accept();
InputStream ips = s.getInputStream();
OutputStream ops = s.getOutputStream();
ops.write("hello,World!".getBytes());
byte[] buf = new byte[1024];
int len = ips.read(buf);
System.out.println(new String(buf, 0, len));
ips.close();ops.close();s.close();ss.close();
//udp发送程序
DatagramSocket ds = new DatagramSocket();
String str = "UDP 通信成功!!";
DatagramPacket dp = new
DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getByName("localhost"),8000);
ds.send(dp);
ds.close(); 
//udp接收程序
DatagramSocket ds = new DatagramSocket(8000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
String str = new String(dp.getData(),0,dp.getLength());
System.out.println(str);
//System.out.println("IP:"+dp.getAddress().getHostAddress() + ",PORT:" + dp.getPort());
ds.close();
//集合
 List l = new ArrayList();
    l.add(1);  l.add(2);   l.add(3);
    Object s[]=l.toArray();//for循环遍历
for(int u=0;u<s.length;u++) {
    System.out.println(s[u]);}       
   Iterator i = l.iterator(); //用迭代器遍历
     while(i.hasNext()){
      System.out.println(i.next()); }}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值