java 基础

来源:互联网 发布:梅林传奇 滚娘 知乎 编辑:程序博客网 时间:2024/05/16 12:11

java中声明数组 只有4种方法int[] a = new int[4];int b[] = new int[4];int[] c = {1, 15, 26};int d[] = {1, 15, 26};


public class exer1 {public static void main(String[] args) {boolean b = false;if (b = false) // 如果写成if(b=false)能编译通过吗?如果能,结果是?System.out.println("a");else if (b)System.out.println("b");else if (!b)System.out.println("c");elseSystem.out.println("d");}}可以通过, 在if里执行了一次赋值操作,然后对其结果进行判断


/* * 从1循环到150并在每行打印一个值, * 另外在每个3的倍数行上打印出“foo”, * 在每个5的倍数行上打印“biz”, * 在每个7的倍数行上打印输出“baz” */public class FooBizBaz {public static void main(String[] args) {for(int i = 1;i<=150;i++){System.out.print(i);if(i % 3 == 0){System.out.print("foo");}if(i % 5 == 0){System.out.print("biz");}if(i % 7 == 0){System.out.print("baz");}System.out.println();}}}用% 可以很好的判断 是不是某个数的倍数


public class OrderStruc {int num1 = 12;int num2 = num1  + 2;public static void main(String[] args){OrderStruc os = new OrderStruc();System.out.println(os.num2);os.num3 = 100;System.out.println(os.num3);}}本类对象可以直接访问 成员变量 ,并且进行赋值等操作


//1—100之间的所有质数public class PrimeNumber {  //2   3      5   6  ....public static void main(String[] args) {boolean flag = false;long start = System.currentTimeMillis();for(int i = 2;i <= 100000;i++){for(int j = 2;j < Math.sqrt(i);j++){if(i % j == 0){flag = true;     // m = j * kbreak;}}if(flag == false){System.out.println(i);}flag = false;}long end = System.currentTimeMillis();System.out.println("所花费的时间:" + (end - start) +"秒");//28143(未加break)         //2600(加break) //139(使用Math.sqrt())}}

//分支结构1:If —else if - ...-elsepublic class TestIfElse {public static void main(String[] args) {int age = 17;if (age > 18){System.out.println("你已经成年了!");}System.out.println(age);System.out.println("*********");//三元元素符:()? ### : ###//1.':'前后的语句的数据类型必须一致//2.三元一定要有一个返回结果//3.关注:三元运算符和if-else的转化int a = 34;int b = 34;int max = (a > b)? a:b;System.out.println(max);String strMax = (a>b)? "a大" : "b大";System.out.println(strMax);//if-elseint max1 = 0;if(a>b){max1 = a;}else{max1 = b;}System.out.println(max1);//if-else if -...-elseif(a > b)System.out.println("a大");else if(a < b)System.out.println("b大");elseSystem.out.println("a和b相等");}}

/* * 从键盘输入小明的期末成绩。 当成绩为100分时,奖励一辆BMW; 当成绩为(80,99]时,奖励一个台iphone5s; 当成绩为[60,80]时,奖励一本参考书; 其它时,什么奖励也没有 *//* * 当诸个条件之间是完全互斥关系的话:那么if 和多个else if 是可以调换顺序的。 * 当诸个条件之间不是完全互斥关系的话:必须将条件范围小的写在范围大的上面! */public class TestIfElse1 {public static void main(String[] args) {System.out.println("从键盘输入小明的成绩:");Scanner s = new Scanner(System.in);int score = s.nextInt();// double score1 = s.nextDouble();if (score < 0 || score > 100) {System.out.println("输入的成绩不合法!!请重新输入");} else {if (score == 100) {System.out.println("奖励一辆BMW");} else if (score > 80) {System.out.println("奖励一个台iphone5s");} else if (score >= 60) {System.out.println("奖励一本参考书");} else {System.out.println("补考!!");}}}}

//九九乘法表public class TestMultiply {public static void main(String[] args) {//外层循环控制行数for(int i = 1;i < 10;i++){//内层循环控制列数for(int j = 1;j <= i;j++){System.out.print(i + "*" + j + "=" + i*j + "\t");}System.out.println();}}}