Java实验一

计算100以内奇数和偶数的和,并输出。

public class dome1 {
    public static void main(String[] args) {
        int sum = 0;
        int sum1 = 0;
        for (int i = 1; i <= 100; i++) {
           if (i%2==0){
               sum=sum+i;
           }else {
                sum1=sum1+i;
            }
        }
        System.out.println("偶数和"+sum);
        System.out.println("奇数和"+sum1);
    }
}

计算 n!并打印输出结果。(说明:n是由用户自己输入的值)

import java.util.Scanner;
public class Dome2 {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.println("请输入数,进行求阶乘:");
        int n=s.nextInt();
        int m=1;
        for (int i = 1; i < n+1; i++) {
            m=m*i;
        }
        System.out.println(n+"的阶乘"+m);
    }
}

计算1—1000 之间能被5整除的数,且每行输出三个。

public class Dome3 {
    public static void main(String[] args) {
        int count = 0;
        for (int j = 1; j <= 1000; j++) {
            if (j % 5 == 0) {
                System.out.print(j + "\t");
                count++;
                if (count % 3 == 0) {
                    System.out.println();
                }
            }
        }
    }
}

程序具有如下功能:生成0-100的随机整数,直到生成88为止,停止,并打印输出循环的次数。

import java.util.Random;
public class Dome4 {
    public static void main(String[] args) {
        int count = 0;
        Random r = new Random();
        while (true) {
            int s = r.nextInt(100);
            count++;
            if (s == 88) {
                break;
            }
        }
        System.out.println("循环的次数:" + count);
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基本的Java实验,用于实现复数类和一些基本操作: ```java public class ComplexNumber { private double real; private double imaginary; public ComplexNumber(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public double getReal() { return real; } public double getImaginary() { return imaginary; } public ComplexNumber add(ComplexNumber other) { return new ComplexNumber(real + other.real, imaginary + other.imaginary); } public ComplexNumber subtract(ComplexNumber other) { return new ComplexNumber(real - other.real, imaginary - other.imaginary); } public ComplexNumber multiply(ComplexNumber other) { double newReal = real * other.real - imaginary * other.imaginary; double newImaginary = real * other.imaginary + imaginary * other.real; return new ComplexNumber(newReal, newImaginary); } public ComplexNumber divide(ComplexNumber other) { double denominator = other.real * other.real + other.imaginary * other.imaginary; double newReal = (real * other.real + imaginary * other.imaginary) / denominator; double newImaginary = (imaginary * other.real - real * other.imaginary) / denominator; return new ComplexNumber(newReal, newImaginary); } public String toString() { if (imaginary < 0) { return real + " - " + (-imaginary) + "i"; } else { return real + " + " + imaginary + "i"; } } } ``` 这个类定义了一个名为ComplexNumber的复数类,包含实部和虚部。您可以通过调用实例方法来执行以下操作: - add():添加两个复数。 - subtract():从两个复数减去一个。 - multiply():将两个复数相乘。 - divide():将两个复数相除。 - toString():将复数作为字符串返回。 以下是一个简单的示例,演示如何使用ComplexNumber类: ```java public class Main { public static void main(String[] args) { ComplexNumber c1 = new ComplexNumber(1, 2); ComplexNumber c2 = new ComplexNumber(3, 4); ComplexNumber sum = c1.add(c2); ComplexNumber difference = c1.subtract(c2); ComplexNumber product = c1.multiply(c2); ComplexNumber quotient = c1.divide(c2); System.out.println("c1: " + c1.toString()); System.out.println("c2: " + c2.toString()); System.out.println("Sum: " + sum.toString()); System.out.println("Difference: " + difference.toString()); System.out.println("Product: " + product.toString()); System.out.println("Quotient: " + quotient.toString()); } } ``` 输出结果: ``` c1: 1.0 + 2.0i c2: 3.0 + 4.0i Sum: 4.0 + 6.0i Difference: -2.0 - 2.0i Product: -5.0 + 10.0i Quotient: 0.44 + 0.08i ``` 希望这个例子能够对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值