java类的定义以及参数传递

class类(类似结构体)的定义

import java.util.Scanner;
import java.io.*;
class student{//类的名称
	public String name;
	public String num;
	public int age;//定义类中属性:姓名,学号,年龄
	public student(){//无参的构造函数
		
	}
	public student(String name1,String num1,int age1){//有参的构造函数
		name=name1;
		num=num1;
		age=age1;
	}                  
	public String getname(){
		return name;                                                                                                     
	}
	public String getnum(){
		return num;
	}
	public int getage(){
		return age;
	}
	public void setname(String Name){
		name=Name;
	}
	public void setnum(String Num){
		num=Num;
	}
	public void setage(int Age){
		age=Age;
	}
	public String Detail(){//返回图书信息
		return name+" "+num+" "+age;
	}
}
public class Main {
	
	public static void main(String[] args){
		int n,i;
		Scanner cin=new Scanner(System.in);
		student[] stu=new student[109];
		//for(i=0;i<stu.length;i++)
			//stu[i]=new student();
		while(cin.hasNext()){
			n=cin.nextInt();
			for(i=1;i<=n;i++)
			{
				//stu[i].setname(cin.next());
				//stu[i].setnum(cin.next());
				//stu[i].setage(cin.nextInt());
				stu[i]=new student(cin.next(),cin.next(),cin.nextInt());
			}
			for(i=1;i<=n;i++)
				System.out.println(stu[i].Detail());
		}
	}
}

import java.util.Scanner;
import java.io.*;
public class Main {
	
	public static void main(String[] args){
		int n,i;
		Scanner cin=new Scanner(System.in);
		int a[]=new int[1009];
		while(cin.hasNext()){
			n=cin.nextInt();
			for(i=0;i<n;i++)
				a[i]=cin.nextInt();
			Output(a,n);
			System.out.println(Sum(a,n));
		}
	}
	public static void Output(int a[],int n){
		for(int i=0;i<n;i++)
			System.out.print(a[i]+" ");
		System.out.println();
	}
	public static int Sum(int a[],int n){
		int sum=0;
		for(int i=0;i<n;i++)
			sum+=a[i];
		return sum;
	}
}


posted @ 2014-12-23 22:53  一样菜  阅读(4278)  评论(0编辑  收藏  举报