java基础题目练习(持续添加)

控制语句
数组

控制语句

1、打印九九乘法表
public class work01 {
    public static void main(String[] args){
        for (int i = 1;i < 10;i++){
            for (int j = 1;j <= i;j++){
                if((i == 3 && j == 3) || (i == 4 && j == 3)){
                    System.out.print(" ");
                }
                System.out.print(j + "x" + i + "=" + (j*i) + "  ");
            }
            System.out.println();
        }
    }
}

代码实现:

实现界面

2、打印三角形
public class work02 {
    public static void main(String[] args){
        for(int i = 1;i<= 4;i++){
            for(int j = 1;j<=(4-i);j++){
                System.out.print(" ");
            }
            for (int j = 1;j<=(i*2-1);j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

代码实现:

在这里插入图片描述

3、打印菱形
public class work03 {
    public static void main(String[] args) {
        for(int i = 1;i<= 4;i++){
            for (int j = 1;j<= 4-i;j++){
                System.out.print(" ");
            }
            for (int j = 1;j<=(i*2-1);j++){
                System.out.print("*");
            }
            System.out.println();
        }
        for(int i = 1;i<= 3;i++){
            for (int j = 3;j>(3-i);j--){
                System.out.print(" ");
            }
            for (int j = 5;j>=(i*2-1);j--){
                System.out.print("*");
            }
            System.out.println();
        }

    }
}

代码实现:

在这里插入图片描述

4、打印空心菱形
public class work04 {
    public static void main(String[] args) {
        for(int i = 1;i<= 4;i++){
            for (int j = 1;j<= 4-i;j++){
                System.out.print(" ");
            }
            for (int j = 1;j<=(i*2-1);j++){
                if(j==1|| j==(i*2-1) ) {
                    System.out.print("*");
                }else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        for(int i = 1;i<= 3;i++){
            for (int j = 3;j>(3-i);j--){
                System.out.print(" ");
            }
            for (int j = 5;j>=(i*2-1);j--){
                if(j==5 || j==(i*2-1)) {
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}
代码实现:

在这里插入图片描述

5、已知账号admin,密码123456,使用键盘录入账号密码完成登录验证流程,允许输错三次,三次后提示账号锁定
方法一:
import java.util.Scanner;

public class work05 {
    public static void main(String[] args) {
        final String STR  = "admin";
        final int RESULT  = 123456;
        Scanner scanner = new Scanner(System.in);
        String str;
        int result;
        int num = 2;
        for(;;){
            System.out.print("请输入账号:");
            str = scanner.next();
            System.out.print("请输入密码:");
            result = scanner.nextInt();
            if(str.equals(STR) && RESULT == result){
                System.out.println("登录成功,欢迎回来!!!");
                break ;
            }else{
                if (num == 0){
                    System.out.println("因错误次数过多,您的账户已经被锁定!");
                    break;
                }
                System.out.println("您的输入有误,请重新输入:你还有" + num + "次机会!!!");
                num--;
            }
        }
    }
}

方法二:

import java.util.Scanner;

public class work05 {
    public static void main(String[] args) {
        int RESULT = 123456;
        String STR = "admin";
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入账号:");
        String str = scanner.next();
        System.out.print("请输入密码:");
        int result = scanner.nextInt();
        for (int i = 2;i> 0;i--){
            if((!STR.equals(str)) || (!(RESULT == result))){
                System.out.println("您输入的信息有误,请重新输入,你还有" + i + "次机会" );
            }else {
                System.out.println("登录成功,欢迎回来!!!");
                break;
            }
            System.out.print("请输入账号:");
            str = scanner.next();
            System.out.print("请输入密码:");
            result = scanner.nextInt();
        }
    }
}

代码实现:

在这里插入图片描述
在这里插入图片描述

6、随机生成一个1~100整数,然后输入一个整数和生成的数字进行比对,猜大小并给予提示,猜对为止。
import java.util.Random;
import java.util.Scanner;

public class work06 {
    public static void main(String[] args) {
        Random random = new Random();
        int result = (random.nextInt(99) + 1);
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个 0  --  100 的数字:");
        int sum = scanner.nextInt();
        for(;;){
            if(sum > result){
                System.out.println("猜错了,不妨往小一点试试");
                System.out.print("请重新输入一个 0  --  100 的数字:");
                sum = scanner.nextInt();
            }else if(sum < result){
                System.out.println("猜错了,不妨往大一点试试");
                System.out.print("请重新输入一个 0  --  100 的数字:");
                sum = scanner.nextInt();
            }else if(sum == result){
                System.out.println("恭喜你,猜对了");
                break;
            }
        }
        System.out.println("您的幸运数字为:  " + result);
    }
}

代码实现:

在这里插入图片描述

7、由键盘输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321
import java.util.Scanner;

public class work07 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个四位数的整数:");
        int num = scanner.nextInt();
        int temp = 0;
        System.out.print("反转:");
        for (;;){
            if(num == 0){
                break;
            }
            temp = num%10;
            System.out.print(temp);
            num /= 10;
        }
    }
}

代码实现:

在这里插入图片描述

8、int a = 2; int b = 5;如何不使用第三个变量交换两个数?(不可以使用按位运算)
public class work08 {
    public static void main(String[] args) {
        int a = 2;
        int b = 5;
//        a *=b;
//        b = a/b;
//        a /=b;
        a = a+b;
        b = a-b;
        a = a-b;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}
代码实现:

在这里插入图片描述

9、输入一个整数,计算该数有多少位,例如123有3位
import java.util.Scanner;

public class work09 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个正整数:");
        int result = scanner.nextInt();
        int temp = 1;
        for (;result/10!=0;temp++,result/=10){
            } 
            System.out.println("您输入的是一个" + temp + "位数!");
    }
}

代码实现:

在这里插入图片描述

数组

1、打印数组,通过监听控制台得要到数组长度,通过随机数赋值,任何按照要求打印格式为:[值1, 值2, …]
package com.yjxxt.dayWork;

import java.util.Random;
import java.util.Scanner;

public class work {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入[ 10 -- 20 ]的数组长度 :");
        int sum = scanner.nextInt();
        int [] str = new int[sum];
        System.out.print("[");
        for (int i = 0;i<sum;i++){
            str[i] = random();
            if(i == sum-1){System.out.print(str[i]);
            }
            else{
                System.out.print(str[i] + ",");
            }

           // System.out.println("str" + i + "=" + str[i]);
        }
        System.out.println("]");
    }
    public static int  random(){
        Random ran = new Random();
        return  ran.nextInt(100)+1;
    }
}

代码实现:

在这里插入图片描述

2、初始化数组,通过控制台,监听一个指定的数,插入数组某一位
package com.yjxxt.dayWork;

import java.util.Scanner;

public class work02 {
    public static void main(String[] args) {
        Scanner  scanner = new Scanner(System.in);
        int[] str = {1,2,3,4,5,6,7,8,9,10};
        int[] str1 = new int[10];
        System.out.print("请输入你想插入的数:");
        int sum = scanner.nextInt();
        System.out.print("请输入"+ sum + "插入的下标[ 0 -- 9]:");
        int suu = scanner.nextInt();
        for (int i = 0 ;i<str.length;i++){
            if(i == suu){
                str1[i] = sum;
            }else{
                str1[i] = str [i];
            }
            System.out.println("str[" + i + "] ==" + str1[i]);
        }
    }

}

代码实现:

在这里插入图片描述

3、:删除数组指定的某一下标位
package com.yjxxt.dayWork;

import java.util.Scanner;

public class work03 {
    public static void main(String[] args) {
        int[] str = {1,2,3,4,5,6,7,8,9,10};
        int[] str1 = new int[9];
        delete(str,str1);

    }
    public static void delete(int[] str,int[] str1){
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入想要删除的下标[ 0 -- 9]:");
        int sum = scanner.nextInt();
        for (int i = 0 ,j = 0;j<str.length;){
            if(j == sum){
                j++;
            }else{
                str1[i] = str [j];
                System.out.println("str[" + i + "] ==" + str1[i]);
                i++;
                j++;
            }
        }
    }

}

代码实现:
在这里插入图片描述

4、 1.使用键盘录入一个整数,然后初始化该整数对应长度的数组
----- 2. 随机生成1~100的整数放入该数组
package com.yjxxt.dayWork;

import java.util.Random;
import java.util.Scanner;

public class work04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入[ 10 -- 20 ]的数组长度 :");
        int sum = scanner.nextInt();
        int [] str = new int[sum];
        create(str,sum);
    }
    //创建数组
    public static void create(int[] str,int sum){
        for (int i = 0;i<sum;i++){
            str[i] = random();
            System.out.println("str[" + i + "] = " + str[i]);
        }

    }
    // 给数组赋值[1--100]
    public static int  random(){
        Random ran = new Random();
        return  ran.nextInt(100)+1;
    }
}


代码实现:
在这里插入图片描述

5、1.使用键盘录入一个整数,然后初始化该整数对应长度的数组
---- 2. 随机生成1~100的整数放入该数组
---- 3. 找到作业4中数组的最大值和最小值
package com.yjxxt.dayWork;

import java.util.Random;
import java.util.Scanner;

public class work05 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入[ 10 -- 20 ]的数组长度 :");
        int sum = scanner.nextInt();
        int [] str = new int[sum];
        create(str,sum);
        work05.maximum(str);
        work05.small(str);
    }
    //创建数组
    public static void create(int[] str,int sum){
        for (int i = 0;i<sum;i++){
            str[i] = random();
            System.out.println("str[" + i + "] = " + str[i]);
        }

    }
    // 给数组赋值[1--100]
    public static int  random(){
        Random ran = new Random();
        return  ran.nextInt(100)+1;
    }
    //最大数
    public static void maximum(int[] str){
        int max = str[0];
        for (int i = 1; i<str.length;i++){
            if(max<str[i]){
                max = str[i];
            }
        }
        System.out.println("最大值为:" + max);
    }
    //最小数
    public static void small(int[] str){
        int mix = str[0];
        for (int i = 1; i<str.length;i++){
            if(mix>str[i]){
                mix = str[i];
            }
        }
        System.out.println("最小值为:" + mix);
    }

}

代码实现:在这里插入图片描述

6、1.使用键盘录入一个整数,然后初始化该整数对应长度的数组
---- 2. 随机生成1~100的整数放入该数组
---- 3. 将数组的从小到大排序(冒泡和选择)
package com.yjxxt.dayWork;

import java.util.Random;
import java.util.Scanner;

public class work06 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入[ 10 -- 20 ]的数组长度 :");
        int sum = scanner.nextInt();
        int [] str = new int[sum];
        create(str,sum);
        work06.print(str);
    }
    //创建数组
    public static void create(int[] str,int sum){
        for (int i = 0;i<sum;i++){
            str[i] = random();
            System.out.println("str[" + i + "] = " + str[i]);
        }

    }
    // 给数组赋值[1--100]
    public static int  random(){
        Random ran = new Random();
        return  ran.nextInt(100)+1;
    }
    //按从小到大顺序输出
    public static void print(int[] str){
        work06.sort(str);
        for (int i = 0; i<str.length; i++){
            System.out.println("str[" + i + "] = " + str[i]);
        }
    }
    //排序
    public static void sort(int[] str){
        for (int i = 0; i<str.length; i++){
            for (int j = i; j<str.length;j++){
                if(str[i]>str[j]){
                    int temp = str[j];
                    str[j] = str[i];
                    str[i] = temp;
                }
            }
        }

    }
}

代码实现:
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值