HelloWorld 1.随便新建一个文件夹,存放代码
2.新建一个Java文件
文件后缀名为.java
Hello.java
【注意点】系统可能没有显示文件后缀名
3.编写代码
public class Hello { public static void main (String[] args) { System.out.print("Hello,World" ); } }
4.编译javac java文件,会生成一个class文件
5.运行class文件,java class文件
可能会遇到的情况 1.每个单词的大小写不能出现问题,Java是大小写敏感的 ;
2.尽量使用英文;
3.文件名和类名必须保持一直,并且首字母大写
4.符号使用了中文的
Scanner的使用 package scanner;import java.util.Scanner;public class Demo01 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("使用next方式接收:" ); if (scanner.hasNext()){ String str = scanner.next(); System.out.println("输出的内容为:" + str); } scanner.close(); } }
package scanner;import java.util.Scanner;public class Demo2 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("使用nextLine方式接受:" ); if (scanner.hasNext()){ String str = scanner.nextLine(); System.out.println("输出内容为:" +str); } } }
package scanner;import java.util.Scanner;public class Demo03 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入数据:" ); String str = scanner.nextLine(); System.out.println("输出的内容为:" +str); scanner.close(); } }
package scanner;import java.util.Scanner;public class Demo04 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); int i = 0 ; float f = 0.0f ; System.out.println("请输入整数:" ); if (scanner.hasNextInt()){ i = scanner.nextInt(); System.out.println("输入的数字为:" +i); } else { System.out.println("输入的不是整数" ); } System.out.println("请输入小数:" ); if (scanner.hasNextFloat()){ f = scanner.nextFloat(); System.out.println("输入的小数为:" +f); } else { System.out.println("输入的不是浮点数" ); } scanner.close(); } }
package scanner;import java.util.Scanner;public class Demo05 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); double sum = 0 ; int m = 0 ; try { while (scanner.hasNext()) { double x = scanner.nextDouble(); sum += x; m++; System.out.println("你输入了第" + m + "个数据,当前结果sum=" + sum); } }catch (Exception e) { System.out.println(m + "个数的和为" + sum); System.out.println(m + "个数的平均值是" + (sum / m)); scanner.close(); } } }
if结构 package struct;import java.util.Scanner;public class ifDemo1 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); String s = scanner.nextLine(); if (s.equals("Hello" )){ System.out.println(s); } System.out.println("End" ); scanner.close(); } }
package struct;import java.util.Scanner;public class ifDemo2 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入成绩:" ); int score = scanner.nextInt(); if (score>60 ){ System.out.println("及格" ); }else { System.out.println("不及格" ); } } }
switch结构 package struct;public class switchDemo1 { public static void main (String[] args) { char grade = 'C' ; switch (grade) { case 'A' : System.out.println("优秀" ); break ; case 'B' : System.out.println("良好" ); break ; case 'C' : System.out.println("及格" ); break ; case 'D' : System.out.println("再接再厉" ); break ; case 'E' : System.out.println("挂科" ); break ; } } }
package struct;public class switchDemo02 { public static void main (String[] args) { String name = "秦疆" ; switch (name) { case "秦疆" : System.out.println("秦疆" ); break ; case "狂神" : System.out.println("狂神" ); break ; default : System.out.println("弄啥嘞" ); } } }
whlie循环 package struct;public class whileDemo1 { public static void main (String[] args) { int i = 0 ; while (i<100 ){ i++; System.out.println(i); } } }
package struct;public class whileDemo2 { public static void main (String[] args) { int i = 0 ; int sum = 0 ; while (i<=100 ){ sum = sum + i; i++; } System.out.println(sum); } }
for循环 package struct;public class forDemo1 { public static void main (String[] args) { int sum1 = 0 ; int sum2 = 0 ; for (int i=0 ;i<=100 ;i++){ if (i%2 !=0 ){ sum1+=i; }else { sum2+=i; } } System.out.println("奇数和为:" +sum1); System.out.println("偶数和为:" +sum2); } }
package struct;public class forDemo2 { public static void main (String[] args) { for (int i=1 ;i<=1000 ;i++){ if (i%5 ==0 ){ System.out.print(i+"\t" ); } if (i%(5 *3 )==0 ){ System.out.println(); } } } }
package struct;public class forDemo3 { public static void main (String[] args) { for (int i=1 ;i<=9 ;i++){ for (int j=1 ;j<=i;j++){ System.out.print(i+"*" +j+"=" +i*j+"\t" ); } System.out.println(); } } }
package struct;public class forDemo4 { public static void main (String[] args) { int [] numbers = {10 ,20 ,30 ,40 ,50 ,60 ,70 ,80 ,90 }; for (int x:numbers){ System.out.println(x); } } }
标签 package struct;public class LabelDemo { public static void main (String[] args) { int count = 0 ; outer:for (int i=101 ;i<150 ;i++){ for (int j=2 ;j<i/2 ;j++){ if (i%j==0 ){ continue outer; } } System.out.println(i+" " ); } } }
package struct;public class test1 { public static void main (String[] args) { for (int i = 1 ;i<=5 ;i++){ for (int j=5 ;j>i;j--){ System.out.print(" " ); } for (int j=1 ;j<=i;j++){ System.out.print("*" ); } for (int j=1 ;j<i;j++){ System.out.print("*" ); } System.out.println(); } } }
方法 package method;public class Demo1 { public static void main (String[] args) { int sum = add(1 ,2 ); System.out.println(sum); } public static int add (int a, int b) { return a + b; } }
package method;public class Demo2 { public static void main (String[] args) { int max = max(10 ,20 ); System.out.println(max); } public static int max (int a, int b) { int result = 0 ; if (a==b){ System.out.println("a==b" ); return 0 ; } if (a>b){ result = a; }else { result = b; } return result; } }
方法的重载 定义 重载就是在一个类中,有相同的函数名称,但形参不同的函数
规则
方法名称必须相同
参数列表必须不同(个数不同、或类型不同、参数排列顺序不同等)
方法的返回类型可用相同也可以不相同
仅仅返回类型不同不足以成为方法的重载
实现理论 方法名称相同时,编译器会根据调用方法的参数个数、参数类型等去逐个匹配,以选择对应的方法,如果匹配失败,则编译器报错
可变参数
JDK1.5开始,Java支持传递同类型的可变参数给一个方法
在方法声明中,在知道参数类型后加一个省略号(…)
一个方法中只能指定一个可变参数,它必须是方法的最后一个参数。任何普通的参数必须在它之前声明
package method;public class Demo3 { public static void main (String[] args) { printMax(32 ,43 ,67 ,36 ,26 ); printMax(new double []{1 ,2 ,3 ,4 ,5 }); } public static void printMax (double ... numbers) { if (numbers == null || numbers.length == 0 ) { return ; } double max = numbers[0 ]; for (int i = 1 ; i < numbers.length; i++) { if (max < numbers[i]) { max = numbers[i]; } } System.out.println(max); } }
递归 package method;public class Demo4 { public static void main (String[] args) { System.out.println(multiple(3 )); } public static int multiple (int a) { if (a ==1 ){ return 1 ; } return a*multiple(a-1 ); } }
数组 package array;public class Demo1 { public static void main (String[] args) { int [] nums; nums = new int [10 ]; for (int i = 0 ; i < nums.length; i++){ nums[i] = i; } int sum=0 ; for (int i = 0 ; i < nums.length; i++){ sum += nums[i]; } System.out.println("sum = " + sum); } }
package array;import java.sql.SQLOutput;public class Demo2 { public static void main (String[] args) { int [][] array={{1 ,2 },{3 ,4 },{5 ,6 }}; System.out.println(array[0 ][0 ])}; } }