/*
*命令行参数的输入,从命令行输入的任何参数,对于java来讲都是用字符串处理的
*/
public class CommandInput{
public static void main(String[] args){
if(args.length==0){
System.out.println("你没有输出参数,程序退出");
}
else{
System.out.println("你一共输入了"+args.length+"个参数");
if(args[0].equals("h"))
System.out.print("hello");
if(args[0].equals("g"))
System.out.print("Bye-Bye");
for(int i=1;i<args.length;i++)
{
System.out.print(" "+args[i]);
}
}
}
}
public class CapacityTest{
public static void main(String[] args){
String buffer sb=new StringBuffer(10);
sb.append("this is a a");
System.out.println(sb.capacity());
System.out.println(sb.length());
}
}
/*
*数组排序及随机数的产生
*/
import java.util.Arrays;
import javax.swing.*
public class ArraySort{
public static void main(String[] args){
String strIn=JoptionPane.showInputDialog("请输入工共多少个彩球:");
String strOut=JOptionPane.showInputDialog("请输入需抽取多少个彩球:");
int in=Integer.parseInt(strIn);
int[] total=new int[in];//生成彩球总数数组
for(int i=0;i<in;i++){
total[i]=i+1;
}
int[] out=new int[Integer.parseInt(strOut)];
for(int i=0;i<out.length;i++){
int r=(int)Math.random()*in;//产生随机元素序列号
out[i]=total[r];
total[r]=total[in-1];//将最后一个元素移到当前位置,把取出的删除
in--;
System.out.print(" "+out[i]);}
Arrays.sort(out);
System.out.println("抽取的数字排序后是:");
for (int i=0;i<out.length;i++){
System.out.print(" "+out[i]);
}
System.exit(0);
}}
/**
*通过这个程序,我们要测试两个方面:
*1.匿名数组可以赋值与一个已经存在数组变量,不关心原来数组变量的容量
*2.数组变量的类型必须要与匿名数组的类型一致
public class ArrayTest{
public static void main(String[] args){
ArrayTest aTest=new ArrayTest();
int[] a={1,2,3};//声明一个新的数组,并赋值与一个新的数组变量
aTest.aprint(a);
a=new int[]{10,20,30,40,50};//将匿名数组赋值与存在的数组变量a
//a=new String[]{"a","b"};//将字符型匿名数组赋值与存在的数组变量a
aTest.print(a);
}
public void print(int[] array){
System.out.println(" 数组变量的长度是 "+array.length);
System.out.println(" 数组中的每一个元素是:");
for(int i=0;i<array.length;i++)
{
System.out.print(" "+array[i]);//打印出数组中的每一个元素
}System.out.println("\n*************");
}
}
/**
*测试数组元素传递方式
*/
public class ArrayPointer{
public static void main(String[] args){
ArrayPointer aPointer=new ArrayPinter();
int[] a={1,2,3,4,5};
System.out.println(打印数组a中的元素);
aPointer.print(a);
int[] b=a;
System.out.println("\n改变数组b中的第三个元素的值。\n ");
System.out.println("打印数组b中的元素。");
b[2]=a[2]+10;
aPointer.print(b);
System.out.println("再打印数组a中的元素。");
aPointer.print(b);
}
public void print(int[] array){
for(int i=0;i<array.length;i++){
System.out.println(" "+array[i]);//打印出数组中的每一个元素
}
System.out.println("\n*******************8");
}}
/**
*通过这个程序,测试数组元素默认值
*/
public class ArrayInitTest{
public static void main(String[] args)
{
ArrayInitTest aTest=new ArrayInitTest();
int[] a=new int[10];//声明一个新的整数数组,并赋值与一个新的数组变量
aTest.println(a);//输出数组元素到控制台
boolean[] c=new boolean[10];//声明一个新的布尔数组
aTest.pprintln(c);
}
public void printInt(int[] array){
System.out.println(" 整数型数组的长度是 "+array.length);
for(int i=0;i<5;i++){
array[i]=i;
}
System.out.println(" 数组中的每个元素是: ");
for(int i=0;i<array.length;i++){
System.out.println(" "+array[i]);//打印出数组中的每一个元素
}
System.out.println("\n******************************");
}
public void printString(String[] array){
System.out.println(" 字符型数组的长度是 "+array.length);
for(int i=0;i<5;i++){
array[i]="元素"+i;
}
System.out.println(" 数组中的每个元素是: ");
for(int i=0;i<array.length;i++){
System.out.println(" "+array[i]);//打印出数组中的每一个元素
}
System.out.println("\n****************************");
}
public void printBoolean(boolean[] array){
System.out.println(" 布尔型数组的长度是 "+array.legnth);
for(int i=0;i<5;i++){
if(i%2==0)
array[i]=true;
else
array[i]=false;
}
System.out.println(" 数组中的每个元素是: ");
for (int i=0;i<array.length;i++){
System.out.print(" "+array[i]);//打印出数组中的每一个元素
}
System.out.println("\n**********************************");
}}
/**
*测试数组元素的拷贝
*/
public class ArrayCopy{
public static void main(String[] args){
ArrayCopy aCopy=new ArrayCopy();
int[] a={1,2,3,4,5};
int[] b={10,20,30,40,50};
aCopy.copy(a,b);
}
public void copy(int[] from,int[] to){
System.out.println("第一个数组中的元素");
for(int i=0;i<from.length;i++){
System.out.println(" "+from[i]);
}System.out.println("\n");
System.out.println("第二个数组中的元素");
for(int i=0;i<to.length;i++){
System.out.println(" "+to[i]);//打印出数组中的每一个元素
}
System.out.println("\n\n将第一个数组拷贝到第二个数组\n");
System.arraycopy(from,2,to,0,3);
System.out.println("拷贝完成后第二个数组中的元素");
for(int i=0;i<to.length;i++){
System.out.println(" "+to[i]);//打印出数组中的每一个元素}}}
public class StudentTest3{
public static void main(String[] args){
Student tom=new Student("Tom","20020410");
tom.setStudentSex("man");
tom.setSutdentAddress("America");
System.out.println(tom.toString());
}
}
class Student{
private String strName="";
private String strNumber="";
private String sexSex="";
private String strBirthday="";
private String strSpeciality="";
private String strAddress="";
public static void main(String[] args){
Student aStudent=new Student("沙和尚","19990000");
aStudent.setStudentAddress("通天河");
System.out.println(aStudent.toString());
}
public Student (String name,String number){
strName=name;
strNumber=number;
}
public String getStudentName(){
return strName;
}
public void setStudentSex(String sex){
strSex=sex;
}
public String getStudentSex(){
return strSex;
}
public String getStudentBirthday(){
return strBirthday;
}
public void setStudentSpciality(String speciality){
strSpeciality=speciality;
}
public String getStudentAddress(){
return strAddress;
}
public void setStudentAddress(String address) {
strAddress=address;
}
public String toString(){
String information="学生姓名="+strName+",学号="+strNumber;
if(!strSex.equals("")){
information+=",性别="+strSex;
if(!strBirthday.equals(""))
information+=", 出生年月="+strBirthday;
if(!strSpeciality.equals(""))
information+=",专业="+strSpeciality;
if(!strAddress.equals(""))
information+=",籍贯="+strAddress;
return information;
}
}
}
/*
*学生类,包括学生的基本信息
*/
public class studentTest2{
public static void main(String[] args){
int i;
for(i=0;i<10;i++) {
Student tom=new Student("Tom"+i);
if(i%2==0){
tom.setStudentSex("man");
}else{
tom.setStudentSex("female");
}
tom.setStudentAddress("America");
tom.setStudentNumber();
System.out.println(tom.toString());
}}}
class Student{
private String strName="";
private int number=0;
private String strSex="";
private String strBirthday="";
private String strSpeciality="";
private String strAddress="";
private Static int nextNumber=1;
public Student(String name){
strName=name;
}
public String getStudentName(){
return strName;
}
public int getStudentNumber(){
return number;
}
public void setStudentNumber(){
number=nextNumber;
nextNumber++;
}
public void setStudentSex(String sex){
strSex=sex;
}
public String getStudentBirthday(){
return strBirthday;
}
public void setStudentBirthday(String birthday){
strBirthday=birthday;
}
public String getStudentSpeciality(){
return strSpciality;
}
public void setStudentSpeciality(String speciality){
strSpeciality=speciality;
}
public String getStudentAddress(){
return strAddress;
}
public String toString (){
String information="student name="+strName+",student number="+number;
if(!strSex.equals(""))
information +=",sex="+strSex;
if(!strBirthday.equals(""))
information+=",birthday="+strBirthday;
if(!strSpeciality.equals(""))
information+=",专业="+strSpeciality;
if(!strAddress.equals(""))
information+=",籍贯="+strAddress;
return information;
}}
/*
*学生类,包括学生的基本信息
*/
public class StudentTest1{
public static void main(){
Student1 tom=new Student1("Tom","20020410");
Student2 jack=new Student2("jack","20030911");
System.out.println(jack.toString());
System.out.println("通过公开字段,修改实例字段值。")
//tom.strName='唐僧";
jack.strName="孙悟空";
System.out.println(jack.toString());
}}
class Student1{
private String strName="";
private String strNumber="";
public Student1(String name,String number){
strName=name;
strNumber=number;
}
public String getStudentName(){
return strName;
}
public String getStudentNumber(){
return strNumber;
}
public String toString(){
return "student="+strName+",student number="+strNumber;
} }
class Student2{
public String strName="";
public String strNuber="";
public Student2(String name,String number){
strName=name;
strNumber=number;
}
public String getStudentName(){
return strName;}
public String getStudentNumber(){
return strNumber;
}
public String toString(){
return "学生姓名="+strName+",学号="+strNumber;
}
}