java编程入门一
1.helloword
1.1 println
1 public class helloWorld { 2 public static void main (String[] args){ 3 System.out.println("hello world tom"); 4 } 5 }
1.2 myOneJava.java
public class myOneJava{ public static void main(String[] args){ System.out.println("Today is sunday"); } }
2.三元运算符
2.1 三个和尚
1 public class heshang{ 2 public static void main (String[] args){ 3 int height1 = 150; 4 int height2 = 210; 5 int height3 = 165; 6 7 int temHeight = (height1>height2) ? height1 :height2; 8 int max = (temHeight>height3) ? temHeight : height3; 9 10 System.out.println("maxHeight:" + max); 11 } 12 }
2.2 两只老虎
public class twoTiger{ public static void main (String[] args){ int weight1 = 180; int weight2 = 200; // a>b?a:b; boolean b = (weight1 == weight2)? true:false; System.out.println("b:" + b); } }
3. 数据输入
3.1 数据输入
import java.util.Scanner; public class inputDate{ public static void main (String[] args){ System.out.println("Input what?please:"); Scanner sc = new Scanner(System.in); int i = sc.nextInt(); System.out.println("output : " + i); } }
3.2三个和尚升级版
1 import java.util.Scanner; 2 3 public class heShangMax{ 4 public static void main (String[] args){ 5 Scanner sc = new Scanner(System.in); 6 7 System.out.println("please input 1 heshang:"); 8 int height1 = sc.nextInt(); 9 System.out.println("please input 2 heshang:"); 10 int height2 = sc.nextInt(); 11 System.out.println("please input 3 heshang:"); 12 int height3 = sc.nextInt(); 13 14 int temHeight = (height1 > height2)?height1:height2; 15 int maxHeight = (temHeight > height3)?temHeight:height3; 16 17 System.out.println("max height is : "+maxHeight); 18 } 19 }
4.IF语句
4.1 if
public class ifLearn{ public static void main(String[] args){ int a = 10; int b = 20; if (a > b){ System.out.println("a>b"); } else if(a < b){ System.out.println("a < b"); } else{ System.out.println("a = b"); } } }
4.2 奇偶数
import java.util.Scanner; public class jioushu{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("please input a number:"); int number = sc.nextInt(); if (number%2 == 0){ System.out.println(number + " is jishu"); }else { System.out.println(number + " is oushu"); } } }
4.3 考试奖励
import java.util.Scanner; public class kaoshijiangli{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("input you score: "); int score = sc.nextInt(); if (score >=80 && score <= 100){ System.out.println("very good!"); }else if (score >=60 && score <80){ System.out.println("just so so"); }else if (score >=0 && score < 60){ System.out.println("son of bitch !"); }else{ System.out.println("Input score in \"0-100\" please !"); } } }
5. Switch
5.1 Demo
import java.util.Scanner; public class SwitchDemo{ public static void main(String[] args){ System.out.println("请输入个数字吧:"); Scanner sc = new Scanner(System.in); int week = sc.nextInt(); switch (week){ case 1: System.out.println("星期一"); break; case 2: System.out.println("星期二"); break; case 3: System.out.println("星期三"); break; case 4: System.out.println("星期四"); break; case 5: System.out.println("星期五"); break; case 6: System.out.println("星期六"); break; case 7: System.out.println("星期日"); break; default: System.out.println("请输入数字1-7之间"); } } }
5.2 四季
import java.util.Scanner; public class SiJi{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("请输入一个月份:1-12之间"); int month = sc.nextInt(); switch (month){ case 3,4,5: System.out.println("春暖花开"); break; case 6,7,8: System.out.println("夏果累累"); break; case 9,10,11: System.out.println("秋风萧瑟"); break; case 12,1,2: System.out.println("冬雪皑皑"); break; default: System.out.println("请输入月份在1-12之间"); } } }
6. For 循环
6.1 Demo
/* 格式: for (初始化语句;条件判断语句;条件控制语句){ 循环体语句; } 执行流程1.执行初始化语句2. 执行条件判断语句3.执行循环体语句。4.执行条件控制语句 5.回到二继续执行 */ public class ForDemo{ public static void main(String[] args){ for (int i=0;i<5;i++){ System.out.println("HELLOWORD"); } for (int n=1;n<=5;n++){ System.out.println(n); } for (int i=5;i>=1;i--){ System.out.println(i); } } }
6.2 求和
public class ForSum{ public static void main(String[] args){ int sum =0; int sum1=0; for(int i=1;i<=100;i++){ sum += i; } System.out.println(sum); System.out.println("-----------(づ ̄3 ̄)づ╭❤~求偶数和--------------"); for (int j=1;j<=100;j++){ if (j%2==0){ sum1 +=j; } }System.out.println(sum1); } }
6.3 水仙花与计数
//在控制台输出水仙花数 public class shuixianhuashu{ public static void main(String[] args){ int count = 0; for (int i=100;i<=999;i++){ //在计算前获取三位数中每个位上的值 int ge =i%10; int shi = i/10%10; int bai = i/100%10; //判定条件是将三位数中的每个数值取出来,计算立方和后与原数相同 if(ge*ge*ge+shi*shi*shi+bai*bai*bai ==i){ //计数count count++; System.out.println(i); } }System.out.println("水仙花数"+count+"个"); } }
7. while 循环
7.1 Demo
public class whileDemo{ public static void main(String[] args){ //在控制台输出10次helloword int i = 0; while (i<=10){ System.out.println("helloword"); i++; } } }
7.2 折纸珠穆朗玛峰
public class zhumulangmafeng{ public static void main(String[] args){ double paper = 0.1; int zf = 8844430; int count = 0; while (paper <= zf){ paper *=2; count++; } System.out.println("一张纸需要折叠"+count+"次,才等于珠穆朗玛峰高度"); } }
7.3 do..while
/* 完整格式 do { 循环体语句; 条件控制语句; }while(条件判断语句); */ public class doWhileDemo{ public static void main(String[] args){ int i = 0; do{ System.out.println(i); i++; }while (i<=5); } }
7.4 continue & break 循环控制
public class conBreak{ public static void main(String[] args){ for (int i=1;i<=5;i++){ if(i%2 == 0){ //continue; break; } System.out.println(i); } } }
8. Random
8.1 Demo
import java.util.Random; public class randomDemo{ public static void main(String[] args){ Random r = new Random(); int number = r.nextInt(10); System.out.println(number); } }
8.2 猜数字
import java.util.Random; import java.util.Scanner; public class caishuzi{ public static void main(String[] args){ Random r = new Random(); int number = r.nextInt(100)+1; while (true){ Scanner sc = new Scanner(System.in); int InNumber = sc.nextInt(); if (InNumber > number){ System.out.println("大了"); }else if (InNumber < number){ System.out.println("小了"); }else{ System.out.println("猜对了,正是:"+InNumber); break; } } } }
123
为美好的生活奋斗!