位运算
package com.lqs.operator;
public class Demo06 {
public static void main(String[] args){
/*
位运算
A = 0011 1100
B = 0000 1101
A&B = 0000 1100 与
A|B = 0011 1101 或
A^B = 0011 0001 异或
~A = 1100 0011 取反
面试题:
2*8 = 16 怎么样计算最快
2*2*2*2
效率极高!!
<< *2
>> /2
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(3<<3);
}
}
总结:面试题2*8怎么样计算最快,把8分为3个2。
3左移3个就是3*2^3
字符串连接符
package com.lqs.operator;
public class Demo07 {
public static void main(String[] args){
int a =10;
int b = 20;
a+=b; //a=a+b;
a-=b; //a=a-b;
System.out.println(a);
//面试题
//字符串连接符 +,String
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
总结:a=a+b(a+=b) a=a-b(a-=b)
(" “+a+b)与(a+b+”")结果不同
三元运算符
package com.lqs.operator;
//三元运算符
public class Demo08 {
public static void main(String[] args){
//x?y:z
//如果x==true,则结果为y,否则为z
int age =20;
String result= age>=18?"成年":"未成年";
System.out.println(result);
}
}
总结:用三元运算符可以代替if判断。
以next方式接收
package com.lqs.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);
}
//凡是属于IO流的类
//在使用完毕后,必须调用close方法,释放资源
scanner.close();
}
}
总结:以next方式接收 可以接收到空格之前的数据。
以nextLine方式接收
package com.lqs.scanner;
import java.util.Scanner;
public class Demo02{
public static void main(String[] args) {
//从键盘接受数据
Scanner scanner= new Scanner(System.in);
System.out.println("请使用nextLine方式接收");
//判断是否还有输入
if(scanner.hasNextLine()){
String s= scanner.nextLine();
System.out.println("输入的数据为:"+s);
}
scanner.close();
}
}
总结:以nextLine方式接收,可以包括空格前后的内容。
输入几个数,求出平均数和总和
package com.lqs.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;
System.out.println("请输入数据");
for (m = 0; scanner.hasNextDouble(); m++){
double x= scanner.nextDouble();
sum=sum+x;
}
System.out.println(m+"个数的平均数是:"+sum/m);
System.out.println(m+"个数的总和为"+sum);
scanner.close();
}
}
总结:利用scanner.hasNextDouble()来判断输入的数若是数字则循环继续,若输入的数不是数字则跳出循环。
break
package com.lqs.struct;
public class BreakDemo01 {
public static void main(String[] args) {
int i =0;
while(i<100){
i++;
System.out.println(i);
if(i==10){
break;//当i等于10时,跳出循环
}
}
}}
总结:当i==10跳出循环。
continue
package com.lqs.struct;
public class ContinueDemo01 {
public static void main(String[] args) {
int i =0;
while(i<100){
i++;
if(i%10==0){
System.out.println();
continue;
}
System.out.print(i+"\t");
}
//break在任何循环语句的主体部分,均可用break控制循环的流程。
//break语句用来退出循环,所以它后面的代码块将不会被执行。
//continue语句用来跳过循环体中剩余的代码,然后继续下一次循环。
}
}
总结:continue:跳出本次判断语句,继续执行剩下的语句。
dowhile
package com.lqs.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i =0;
int sum=0;
do {
sum+=i;
i++;
}while(i<=100);
System.out.println(sum);
}
}
总结:计算1~100的总和。
while和dowhile的区别
package com.lqs.struct;
public class DowhileDemo02 {
//while循环和do...while循环的区别
//while循环先判断后执行,do...while循环先执行后判断
public static void main(String[] args) {
int a =0;
while (a<0){
System.out.println(a);
a++;
}
System.out.println("================");
do {
System.out.println(a);
a++;
}while (a<0);
}
}
总结:while循环先判断后执行,do…while循环先执行后判断。
for循环
package com.lqs.struct;
public class ForDemo01 {
public static void main(String[] args) {
//打印出1-100中的奇数
int a =1;
while (a<=100){
System.out.println(a);
a+=2;
}
System.out.println("----------------");
//100.for 快捷键
for (int i = 1; i <= 100; i+=2){
System.out.println(i);
}
}
}
总结: 打印出1-100中的奇数。
打印九九乘法表
package com.lqs.struct;
//打印九九乘法表
public class ForDemo04 {
public static void main(String[] args) {
for (int i=1;i<10;i++ ){
System.out.println();
for (int j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+i*j +"\t");
}
}
}
}
总结:外层代表行,内层代表列。
增强for循环
package com.lqs.struct;
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers={10,20,30,40,50};
for (int x :numbers){
System.out.println(x);
}
}
}
总结:用增强for循环来遍历数组。
if判断
package com.lqs.struct;
import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入内容:");
String s= scanner.nextLine();
//equals 判断字符串是否相等
if(s.equals("hello")){
System.out.println(s);
}
System.out.println("end");
}
}
总结:当判断字符串时要用equals。
if ,else if , else
package com.lqs.struct;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的分数");
Double score = scanner.nextDouble();
if (score == 100) {
System.out.println("成绩满分");
} else if (score >= 90 && score < 100) {
System.out.println("成绩优秀");
} else if (score >= 60 && score < 90) {
System.out.println("成绩合格");
} else if (score >= 0 && score < 60)
System.out.println("成绩不合格");
else {
System.out.println("输入有误");
}
}
}
总结:利用if判断来给成绩打分。
swith
package com.lqs.struct;
public class SwithDemo01 {
public static void main(String[] args) {
//jdk7之前表达式可以是字符串
String str = "a";
switch (str){
case "a":
System.out.println("a");
break;
case "b":
System.out.println("b");
break;
case "c":
System.out.println("c");
break;
case "d":
System.out.println("d");
break;
case "e":
System.out.println("e");
break;
}
}
}
总结:可以用swith来进行选择判断。
定义一个方法,用于计算两个数的和
package com.lqs.method;
public class Demo01 {
// 定义一个方法,用于计算两个数的和
public static void main(String[] args){
Demo01 d = new Demo01();
//实际参数:实际调用传递给他的参数
int a= d.add(3,4);
System.out.println(a);
}
//形式参数,用来定义作用
public int add(int a, int b){
return a+b;
}
public int add(int a, int b,int c){
return a+b;
}
public double add(double a, int b){
return (double)a+b;
}
}
总结:定义一个方法,在主函数中进行调用传参。
方法的重载
package com.lqs.method;
//面试题
//方法的重载,方法名相同,参数的列表(参数的类型,参数个数)不同,与返回值无关
public class Demo02 {
public static void main(String[] args) {
double i= max(10,20);
System.out.println(i);
}
public static int max(int a,int b){
if (a>b){
return a ;
}else if(a<b){
return b;
}else {
System.out.println("a=b");
return 0; //终止方法
}
}
public static double max(double a,double b){
if (a>b){
return a ;
}else if(a<b){
return b;
}else {
System.out.println("a=b");
return 0; //终止方法
}
}
public static double max(double b,double a,double c){
if (a>b){
return a ;
}else if(a<b){
return b;
}else {
System.out.println("a=b");
return 0; //终止方法
}
}
}
总结:方法的重载:方法名相同,方法的类型、个数不同。