基本数据类型与运算符
-
java中的运算符
算术运算符
- +,-,*,/,%
- " /" 在进行整数运算时,表示整除.当两个才作数否为整数时,则为整数。当其中一个操作数是小数时,结果为小数。
- "%"通常对正整数取余数,事实上,对小数和负数一样可以取余数。
比较运算符
- <,>,>=,<=,==,!=,结果为boolean类型的值。
逻辑运算符
- 与 &&、或 ||、非 !,结果为boolean类型的值。
赋值运算符
- = ,复合赋值运算符+=,-=,/=,%=,自增++,自减–
- 自加自减有前缀表示法和后缀表示法,前缀式先运算后取值,后缀式先取值后运算。b = ++a,b = a+1;b = a++,b = a;
条件运算符:a?b:c,a是条件判断表达式,b和c是连个分支的值,当条件a成立,取
b的值,否则取c的值。其他运算符:位运算符>>, << 等,左移相当于乘以2,右移相当于除以2。
顺序,分支,循环
- 分支结构
- if语句,if…else语句
- switch语句使用场景受限,只能判断相等的情况。可以用if…else来实现。
课后作业
1、输入一个年份判断是否为闰年,不得使用逻辑运算符。闰年:能被400整除或者能被4整除但不能被100整除*
package javaproject0311;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入一个整数");
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
if (year % 4 == 0) {
if (year % 100 != 0) {
System.out.println("该年是闰年");
} else {
System.out.println("该年不是闰年");
}
} else if (year % 400 == 0) {
System.out.println("该年是闰年");
} else {
System.out.println("该年不是闰年");
}
scanner.close();
}
}
-
"Resource leak: ‘scanner’ is never closed"警告,在Scanner语句出现解决办法
- main函数的最后加个 scanner.close();“
- 资源泄漏:“扫描器”永远不会关闭”。其实就是你申请了一个输入流system.in在你用完这个后没有将其关闭,所以会警告。
-
Scanner:是jdk自带的一个类,可以用来接收用户输入数据。
添加方法:- 在类中写“import java.util.Scanner;”
- 如果用eclipse,可以用快捷键“Ctrl+shift+O”自动导入import所需类
2、将所有包含3的三位数整数相加求和,103,131,133等。只允许使用while循环,不使用其他关键字形成循环。
package javaproject0311;
public class Test2 {
```java
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 100;
int n1, n2, n3;
int sum = 0;
while (a < 1000) {
a++;
n1 = a / 100;
n2 = (a - n1 * 100) / 10;
n3 = a - n1 * 100 - n2 * 10;
if (n1 == 3 || n2 == 3 || n3 == 3) {
sum += a;
//System.out.println(sum);
}
}
System.out.println(sum);
}
3、输出数列1 1 2 3 5 8 13 21 34 55 第25个值,只允许使用while循环,不能使用其他关键字形成循环。
package javaproject0311;
/*输出数列1 1 2 3 5 8 13 21 34 55 第25个值。*/
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
int n1 = 1, n2 = 1;
// System.out.println(n1+","+n2);
while (i < 12) {
i++;
n1 += n2;
n2 += n1;
// System.out.println(n1+","+n2);
}
System.out.println(n1);
}
}
- 输出第100项时:
package javaproject0311;
import java.math.BigInteger;
public class Test4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
BigInteger n1 = new BigInteger("1");
BigInteger n2 = new BigInteger("1");
System.out.println(n1+","+n2);
while (i < 49) {
i++;
n1 = n1.add(n2);
n2 = n2.add(n1);
System.out.println(n1+","+n2);
}
//System.out.println(n1);
}
}
-
以下内容来自CSDN博主命z
-
java中可以使用BigInteger操作大整数,也可以转换进制。如果在操作的时候一个整型数据已经超过了整数的最大类型长度long的话,则此数据就无法装入,所以,此时要使用BigInteger类进行操作。这些大数都会以字符串的形式传入。
-
BigInteger类常用方法
- BigInteger abs() 返回大整数的绝对值
- BigInteger add(BigInteger val) 返回两个大整数的和
- BigInteger and(BigInteger val) 返回两个大整数的按位与的结果
- BigInteger andNot(BigInteger val) 返回两个大整数与非的结果
- BigInteger divide(BigInteger val) 返回两个大整数的商
- double doubleValue() 返回大整数的double类型的值
- float floatValue() 返回大整数的float类型的值
- BigInteger gcd(BigInteger val) 返回大整数的最大公约数
- int intValue() 返回大整数的整型值
- long longValue() 返回大整数的long型值
- BigInteger max(BigInteger val) 返回两个大整数的最大者
- BigInteger min(BigInteger val) 返回两个大整数的最小者
- BigInteger mod(BigInteger val) 用当前大整数对val求模
- BigInteger multiply(BigInteger val) 返回两个大整数的积
- BigInteger negate() 返回当前大整数的相反数
- BigInteger not() 返回当前大整数的非
- BigInteger or(BigInteger val) 返回两个大整数的按位或
- BigInteger pow(int exponent) 返回当前大整数的exponent次方
- BigInteger remainder(BigInteger val) 返回当前大整数除以val的余数
- BigInteger leftShift(int n) 将当前大整数左移n位后返回
- BigInteger rightShift(int n) 将当前大整数右移n位后返回
- BigInteger subtract(BigInteger val)返回两个大整数相减的结果
- byte[] toByteArray(BigInteger val)将大整数转换成二进制反码保存在byte数组中
- String toString() 将当前大整数转换成十进制的字符串形式
- BigInteger xor(BigInteger val) 返回两个大整数的异或
-
利用BigInteger构造方法转换进制
String string1 = new BigInteger("20", 10).toString(2);
Log.d("TAG","十进制的20转换成二进制是:"+string1);
String string2 = new BigInteger("20", 10).toString(8);
Log.d("TAG","十进制的20转换成八进制是:"+string2);
String string3 = new BigInteger("20", 10).toString(16);
Log.d("TAG","十进制的20转换成十六进制是:"+string3);
String string4 = new BigInteger("110", 2).toString(10);
Log.d("TAG","二进制的110转换成十进制是:"+string4);
String string5 = new BigInteger("110", 8).toString(10);
Log.d("TAG","八进制的110转换成十进制是:"+string5);
String string6 = new BigInteger("110", 16).toString(10);
Log.d("TAG","十六进制的110转换成十进制是:"+string6);
`
- 结果:
十进制的20转换成二进制是:10100
十进制的20转换成八进制是:24
十进制的20转换成十六进制是:14
二进制的110转换成十进制是:6
八进制的110转换成十进制是:72
十六进制的110转换成十进制是:272
4、输出所有3位数的水仙花数,水仙花数特点:百位数m³ + 十位数m³ + 个位数m³ = 此数本身
package javaproject0311;
public class Test4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 100;
while (i < 1000) {
int a, b, c;
a = i / 100;
b = (i - a * 100) / 10;
c = i - a * 100 - b * 10;
if (i == a * a * a + b * b * b + c * c * c) {
System.out.println(i);
}
i++;
}
}
}
5、从控制台输入一个年份,统计输出的年份到现在,一共有多少天
package javaproject0311;
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
//write your code here........
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
int sum = 0;
scanner.close();
for(int i = year;i < 2022;i++) {
sum+=leapYear(i);
}
int temp = sum + 31 + 28 + 11;
System.out.println(temp);
}
public static int leapYear(int y) {
if (y % 4 == 0) {
if (y % 100 != 0) {
return 366;
} else {
return 365;
}
} else if (y % 400 == 0) {
return 366;
} else {
return 365;
}
}
}