Helloworld
package HelloWorld; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello World"); } }
int类型的1+1
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner Ind = new Scanner(System.in); int a=Ind.nextInt(); int b=Ind.nextInt(); System.out.println(a+b); } }
数组的定义与使用
Scanner Ind = new Scanner(System.in); int[] a=new int[10];///一维数组
///int[][] a = new int[20][20] 二维数组的定义 a[0]=Ind.nextInt(); a[1]=Ind.nextInt(); System.out.println(a[0]+a[1]);
package BuyCar; public class Tcar {///建造一个汽车类 并且进行调用 public static void main(String[] args) { Car c1 = new Car(); c1.info(); c1.info("baoma",4); System.out.println(c1.getName()); c1.info(); Factory f=new Factory(); Car c2=f.newCar(); c2.info(); Car c3=f.newCar("aodi", 6); c3.info(); f.chakan(c3); } } class Factory{ public Car newCar() { return new Car(); } public Car newCar(String s,int a) { Car ans=new Car(); ans.name=s; ans.wheel=a; return ans; } public void chakan(Car a) { a.info(); } } class Car { String name;///属性 int wheel; public void info() {///方法 System.out.println("name:"+name+",wheel:"+wheel); } public void info(String str,int a) { name=str;wheel=a; } public String getName() { return name; } } 输出:
name:null,wheel:0
baoma
name:baoma,wheel:4
name:null,wheel:0
name:aodi,wheel:6
name:aodi,wheel:6
package AddSum;///JDK新特性之可变个数的形参的方法 public class Add { public static void main(String[] args) { // TODO Auto-generated method stub Add sum=new Add(); System.out.println(sum.getSum(1,2,3,4)); System.out.println(sum.getStr("qwe","asd","zxcc")); } public int getSum(int ... args){ int sum=0; for(int i=0;i<args.length;i++) sum+=args[i]; return sum; } public String getStr(String ... s) { String str=new String();for(int i=0;i<s.length;i++) str=str+s[i]; return str; } }
10
qweasdzxcc