花几千上万学习Java,真没必要!(十四)

方法:

测试代码1:不带参数,不带返回值的方法

package testmethod.com;

public class TestClassMethod {
    // 定义一个不带参数且不带返回值的方法
    public static void myMethod() {
        // 在这里编写方法体
        System.out.println("This is my method");
    }

    public static void main(String[] args) {
        // 调用不带参数不带返回值的方法
        myMethod();
    }
}

测试代码2:带参数的方法,不带返回值的方法

package testmethod.com;
//定义一个名为printMessage的方法,它接收一个类型为String的参数message。
//在`main`方法中,创建MyClass的实例myObject,然后调用printMessage方法,
//并传入Hello, World!作为参数值。
public class MyClass {
    // 定义带参数的方法
    public void printMessage(String message) {
        System.out.println(message);
    }

    // 调用带参数的方法
    public static void main(String[] args) {
        MyClass myObject = new MyClass();
        myObject.printMessage("Hello, World!");
    }
}

测试代码3:带参数,带返回值的方法

package testmethod.com;
public class TestCalculator {  
	  
    // 定义一个带参数且返回整型值的方法  
    public int multiply(int num1, int num2) {  
        // 计算两个整数的乘积并返回  
        return num1 * num2;  
    }  
  
    // 主方法,用于测试multiply方法  
    public static void main(String[] args) {  
        TestCalculator calculator = new TestCalculator();  
        // 调用multiply方法并传入两个参数,然后打印返回的乘积  
        int result = calculator.multiply(5, 3);  
        System.out.println("The product is: " + result);  
    }  
}

测试代码4:不带参数,带返回值的方法 

package testmethod.com;
import java.util.Random;  

public class Calculator {  
  
    // 定义一个不带参数返回整型值的方法  
    public int getRandomNumber() {  
        // 创建Random类的实例  
        Random random = new Random();  
        // 生成一个随机的整数并返回,返回的是0到99之间的随机数。  
        return random.nextInt(100);  
    }  
  
    // 主方法,用于测试getRandomNumber方法  
    public static void main(String[] args) {  
        Calculator calculator = new Calculator();  
        // 调用getRandomNumber方法并打印返回的随机数  
        System.out.println("Random Number: " + calculator.getRandomNumber());  
    }  
}

测试代码5:实参和形参

package testmethod.com;
//定义一个名为printSum的方法,它接收两个int类型的参数num1和num2作为形参。
//在main方法中,创建ParameterExample类的实例example,
//然后提供两个实际的参数值a和b给printSum方法进行调用。
public class ParameterExample {
    // 定义一个带有形参的方法
    public void printSum(int num1, int num2) {
        int sum = num1 + num2;
        System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);
    }

    // 在main方法中调用printSum方法并提供实参
    public static void main(String[] args) {
    	
        ParameterExample example = new ParameterExample();
        
        // 提供实参给printSum方法
        int a = 5;
        int b = 3;
        example.printSum(a, b);
    }
}

测试代码6:模拟用户登录的方法

package testmethod.com;
import java.util.Scanner;
public class UserLoginSimulation {
    public static void main(String[] args) {
    	
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入用户名:");
        String username = scanner.nextLine();

        System.out.println("请输入密码:");
        String password = scanner.nextLine();

        // 模拟用户登录
        boolean loggedIn = login(username, password);
        if (loggedIn) {
            System.out.println("用户登录成功!");
        } else {
            System.out.println("用户名或密码错误,登录失败。");
        }

        scanner.close();
    }

    // 模拟用户登录的方法
    public static boolean login(String username, String password) {
        // 模拟数据库中存储的用户名和密码
        String storedUsername = "user1";
        String storedPassword = "password123";

        // 验证输入的用户名和密码是否与数据库中存储的匹配
        if (username.equals(storedUsername) && password.equals(storedPassword)) {
            // 匹配成功,返回true表示登录成功
            return true;
        } else {
            // 匹配失败,返回false表示登录失败
            return false;
        }
    }
}

测试代码7:有多个参数的方法

package testmethod.com;
//定义一个名为processValues的方法,接收一个int类型参数intValue,一个double类型参数doubleValue,和一个String类型参数stringValue。
//在main方法中,创建TestClass的实例testObject,然后调用processValues方法并传入intArg、doubleArg和stringArg作为参数值。

public class TestClass {
    // 定义带三个不同类型参数的方法
    public void processValues(int intValue, double doubleValue, String stringValue) {
        System.out.println("Received values: " + intValue + ", " + doubleValue + ", " + stringValue);
    }

    // 调用带三个不同类型参数的方法
    public static void main(String[] args) {
        TestClass testObject = new TestClass();
        int intArg = 10;
        double doubleArg = 5.5;
        String stringArg = "Hello";
        testObject.processValues(intArg, doubleArg, stringArg);
    }
}

测试代码8: 一个简单的还贷计算

package testmethod.com;
public class FinancialCalculator {  
  
    // 定义一个带参数且返回双精度浮点值的方法,用于计算贷款的每月还款额  
    public double calculateLoanPayment(double principal, double annualInterestRate, int numberOfYears, int paymentsPerYear) {  
        // 将年利率转换为月利率  
        double monthlyInterestRate = annualInterestRate / paymentsPerYear / 12;  
  
        // 将贷款期限转换为总支付月数  
        int totalPayments = numberOfYears * paymentsPerYear;  
  
        // 使用等额本息还款法公式计算每月还款额  
        // 公式为:M = P * [r/(1 - (1 + r)^(-n))]  
        // 其中,M 是每月还款额,P 是贷款总额,r 是月利率,n 是总支付月数  
        double r = monthlyInterestRate;  
        double n = totalPayments;  
        double monthlyPayment = principal * (r / (1 - Math.pow(1 + r, -n)));  
  
        return monthlyPayment;  
    }  
  
    // 主方法,用于测试calculateLoanPayment方法  
    public static void main(String[] args) {  
        FinancialCalculator financialCalculator = new FinancialCalculator();  
        // 假设贷款总额为100,000元,年利率为5%,贷款期限为10年,每月还款一次  
        double monthlyPayment = financialCalculator.calculateLoanPayment(100000, 0.05, 10, 12);  
        // 打印每月还款额  
        System.out.println("The monthly payment is: " + String.format("%.2f", monthlyPayment));  
    }  
}

运行结果如下:

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值