@Sarah
2015-11-05T17:04:51.000000Z
字数 4701
阅读 1070
保留字
标识符
JAVA代码写完以后存到 .class 文件里
main主函数 程序执行main后面的内容,如果不想执行就加上数字
main01()
打主函数的时候,先打main,然后找快捷方式里第二个即可
1函数:方法 method
2变量:存放数据
成员变量:针对整体的
局部变量:写在代码块里的整数 int long
浮点数 float double
字符串 char
布尔 boolean
如果超出范围会变成负的“溢出”
3语句:执行
1.if
形式1
if(逻辑表达式){代码块}
形式2
if () {}else{}
形式3
if (){}else if(){}else if(){}...else{}
2.switch
switch(整形变量){case1:代码break;case2:代码break;}
1while
形式1
while (条件){}
形式2
do{}while(条件)
2for
for(初始化;循环;增量){}
eg
for(i=0;i<=100;i++)
阶乘代码
是否构成三角形代码
这两个结果是布尔值
and :&&
or: ||
这两个结果是个数
&
|
问号?如何使用
i=条件? A;B如果真执行A如果假执行B
上课所有范例
//1熟悉变量public static void main1(String[] args) {int ageOfMine = 100;int height;height = 200;long g = 123456789876543l;float f1 = 5678.567f;double f2 = 3456.5678 + 1;boolean b = 1 == 0;height = -height;System.out.println(height);}//2用两种方式判断数字是星期几public static void main2(String[] args) {int i = 4;// if (i==1){// System.out.println("Mondy");// }// else if( i ==2){// System.out.println("Tuesday");//// }switch (i) {case 1:System.out.println("monday");break;case 2:System.out.println("");break;case 3:System.out.println("Wendsday");break;// default:// System.out.println("default");//如果都不符合,输出default}}//求阶乘public static void main3(String[] args) {long k = 1;for (int i = 1; i <= 26; i++) {k = k * i;System.out.println(k);}// System.out.println(k);}//判断三边是否构成三角形public static void main4(String[] args) {int a = 3, b = 5;int c = 7;if (a + b > c && b + c > a && a + c > b) {System.out.println("YES");} else {System.out.println("NO");}}//大括号的作用public static void main5(String[] args) {int i = 67, j = 45;if (i == j) {System.out.println("1");}System.out.println("2");System.out.println("ok");}//累加到100 方法1public static void main6(String[] args) {int k = 0, i = 1;while (i < 101) {k = k + i;i++;}System.out.println(k);//方法2k = 0;i = 1;while (true) {// 这个true能让循环继续下去if (i > 100)break;elsek = k + i;i++;}System.out.println(k);}//判断成绩优良中差public static void main(String[] args) {int testscore = 76;char grade;if (testscore >= 90) {grade = '优';} else if (testscore >= 80) {grade = '良';} else if (testscore >= 70) {grade = '中';} else if (testscore >= 60) {grade = 'D';} else {grade = 'F';}System.out.println("Grade = " + grade);}//左右对称的三角形public static void main0(String[] args) {for (int i = 1; i <=10; i++) {for (int k = 10; k>i; k--) {System.out.print(" ");}for (int j = 0; j < i; j++)System.out.print("*");for (int j = 0; j < i; j++)System.out.print("*");System.out.println();}}}//打印出星号组成的左对齐三角形//[右对齐三角形public static void main(String[] args) {for (int i = 1; i <10; i++) {for (int k= 10; k> i; k--) {System.out.print(" ");}for (int j = 0; j < i; j++)System.out.print("*");System.out.println();}}}//左对齐的三角形public class main01 {public static void main(String[] args) {for (int i=0;i<=10;i++){for (int j=0;j<i;j++)System.out.print("*");//右对齐倒三角形public static void main(String[] args) {for (int i=0;i<=10;i++){for (int j=0;j<i;j++)System.out.print(" ");for (int k=10;k>i;k--){System.out.print("*");}//左对齐倒三角形public static void main(String[] args) {for (int i=0;i<=10;i++){for (int k=10;k>i;k--){System.out.print("*");}//从上往下递减=倒三角形//从上往下递增=正三角形//先有空格三角形=右对齐//直接星号=左对齐
注意大括号的位置:
如果是在大括号里面,那每循环一次就执行一次;
如果是在大括号外面,那括号里面循环结束才会执行
//判断月份class SwitchDemo {public static void main(String[] args) {int month = 8;switch (month) {case 1: System.out.println("January"); break;case 2: System.out.println("February"); break;case 3: System.out.println("March"); break;case 4: System.out.println("April"); break;case 5: System.out.println("May"); break;case 6: System.out.println("June"); break;case 7: System.out.println("July"); break;case 8: System.out.println("August"); break;case 9: System.out.println("September"); break;case 10: System.out.println("October"); break;case 11: System.out.println("November"); break;case 12: System.out.println("December"); break;default: System.out.println("Invalid month.");break;}}}
//一个月多少天class SwitchDemo2 {public static void main(String[] args) {int month = 2;int year = 2000;int numDays = 0;switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:numDays = 31;break;case 4:case 6:case 9:case 11:numDays = 30;break;case 2:if ( ((year % 4 == 0) && !(year % 100 == 0))|| (year % 400 == 0) )numDays = 29;elsenumDays = 28;break;default:System.out.println("Invalid month.");break;}System.out.println("Number of Days = " + numDays);}}
class WhileDemo {public static void main(String[] args){int count = 1;while (count < 11) {System.out.println("Count is: " + count);count++;}}}
class DoWhileDemo {public static void main(String[] args){int count = 1;do {System.out.println("Count is: " + count);count++;} while (count <= 11);}}
class ForDemo {public static void main(String[] args){for(int i=1; i<11; i++){System.out.println("Count is: " + i);}}}
class EnhancedForDemo {public static void main(String[] args){int[] numbers = {1,2,3,4,5,6,7,8,9,10};for (int item : numbers) {System.out.println("Count is: " + item);}}}
class BreakDemo {public static void main(String[] args) {int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076,2000, 8, 622, 127 };int searchfor = 12;int i;boolean foundIt = false;for (i = 0; i < arrayOfInts.length; i++) {if (arrayOfInts[i] == searchfor) {foundIt = true;break;}}if (foundIt) {System.out.println("Found " + searchfor+ " at index " + i);} else {System.out.println(searchfor+ " not in the array");}}}