1.public class Calculator{
static int sum=0;
static void add(int a,int b){
// int sum=0;
sum =a+b;
System.out.println (sum);
}
static void sub(int a,int b){
//int sum=0;
sum=a-b;
System.out.println (sum);
}
static void mul(int a,int b){
// int sum=0;
sum=a*b;
System.out.println (sum);
}
static void div(int a,int b){
// int sum=0;
sum=a/b;
System.out.println (sum);
}
public static void main(String[] args){
add(1,2);
sub(2,1);
mul(1,2);
div(2,1);
}
}
2.public class Polygon{
void printRec(int height,int width){
for(int a=0;a<=height;a++){
for(int j=0;j<=width;j++){
System.out.print ("*");
}
System.out.println ();
}
}
void printTri(int height){
for(int a=0;a<=height;a++){
for(int j=a;j<height;j++){
System.out.print("*");
}
System.out.println ();
}
}
public static void main(String[] args){
Polygon p=new Polygon();
p.printRec(5,4);
System.out.println ();
p.printTri(5);
}
}
3.public class Worker{
String name;
int price;
int num;
String place;
Worker(String name,int price,int num,String place){
this.name=name;
this.price=price;
this.num=num;
this.place=place;
}
void dispaly(){
System.out.println ("姓名:"+name);
System.out.println ("工资:"+price);
System.out.println ("工号:"+num);
System.out.println ("工作地:"+place);
}
public static void main(String[] args){
Worker w=new Worker("DK",1000,99,"wuhan");
w.dispaly();
}
}
4.public class Compare{
int num1,num2,num3;
void max(){
if(num1>num2){
if(num1>num3){
System.out.println (num1);
}else{
System.out.println (num3);
}
}else{
if(num2>num3){
System.out.println (num2);
}else{
System.out.println (num3);
}
}
}
void min(){
if(num1>num2){
if(num2>num3){
System.out.println (num3);
}else{
System.out.println (num2);
}
}else{
if(num1>num3){
System.out.println (num3);
}else{
System.out.println (num1);
}
}
}
void avg(){
System.out.println ((num1+num2+num3)/3);
}
public static void main(String[] args){
Compare c=new Compare();
c.num1=1;
c.num2=2;
c.num3=3;
c.max();
c.min();
c.avg();
}
}
5.public class User{
String 用户名;
String 密码;
String 用户权限;
User(String name,String password,String quanxian){
用户名=name;
密码=password;
用户权限=quanxian;
}
void login(){
if(用户名=="admin"&&密码=="123"){
System.out.println ("登陆成功");
System.out.println (用户权限="administrator");
}else{
if(用户名=="snake"&&密码=="123456"){
System.out.println ("登陆失败");
System.out.println (用户权限="user");
}
}
}
public static void main(String[] args){
User u= new User("snake","123456"," ");
u.login();
}
}
posted on 2008-04-01 19:51
矿矿 阅读(1427)
评论(7) 编辑 收藏