Java程序设计简单练习题

整理了一下最近做的Java练习题,编程新手请多指教
1、编写一个实现由1到k的平方和计算

package demo1.code1;
import java.util.Scanner;
public class Calculate_k {
    public static int calculation(int k) {
        int result = 0;
        for (int i = 1; i <= k; i++) {
            result = i * i + result;
        }
        return result;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int result;
        int num;
        System.out.print("输入一个整数:");
        num = sc.nextInt();
        result = calculation(5);
        System.out.println(result);
    }
}

2.用Java描述一个三角形,提供计算三角形面积方法。三角形的三个边长从键盘输入。

package demo1.code1;
import java.util.Scanner;
public class Triangle {
    private double a, b, c;  //三角形的三条边的边长
    public Triangle(double aa, double bb, double cc) {
        a = aa;
        b = bb;
        c = cc;
    }//构造函数
    public double getArea() {
        double p;
        double s;
        p = (a + b + c) / 2;
        s = p * (p - a) * (p - b) * (p - c);
        s = Math.sqrt(s);
        return s;
    }//计算三角形面积
    public static void main(String[] args) {
        double a, b, c;
        double s;
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入三角形的三条边:");
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        Triangle tri = new Triangle(a, b, c);
        s = tri.getArea();
        System.out.println(s);
    }
}

3.假如今天是星期二,100天后将是星期几?

package demo1.code1;
public class CountDate {
    public static void main(String[] args) {
        int days = 100;
        int d;
        d = days % 7;
        System.out.println("假设今天是星期二");
        switch (d) {
            case 0:
                System.out.println("今天的100天后是星期二");
                break;
            case 1:
                System.out.println("今天的100天后是星期三");
                break;
            case 2:
                System.out.println("今天的100天后是星期四");
                break;
            case 3:
                System.out.println("今天的100天后是星期五");
                break;
            case 4:
                System.out.println("今天的100天后是星期六");
                break;
            case 5:
                System.out.println("今天的100天后是星期日");
                break;
            case 6:
                System.out.println("今天的100天后是星期一");
                break;
        }
    }
}

4.人民币的面值有100元,50元,20元,10元,5元,1元,5角,1角,5分,1分。编写程序实现从键盘输入1万元以内的任意数,然后分解成不同数量的,不同面值的人民币?

package demo1.code1;
import java.util.Scanner;
public class RMBDivision {
    public static void main(String[] args) {
        double rmb;
        int rmb_left;
        int rmb_fj;
        int hundred = 10000;
        int fif = 5000;
        int ten = 1000;
        int fiv = 500;
        int one = 100;
        double fivej = 50;
        double onej = 10;
        double fivef = 5;
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入人民币数值(单位:元):");
        rmb = sc.nextDouble();
        rmb_left = (int) rmb;
        rmb = rmb - rmb_left;
        rmb_left = rmb_left * 100;    //将用户输入的人民币单位从元化作分
        rmb = rmb * 100;
        rmb_fj = (int) rmb;
        hundred = rmb_left / hundred;
        rmb_left = rmb_left - hundred * 10000;
        fif = rmb_left / fif;
        rmb_left = rmb_left - fif * 5000;
        ten = rmb_left / ten;
        rmb_left = rmb_left - ten * 1000;
        fiv = rmb_left / fiv;
        rmb_left = rmb_left - fiv * 500;
        one = rmb_left / one;
        rmb_left = rmb_left - one * 100;
        fivej = rmb_fj / (int) fivej;
        rmb_fj = rmb_fj - (int) fivej * 50;
        onej = rmb_fj / (int) onej;
        rmb_fj = rmb_fj - (int) onej * 10;
        fivef = rmb_fj / (int) fivef;
        rmb_fj = rmb_fj - (int) fivef * 5;
        System.out.println("该数值的人民币可分解为" + hundred + "张一百元," + fif + "张五十元," + ten + "张十元," + fiv + "张五元," +
                one + "张一元," + (int) fivej + "张五角," + (int) onej + "张一角," + (int) fivef + "张五分," + rmb_fj + "张一分纸币。");
    }

}
5.编写程序,提示输入两点(x1,y1)和(x2,y2),然后计算两点之间的距离。

package demo1.code1;
import java.util.Scanner;
public class DistanceOfTwoPoints {
    public static double distance(double a, double b, double c, double d) {
        double distance;
        distance = Math.sqrt((a - c) * (a - c) + (b - d) * (b - d));
        return distance;
    }
    public static void main(String[] args) {
        double a, b, c, d;
        double dist;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一个点的横坐标与纵坐标(空格键隔开):");
        a = sc.nextDouble();
        b = sc.nextDouble();
        System.out.println("请输入第二个点的横坐标与纵坐标(空格键隔开):");
        c = sc.nextDouble();
        d = sc.nextDouble();
        dist = distance(a, b, c, d);
        System.out.print("两点之间的距离为:" + dist);
    }
}

6.已知三角形的底边和高尺寸分别是56厘米、120厘米,计算该三角形的面积?

package demo1.code1;
public class Triangle02 {
    private double bottom, height;  //三角形的底边和高
    public Triangle02(double b, double h) {
        bottom = b;
        height = h;
    }//构造函数
    public double getArea() {
        double s;
        s = bottom * height / 2;
        return s;
    }
    public static void main(String[] args) {
        double result;
        Triangle02 tri = new Triangle02(56, 120);
        result = tri.getArea();
        System.out.println("三角形的面积为" + result + "平方厘米。");
    }
}

7.从键盘输入三个整数,分别计算它们的平均值、累加和、最大值和最小值。

package demo1.code1;
import java.util.Scanner;
public class ThreeNumCalculation {
    public static int Sum(int a, int b, int c) {
        int result;
        result = a + b + c;
        return result;
    }
    public static int Average(int a, int b, int c) {
        int result;
        result = (a + b + c) / 3;
        return result;
    }
    public static int Max(int a, int b, int c) {
        int max;
        if (a > b) {
            max = a;
        } else {
            max = b;
        }
        if (max > c) {
            return max;
        } else {
            return c;
        }
    }
    public static int Min(int a, int b, int c) {
        int min;
        if (a < b) {
            min = a;
        } else {
            min = b;
        }
        if (min < c) {
            return min;
        } else {
            return c;
        }
    }
    public static void main(String[] args) {
        int a, b, c;
        int result;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入三个整数:");
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        result = Average(a, b, c);
        System.out.println("三个数的平均值为:" + result);
        result = Sum(a, b, c);
        System.out.println("三个数的累加和为:" + result);
        result = Max(a, b, c);
        System.out.println("三个数中的最大值为:" + result);
        result = Min(a, b, c);
        System.out.println("三个数中的最小值为:" + result);
    }
}

8.已知圆的半径为R,从键盘输入R的值,分别计算圆的面积和周长。

package demo1.code1;
import java.util.Scanner;
public class Circle {
    private double radius;
    public Circle(double R) {
        radius = R;
    }
    public double getCircumference() {
        return 2 * 3.14 * radius;
    }
    public double getArea() {
        return 3.14 * radius * radius;
    }
    public static void main(String[] args) {
        double result;
        double R;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入圆半径大小:");
        R = sc.nextDouble();
        Circle cir = new Circle(R);
        result = cir.getArea();
        System.out.println("圆的面积为:" + result);
        result = cir.getCircumference();
        System.out.println("圆的周长为:" + result);
    }
}

9.编写程序生成15个随机(int类型)整数?

package demo1.code1;
import java.util.Random;
public class FifteenRandomInt {
    public static void main(String[] args) {
        int num;
        Random ran = new Random();
        for (int i = 0; i < 15; i++) {
            num = ran.nextInt(100);
            System.out.print(num + " ");
        }
    }
}

10.有一三角形的三个顶点A(x,y)、B(x,y)和C(x,y),编写程序从键盘输入三个顶点的坐标,然后计算这个三角形的三个边长和面积?

package demo1.code1;
import java.util.Scanner;
public class Triangle03 {
    private double fir_x, fir_y;
   private double sec_x, sec_y;
   private double thi_x, thi_y;
   public Triangle03(double fx, double fy, double sx, double sy, double tx, double ty) {
        fir_x = fx;
        fir_y = fy;
        sec_x = sx;
        sec_y = sy;
        thi_x = tx;
        thi_y = ty;
    }
    public static double distance(double a, double b, double c, double d) {
        double distance;
        distance = Math.sqrt((a - c) * (a - c) + (b - d) * (b - d));
        return distance;
    }
    public double getPerimeter() {
        double dist1 = distance(fir_x, fir_y, sec_x, sec_y);
        double dist2 = distance(fir_x, fir_y, thi_x, thi_y);
        double dist3 = distance(sec_x, sec_y, thi_x, thi_y);
        return dist1 + dist2 + dist3;
    }
    public double getArea() {
        double p;
        double s;
        double dist1 = distance(fir_x, fir_y, sec_x, sec_y);
        double dist2 = distance(fir_x, fir_y, thi_x, thi_y);
        double dist3 = distance(sec_x, sec_y, thi_x, thi_y);
        p = (dist1 + dist2 + dist3) / 2;
        s = p * (p - dist1) * (p - dist2) * (p - dist3);
        s = Math.sqrt(s);
        return s;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double a, b, c, d, e, f;
        System.out.print("请输入三角形第一个顶点坐标(空格键隔开):");
        a = sc.nextDouble();
        b = sc.nextDouble();
        System.out.print("请输入三角形第二个顶点坐标(空格键隔开):");
        c = sc.nextDouble();
        d = sc.nextDouble();
        System.out.print("请输入三角形第三个顶点坐标(空格键隔开):");
        e = sc.nextDouble();
        f = sc.nextDouble();
        Triangle03 tri = new Triangle03(a, b, c, d, e, f);
        System.out.println("该三角形的周长为:" + tri.getPerimeter());
        System.out.println("该三角形的面积为:" + tri.getArea());
    }
}

11.编写程序,提示从键盘输入一个字符串,然后显示字符串的长度及第一个和最后一个字符?

package demo1.code1;
import java.util.Scanner;
public class StringLength {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = new String();
        int length;
        System.out.print("请输入一个字符串:");
        str = sc.nextLine();
        length = str.length();
        char[] c = str.toCharArray();
        char last_char = c[length - 1];
        System.out.println("该字符串长度为:" + length);
        System.out.println("该字符串最后一个字符为:" + last_char);
    }
}

12.编写程序,提示从键盘输入两个字符串,然后检测第二个字符串是否包含于第一个串中?例如,字符串“Lambda expressions let you express instances of single-method classes more compactly.”和“express”,其中“express”包含于第一个字符串内。

package demo1.code1;
import java.util.Scanner;
public class TwoStringsContains {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str1, str2;
        System.out.print("请输入第一个字符串:");
        str1 = sc.nextLine();
        System.out.print("请输入第二个字符串:");
        str2 = sc.nextLine();
        if (str1.contains(str2)) {
            System.out.println("第二个字符串包含于第一个字符串内。");
        } else {
            System.out.println("第二个字符串不包含于第一个字符串内。");
        }
    }
}

13.编写程序生成2016个随机(int类型)整数,然后按“升序”排序?

package demo1.code1;
import java.util.Arrays;
import java.util.Random;
public class RandomInt2016 {
    public static void main(String[] args) {
        int num;
        Random ran = new Random();
        int[] array = new int[2016];
        for (int i = 0; i < 2016; i++) {
            num = ran.nextInt(2000);
            array[i] = num;
        }
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}

14.编写一个Java类实现查找1~n之间的素数?

package demo1.code1;
import java.util.Scanner;
public class OneToNPrime {
    public static void findPrime(int num) {
        boolean isPrime = true;
        for (int i = 2; i <= num; i++) {
            for (int j = 2; j < i; j++) {// 不能写j<=i,任何数都能被自身整除
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                System.out.print(i + " ");
            }
            isPrime = true;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num;
        System.out.println("请输入一个正整数:");
        num = sc.nextInt();
        System.out.println("1~" + num + "之间的素数有:");
        findPrime(num);
    }
}

15.阶乘函数f(n)=n!,其中n为正整数。试设计一个计算阶乘的方法,并计算f(5)和f(10)的值。

package demo1.code1;
import java.util.Scanner;
public class CalculateFactorial {
    public static int calculateFactorial(int num) {
        int result = 1;
        for (int i = 1; i <= num; i++) {
            result = result * i;
        }
        return result;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int result;
        int num;
        System.out.println("请输入一个正整数:");
        num = sc.nextInt();
        result = calculateFactorial(num);
        System.out.println(num + "!=" + result);
    }
}

16.有整数M,求出小于M的所有素数?

package demo1.code1;
import java.util.Scanner;
public class CalculateFactorial {
    public static int calculateFactorial(int num) {
        int result = 1;
        for (int i = 1; i <= num; i++) {
            result = result * i;
        }
        return result;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int result;
        int num;
        System.out.println("请输入一个正整数:");
        num = sc.nextInt();
        result = calculateFactorial(num);
        System.out.println(num + "!=" + result);
    }
}

17、利用下列数列计算圆周率的近似值。
在这里插入图片描述

package demo1.code1;
import java.util.Scanner;
public class CalculateΠ {
    public static double calculate(int num) {
        double result = 1;
        double sign = -1;// 如果sign是int类型的话会造成数据精度丢失
        for (int i = 2; i <= num; i++) {
            result = result + (sign / (2 * i - 1));
            sign *= (-1);
        }
        result *= 4;
        return result;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num;
        System.out.println("请输入一个整数:");
        num = sc.nextInt();
        System.out.println("圆周率的近似值为:" + calculate(num));
    }
}

你可能感兴趣的:(Java,java,JAVA)