break跳出所有循环,continue跳过本次循环。十进制转二进制,求出二进制中1的个数
public static void main(String[] args) {
int a = 7;
int number = 0;
while(a>0){
int b = 1;
b = a & b;
a >>= 1;//右移相当于除二
if (b == 1) {
number++;
}
}
System.out.println(number);
}
Scanner input=new Scanner(System.in);
System.out.println("请输入n的值");
int number=input.nextInt();
double sum=0;
for(int i=1;i<number+1;i++){
if(i%2==0){//用if判断,奇数则加,偶数则减。
sum-=(1.0/i);
}else{
sum+=(1.0/i);
}
}
System.out.println(sum);
计算1+3+5+7+9+……..+(2*21-1)=?
int sum=0;
for(int i=1;i<21;i++){
int m=1;
m=m<<i;
sum+=m-1;
}
public static void main(String[] args) {
int array1[] = { 12, 3, 45, 67, 32, 24, 67, 90, 41 };
int array2[] = Arrays.copyOfRange(array1, 2, 6);// 数组截取
System.out.println(Arrays.toString(array2));
}
public static void main(String[] args) {
int[] array1=new int[10];
Random rd=new Random();//创建一个新对象
for(int i=0;i<array1.length;i++) {
array1[i]=rd.nextInt(90)+10;//产生随机数
}
Arrays.sort(array1);
// System.out.println(Arrays.toString(array1));不用for循环输出,而用tostring()输出
for(int i=0;i<array1.length;i++){
System.out.print(array1[i]+"\t");
}
public static void main(String[] args) {
int array1[] = { 12, 23, 41, 25, 56, 78, 5, 96 };
int array2[] = new int[10];
Arrays.sort(array1);
// for(int i=0;i<array1.length;i++){
// System.out.print(array1[i]+"\t");
// }
System.out.println(Arrays.toString(array1));//tostring返回指定数组的表示形式
}
public static void main(String[] args) {
int[][] array1 = new int[4][4];
// Scanner input = new Scanner(System.in);
Random rd = new Random();
int sum1 = 1;
int sum2 = 1;
String str = "";
String str1 = "";
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
array1[i][j] = rd.nextInt(9);
System.out.print(array1[i][j] + "\t");
}
System.out.println();
}
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
if (i == j) {
str += "\t" + array1[i][j];
sum1 *= array1[i][j];
}
if (i + j == 3) {
str1 += "\t" + array1[i][j];
// System.out.print(array1[i][j] + "\t");
sum2 *= array1[i][j];
}
}
}
System.out.println("对角线的数:" + str);
System.out.println("对角线的乘积是:" + sum1);
System.out.println("副对角线的数:" + str1);
System.out.println("副对角线上的乘积是:" + sum2);
}
//另一种方法
public static void main(String[] args) {
int[][] array1 = new int[4][4];
Random rd = new Random();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
array1[i][j] = rd.nextInt(9);
}
System.out.println(Arrays.toString(array1[i]));
}
String str1 = "";
String str2 = "";
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i == j) {
str1 += "\t" + array1[i][j];
}
if (i + j == 3) {
str2 += "\t" + array1[i][j];
}
}
}
int temp = 1;
int temp2 = 1;
for (int i = 0; i < 4; i++) {
temp = temp * array1[i][i];
temp2 = temp2 * array1[i][3 - i];
}
System.out.println(str1);
System.out.println("对角线上的乘积是:" + temp);
System.out.println(str2);
System.out.println("副对角线上的乘积是:" + temp2);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String email = input.next();
boolean flag = false;
do {
if (email.contains("@")) {
int index = email.indexOf("@");
String start = email.substring(0, index);
boolean isLetterOrNumber = true;
for (int i = 0; i < start.length(); i++) {
char ch = start.charAt(i);
if (!Character.isLetterOrDigit(ch)) {
isLetterOrNumber = false;
break;
}
}
if (isLetterOrNumber) {
System.out.println("开头是以数字或字母开始的");
} else {
System.out.println("不是以数字或字母开头的");
System.out.println("不是正确的邮箱");
break;
}
if (email.contains(".")) {
int index1 = email.lastIndexOf(".");
String middle = email.substring(index + 1, index1);
boolean middleIsLetterOrNumber = true;
for (int i = 0; i < middle.length(); i++) {
char ch = middle.charAt(i);
if (!Character.isLetterOrDigit(ch)) {
middleIsLetterOrNumber = false;
break;
}
}
if (middleIsLetterOrNumber) {
System.out.println("中间是以数字或字母开始的");
} else {
System.out.println("中间不是以数字或字母开始的");
System.out.println("不是正确的邮箱");
break;
}
if (email.endsWith(".com") || email.endsWith(".cn") || email.endsWith(".net")) {
System.out.println("是正确的邮箱");
} else {
System.out.println("但是后缀不正确,不是正确的邮箱");
}
}
} else {
System.out.println("没有@不是正确的邮箱");
}
} while (flag);
}
字符 | 说明 |
---|---|
0 | 代表阿拉伯数字,使用特殊字符“0”表示一位阿拉伯数字,如果该位不存在数字,则显示0 |
# | 代表阿拉伯数字,使用特殊字符“#”表示一位阿拉伯数字,如果该位存在数字,则显示字符;不存在数字则不显示 |
. | 小数分隔符或货币小数分隔符 |
- | 负号 |
, | 分组分隔符 |
E | 分隔科学计数法中的位数和指数 |
% |