Java 学习笔记
1. Java中变量名
变量名在实际应用中通常是以字母开头并且小写是名词。其后面的单词首字母大写。
2. Java中的运算符(++和–)
算数运算符:=、-、*、/、%
递增递减运算符:++、–、
赋值运算符:=、+=、-=、*=、/=\
关系运算符:>、>=、<、<=、==、!=
逻辑运算符:&&、||、^(异或运算)
三目运算符:表达式1:?:表达式2
位运算符:&、|、^、~
移位运算符:>>、<<、>>>
“`java
package com.day2;
public class test {
public static void main(String[] args) {
int i=9;
int j;
i++;
System.out.println(i);//i=10
++i;
System.out.println(i);//i=11
j=i++; //先使用后++
System.out.println(j);//j=11,i=12
j=++i; //先++后使用
System.out.println(j);//i=13,j=13
}
}
“`
3. Java中的基本数据类型
byte默认值:0
short默认值:0
int默认值:0
long默认值:0L
float默认值:0.0f
double默认值:0.0
char默认值:” “(空格)
bollean默认值:false
引用数据类型默认值为:null
4. Java中的整形数据和浮点数的取模运算
浮点数://a%b=a-(b*q),q=int(a/b).
整 数://a%b=a-(a/b)*b.
5. Java中的循环
- if-else循环
import java.util.Scanner;
public class test {
public static void main(String[] args) {
int score;
while(true){
Scanner scan = new Scanner(System.in);
score = scan.nextInt();
if (score>90)
System.out.println("恭喜你,考试优秀!");
else if (score>80)
System.out.println("祝贺你,考试良好,继续努力");
else if(score>70)
System.out.println("考试中等,请继续努力");
else if(score>60)
System.out.println("差一点挂科,去买彩票吧。");
else
System.out.println("你挂科了,假期在家好好学习,开学来补考。");
}
}
}
测试结果如下:
- switch循环
java
import java.util.Scanner;
public class test {
public static void main(String[] args) {
int score;
while(true){
Scanner scan = new Scanner(System.in);
score = scan.nextInt();
/*if (score>90)
System.out.println("恭喜你,考试优秀!");
if(score==100)
System.out.println("考试一百分,非常好,鼓掌鼓掌");
else if (score>80)
System.out.println("祝贺你,考试良好,继续努力");
else if(score>70)
System.out.println("考试中等,请继续努力");
else if(score>60)
System.out.println("差一点挂科,去买彩票吧。");
else
System.out.println("你挂科了,假期在家好好学习,开学来补考。");*/
switch(score/10){
case 10:
System.out.println("考试满分,抄的吧??");
break;
case 9:
System.out.println("考试优秀,再接再厉");
break;
case 8:
System.out.println("考试良好,下学习继续努力");
break;
case 7:
System.out.println("考试中等,成绩不是很好");
break;
case 6:
System.out.println("差一点你就要挂科了,小心一点哦");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
System.out.println("这学期玩的太嗨了,考试挂科了。柯南啊,下学习准备补考吧");
break;
}
}
}
}
测试结果如下:
结构图如下:
6.分别用三种循环结构,输入任意整数,输出1加到该整数的和。
package com.day2;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
int result = 0;
for(int i=0;i<=number;i++){
result+=i;
}
System.out.println("for循环的和为:"+result);
int j=0;
int result1=0;
while( j<=number){
result1+=j;
j++;
}
System.out.println("while循环的和为:"+result1);
int k=0;
int result2=0;
do{
result2+=k;
k++;
}
while(k<=number);
System.out.println("while循环的和为:"+result2);
}
}
程序运行结果如下:
7.求出100-1000之间的水仙花数子
代码如下:
package com.day2;
public class test3 {
public static void main(String[] args) {
for(int j=100;j<1000;j++){
int a=j/100;
int b=j/10%10;
int c=j%10;
if(a*a*a+b*b*b+c*c*c==j){
System.out.println("水仙花数是:"+j);
}
}
System.out.println("--------------------");
for(int a=1;a<10;a++){
for(int b=0;b<10;b++){
for(int c=0;c<10;c++){
if(a*a*a+b*b*b+c*c*c==a*100+b*10+c){
System.out.println("水仙花数是:"+a+b+c);
}
}
}
}
}
}
程序运行结果如下:
8.分别用三种循环结构,输入任意整数,输出1加到该整数的和。
package com.day2;
import java.util.Scanner;
public class test4 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
/*int number = scan.nextInt();
int count=0;
for(int i=1;i<=32;i++){
if((number&1)==1){
count++;
}
number>>=1;
}
System.out.println("二进制表示时1的个数为:"+count);*/
int number1=scan.nextInt();
int count=0;
while(number1!=0){
 if(number1%2==1){
count++;
}
// number1>>=1;
number1/=2;
}
System.out.println("二进制表示时1的个数为:"+count);
}
}
程序运行结果是: