1、从1加到100
public class addsum
{
public static void main(String[] args)
{
int i=1,sum=0;
for(;i<101;i++)sum+=i;
System.out.print(sum);
}
}
2、输出菱形
//设计一程序,打印菱形
public class ling
{
public static void main(String[] args)
{
System.out.println("开始打印菱形:\n");
int i=1,j=1,k=0;
int iMax=13;//最大一行等于9
//第一行等于1 需要补空格数等于 iMax-1/2
for(;i<=iMax;i=i+2)
{
System.out.print(" ");
for(j=1;j<=((iMax-1)/2-k);j++)
System.out.print(" ");
for(j=0;j<i;j++)
System.out.print("*");
System.out.println("\n");
k++;
}
i=iMax-2;
k=0;
for(;i>0;i=i-2)
{
System.out.print(" ");
k++;
for(j=1;j<=k;j++)
System.out.print(" ");
for(j=0;j<i;j++)
System.out.print("*");
System.out.println("\n");
}
System.out.println("\n菱形输出完毕。");
}
}
[Script End]
[Script]
用AWT写的一个简单的Applet
//:Button2New.java
//Capturing button presses
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Button2New extends Applet{
Button
b1=new Button("yeqiufeng"),
b2=new Button("qfkx");
public void init(){
b1.addActionListener(new B1());
b2.addActionListener(new B2());
add(b1);
add(b2);
}
class B1 implements ActionListener{
public void actionPerformed(ActionEvent e){
getAppletContext().showStatus("Button 1");
}
}
class B2 implements ActionListener{
public void actionPerformed(ActionEvent e){
getAppletContext().showStatus("Button 2");
}
}
}
用新的事件模型写的按钮们:
//:Button2New.java
//Capturing button presses
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Button2New extends Applet{
Button
b1=new Button("邓东亚"),
b2=new Button("李闯");
public void init(){
b1.addActionListener(new B1());
b2.addActionListener(new B2());
add(b1);
add(b2);
}
class B1 implements ActionListener{
public void actionPerformed(ActionEvent e){
getAppletContext().showStatus("Button 1");
}
}
class B2 implements ActionListener{
public void actionPerformed(ActionEvent e){
getAppletContext().showStatus("Button 2");
}
}
}
编译成功后 用Dreamweaver把class插入到网页中 调整一下外观大小 用浏览器打开 就可以啦
如果Applet可以读取XML 我会把我的网站……哼哼……
今天的练习是网络编程
先用以下示例来了解一下网络编程
[Script Begin]
import java.net.*;
public class WhoAmi{
public static void main(String[] args)
throws Exception{
if(args.length!=1){
System.err.println("Usage:WhoAmi MachineName");
System.exit(1);
}
InetAddress a=InetAddress.getByName(args[0]);
System.out.println(a);
}
}
//调用示例 java WhoAmi yqf
//返回 yqf/10.1.1.20
[Script End]