【JAVA】上机实验题目一(中国地质大学)

上机实验题目

主要记录一下Java程序设计的上机实验题目,提供题目和代码,可以参考
在这里插入图片描述

本来是面向过程,没有仔细审题,有的写成了面向过程的。不过也是可以参考的。

三角形周长和面积

这个没什么好说的

主要有一个LegalityJudgment,合法性判断是否构成一个三角形。我把它加入在setLength里面,如果不是三角形,就无法赋值,如果是才能赋值。

另外就是鲁棒性的考量,能够支持多次输入,支持用户交互,这个是可以参考的。

import java.lang.*;
import java.util.Scanner;

public class Triangle {
    public static void main(String[] args){              
        Scanner scanner = new Scanner(System.in);
        label:
        while (true) {
            System.out.println("请输入三角形的三条边长:");
            double l1 = scanner.nextDouble();
            double l2 = scanner.nextDouble();
            double l3 = scanner.nextDouble();
            scanner.nextLine();

            TriangleClass triangle = new TriangleClass();
            triangle.setlength(l1, l2, l3);

            System.out.println("如果你想获取面积,请输入“面积”,如果向获取周长,请输入“周长”,如果想结束程序,输入“结束”");
            String str = scanner.nextLine();
            switch (str) {
                case "面积":
                    System.out.println(triangle.getArea());
                    break;
                case "周长":
                    System.out.println(triangle.getCircumference());
                    break;
                case "结束":
                    break label;
                default:
                    System.out.println("您的输入有误><,请重试");
                    break;
            }
        }
        scanner.close();
    }
}
class TriangleClass{
    double l1;
    double l2;
    double l3;
    //合法性判断
    private boolean LegalityJudgment(double a,double b,double c){ 
        return !(a + b <= c);
    }
    public void setlength(double a,double b,double c){
        boolean sign = LegalityJudgment(a, b, c);

        if(!sign) System.out.println("您的输入错误");
        else{this.l1 = a;
        this.l2 = b;
        this.l3 = c;}
    }
    public double getCircumference(){
        return l1+l2+l3;
    }
    public double getArea(){
        double p = getCircumference()/2;
        return Math.sqrt(p*(p-l1)*(p-l2)*(p-l3));
    }
}









## 输出菱形

算是经典的入门题,主要控制“ ”,“*”,“\n”与行数,列数的关系。

```java
import java.util.Scanner;

public class Lozenge {
    public static void main(String[] args){    
        Scanner scanner = new Scanner(System.in);
        int rows = scanner.nextInt();
        LozengeClass lozengeClass = new LozengeClass();
        lozengeClass.setRows(rows);
        lozengeClass.solve();
    }
}

class LozengeClass {
    int rows;

    public void setRows(int row){
        this.rows = row;
    }

    // 上半部分
    public void solve() {
        int spaces = rows - 1;
        int stars = 1;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= spaces; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= stars; j++) {
                if (j == 1 || j == stars) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
            spaces--;
            stars += 2;
        }

        // 下半部分
        spaces = 1;
        stars = rows * 2 - 3;
        for (int i = 1; i <= rows - 1; i++) {
            for (int j = 1; j <= spaces; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= stars; j++) {
                if (j == 1 || j == stars) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
            spaces++;
            stars -= 2;
        }
    }
}

解方程

这个主要考虑delta的情况,没什么好说的。

import java.util.Scanner;

public class equationsSolve {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double a = scanner.nextDouble();
        double b = scanner.nextDouble();
        double c = scanner.nextDouble();
        equationSolver equationsolver = new equationSolver();
        equationsolver.setABC(a, b, c);
        equationsolver.sovle();
    }
}
class equationSolver{
    double a;
    double b;
    double c;
    public void setABC(double A,double B,double C) {
        this.a = A;
        this.b = B;
        this.c = C;
    }
    public void sovle() {
        double delta = b * b - 4 * a * c;

        if (delta < 0) System.out.println("无实数跟");
        else if (delta > 0) {
            double x1 = (-b - Math.sqrt(delta)) / 2 * a;
            double x2 = (-b + Math.sqrt(delta)) / 2 * a;
            System.out.print(x1+ " ");
            System.out.println(x2);
        } else {
            double x = (-b / 2) * a;
            System.out.println(x);
        }
    }
}

输出菱形

算是经典的入门题,主要控制“ ”,“*”,“\n”与行数,列数的关系。

import java.util.Scanner;

public class Lozenge {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int rows = scanner.nextInt();
        LozengeClass lozengeClass = new LozengeClass();
        lozengeClass.setRows(rows);
        lozengeClass.solve();
    }
}

class LozengeClass {
    int rows;

    public void setRows(int row){
        this.rows = row;
    }

    // 上半部分
    public void solve() {
        int spaces = rows - 1;
        int stars = 1;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= spaces; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= stars; j++) {
                if (j == 1 || j == stars) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
            spaces--;
            stars += 2;
        }

        // 下半部分
        spaces = 1;
        stars = rows * 2 - 3;
        for (int i = 1; i <= rows - 1; i++) {
            for (int j = 1; j <= spaces; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= stars; j++) {
                if (j == 1 || j == stars) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
            spaces++;
            stars -= 2;
        }
    }
}

质数

下次试试更高级的方法。

这里单纯就是二重循环判断

import java.util.Scanner;

public class Primenumber {
    public static void main(String[] args){          
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int[] b =new int[a];
        for(int i = 0;i < b.length;i++){
            b[i] = scanner.nextInt();
        }
        Primenumberselector primenumberselector = new Primenumberselector();
        primenumberselector.setArray(b);
        System.out.println(primenumberselector.solve());
    }
}

class Primenumberselector{
    private int[] a;
    public void setArray(int[] A){
        this.a = A;
    }

    public int solve() {
        int count = 0;
        for (int k : a) {
            boolean isPrime = k > 1;
            for (int j = 2; j * j <= k; j++) {
                if (k % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                count++;
            }
        }
        return count;
    }
}

基于ASCII的加密和解密

很基础的题目,看代码,

import java.util.Scanner;

public class UnlockASII {
    public static void main(String[] args) {       
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();

        System.out.println("Original string: " + input);

        String encryptedString = encrypt(input);
        System.out.println("Encrypted string: " + encryptedString);

        String decryptedString = decrypt(encryptedString);
        System.out.println("Decrypted string: " + decryptedString);
        }
    public static String encrypt(String input) {
        StringBuilder encrypted = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            int ascii = (int) input.charAt(i);
            ascii += 1; // Shift each character by 1
            encrypted.append((char) ascii);
        }
        return encrypted.toString();
    }

    public static String decrypt(String encryptedString) {
        StringBuilder decrypted = new StringBuilder();
        for (int i = 0; i < encryptedString.length(); i++) {
            int ascii = (int) encryptedString.charAt(i);
            ascii -= 1; // Shift each character back by 1
            decrypted.append((char) ascii);
        }
        return decrypted.toString();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值