Java基础练习

1.接受用户输入的一个整数,判断它是奇书数还是偶数
import java.util.Scanner;
public class onePerject {
    public static void main(String[] args) {
        Scanner s =new Scanner(System.in);
        int number =s.nextInt();
        if(number%2==1){
            System.out.println("你输入了一个奇数");
        }else {
            System.out.println("你输入了一个偶数");
        }
    }
}
2.使用循环语句计算20的阶乘 20!
import java.util.Scanner;
public class twoProject {
    public static void main(String[] args) {
        Scanner s =new Scanner(System.in);
        long result = 1;
        for (int i=1;i<=20;i++){
            result *=i; //1*2*3*4....*20
        }
        System.out.println(result);
    }
}
3.使用循环语句计算1!+2!+ 3!+…+20!
import java.util.Scanner;
public class treeProject {
    public static void main(String[] args) {
        Scanner m = new Scanner(System.in);
        long finalResult = 0;
        for (int i = 1; i <= 20; i++) { //i=1 2 3 4 ....20
            long result = 1;
            for (int j = 1; j <= i; j++) { //j! 1 2 3 ...i
                result *= j;
            }
            finalResult += result;
        }
        System.out.println(finalResult);
    }
}
4.接受三个整数abc,对这个三个整数按照从小到大的顺序进行排列
import java.util.Scanner;
public class fourProject {
    public static void main(String[] args) {
        Scanner m = new Scanner(System.in);
        int a, b, c;
        System.out.println("输入第一个数");
        a = m.nextInt();
        System.out.println("输入第二个数");
        b = m.nextInt();
        System.out.println("输入第三个数");
        c = m.nextInt();

        if (a > b) {
            int temp = a;
            a = b;
            b = temp;
        }
        if (b > c) {
            int temp = b;
            b = c;
            c = temp;
        }
        if (a > b) {
            int temp = a;
            a = b;
            b = temp;
        }
    }
}
5.编写程序,给定一个学生成绩,给出相应等级 90~100 优秀, 80~89 良好,79~79 中等,60~69 及格,0 ~59 不及格
public class sixPeoject {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("输入一个成绩");
        int score=s.nextInt();
        if(score>=90&&score<=100){
            System.out.println("优秀");
        }else if(score>=80&&score<=89){
            System.out.println("良好");
        }else if(score>=70&&score<=79){
            System.out.println("中等");
        }else if(score>=60&&score<=69){
            System.out.println("及格");
        }else if(score>=0&&score<=59){
            System.out.println("不及格");
        }else{
            System.out.println("输入有误");
        }
    }
}
6.用while 循环 计算1~200之间所有3的倍数之和
public class sevenProject {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int i = 3;
        int total = 0;
        while (i <= 200) {
//            if (i % 3 == 0) {
//                total += i;
//            }
            total += i;
            i += 3;
        }
        System.out.println(total);
    }
}

7.编写程序,输出200~500之间的所有素数
public class eightProject {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int number = s.nextInt();
        for (int j = 200; j <= 500; j++) {
            boolean isSu = true;
            for (int i = 2; i <= j - 2; i++) {
                if (j % i == 0) {
                    isSu = false;
                }
                if (isSu) {
                    System.out.println(j);
                }
            }
        }
    }
}
8.编写程序解决“百元买百鸡”问题。公鸡五钱一只,母鸡三钱一只,小鸡一钱三只,现有白钱欲买百鸡,共有多少种买法?
//  公鸡 5  母鸡 3   小鸡 1/3只
public class nineProject {
    public static void main(String[] args) {
        for(int i=0;i<=20;i++){
            for(int j=0; j<=(100-i*5)/3;j++){
                int chick =(100-i*5-j*3)*3;
                int count = i+j+chick;
                if(count==100){
                    System.out.println("公鸡 :"+i+" 母鸡 :"+j+" 小鸡:"+chick);
                }
            }
        }

    }
}
9.打印出所有“水仙花数”,所谓“水仙花数”是指一个三位数,其个位数字立方和等
public class tenProject {
        for (int i = 100; i < 1000; i++) {
            int ge = i % 10;
            int bai = i / 100;
            int shi=(i/10)%10;
            int total =ge*ge*ge+bai*bai*bai+shi*shi*shi;
            if(total==i){
                System.out.println(total);
            }
        }
    }
}
10.猜数字,随机生成一个数字,让用户猜大小,然后提示猜大了或猜小了,猜不中让用户一直猜,直到用户猜中,游戏结束,就不需要继续猜了
import java.util.Scanner;
public class elevenProject {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int x = (int) (Math.random() * 100);
        System.out.println("-----猜数字游戏------");
        while (true) {
            int number = s.nextInt();
            if (x == number) {
                System.out.println("恭喜猜中了");
                break;
            } else if (x > number) {
                System.out.println("猜小了");
            } else {
                System.out.println("猜大了");
            }
        }
    }
}
11.有一个数组,里面存储的是整数,从这里面查找一个指定整数,找到这个整数所在位置(索引位置)(查找第一个出现的位置)
public class ArrayDamo07 {
    public static void main(String[] args) {
        int[] oldArr = {2, 5, 3, 2, 1, 31, 1, 3, 4};
        int numberTofind = 3;
        int index=-1;
        for (int i = 0; i < oldArr.length ; i++) {
            if(oldArr[i]==numberTofind){
                index=i;break;
            }
        }
        System.out.println(index);
    }
}
12.现在有如下数组 int oldArr[]={12,0,45,0,6,0,0,2,4,3,0,56}; 要求将以上数组中的0去掉,将不为0的值存入一个新的数组生成新的数组为int newArr[]={12,45,6,2,4,3,56};
public class ArrayDame06 {
    public static void main(String[] args) {
        int[] oldArr = {12, 0, 0, 32, 45, 0, 56, 0, 21};
        //            12  32  45  56  21  索引不对应
        int count = 0;
        for (int i = 0; i < oldArr.length; i++) {
            if (oldArr[i] != 0) {
                count++;
            }
        }
        int[] newArr = new int[count];
        int number = 0;
        //当 number = 0  放在索引为0的位置
        //当 number = 1  放在索引为1的位置
        //当 number = 2  放在索引为2的位置
        for (int i = 0; i < oldArr.length; i++) {
            if (oldArr[i] != 0) {
                newArr[number] = oldArr[i];
                number++;
            }
        }
        for (int i = 0; i < newArr.length; i++) {
            System.out.println(newArr[i]+" ");
        }
    }
}
13.把数组里面的元素倒置({123,133,31,23} —>{23 ,31 ,133 ,123})
public class ArrayDame05 {
    public static void main(String[] args) {
        int[] a = {32, 12, 232, 12, 87};
        //7    0 1 2 3 4 5 6      3
        //     0 1 2     7-1-2  7-1-1  7-1
        for (int i = 0; i < (a.length / 2); i++) {
            //i  length-1-i
            int  temp =a[i];
            a[i]=a[a.length-1-i];
            a[a.length-1-i]=temp;
        }
        for (int i = 0; i <a.length ; i++) {
            System.out.println(a[i]+" ");
        }
    }
}
14.取得一个数组里面的最大值和最小值
public class ArrayDame04 {
    public static void main(String[] args) {
    int[] TextArray=new int[]{12,5,13,54,76,233};
    int max=TextArray[0];
    int min=TextArray[0];
        for (int i = 0; i < TextArray.length; i++) {
            if(TextArray[i]>max){
                max=TextArray[i];
            }
            if(TextArray[i]<min){
                min=TextArray[i];
            }
        }
        System.out.println(max);
        System.out.println(min);
    }
}
15.复制数组,将数组里面的数据全部复制到另一个数组里面
public class ArrayDame03 {
    public static void main(String[] args) {
    int[] S=new int[]{12,23,42};
    int[] T=new int[S.length];
        for (int i = 0; i <S.length ; i++) {
            T[i]=S[i];
        }
        for (int i = 0; i < T.length; i++) {
            System.out.println(T[i]+" ");
        }
    }
}
16.将“A”,“B”,“C” 存入数组,然后再输出
public class ArrayDame02 {
    public static void main(String[] args) {
        char[] c = {'A','B','C'};
        for (int i = 0; i <c.length ; i++) {
            System.out.println(c[i]);
        }
    }
}
17.遍历一个数组中所有的元素,并输出出来
public class ArrayDame01 {
    public static void main(String[] args) {
        int[] scoreArray=new int[]{12,43,46,5,21,32,54,65,76,34,11};
        for (int i = 0; i < scoreArray.length; i++) {
            System.out.println(scoreArray[i]);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值