Java编程练习

1、计算圆周率
  使用莱布尼茨公式(一个无穷级数):代码中的 do-while 循环用于计算这个级数的和,直到项的绝对值小于 1e-6(即 0.000001),这意味着当项变得非常小,对总和的贡献可以忽略不计时,循环停止。
public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        double s = 0.0;
        int sign = 1;
        double n = 1.0;
        do {
            s += 1/n*sign;
            n += 2;
            sign *= -1;
        }while(!(Math.abs(1/n)<1e-6));
        System.out.println(s*4);
    }
}

2、函数关系求解
根据变量 x 的值来计算变量 y 的值输出:
如果 x 小于 0,则 y 为 0.
如果 x 在 0 到 10 之间(包括 0 和 10),则 y 等于 x.
如果 x 在 10 到 20 之间(包括 10 和 20),则 y 等于 0.5 * x + 18.
如果 x 大于或等于 20,则 y 为 100.
class test {
    public static void main(String[] args) {
        double x, y;      // 定义变量 x 和 y
        x = 10;
        y = 20;            // 初始化变量 x 和 y
        if (x < 0) {
            y = 0;
        } else if (x >= 0 && x < 10) {
            y = x;
        } else if (x < 20 && x >= 10) {
            y = 0.5 * x + 18;
        } else if (x >= 20) {
            y = 100;
        }    // 判断
        System.out.println(y);
        // 输出结果
    }
}

3、字符串统计
  通过遍历用户输入的字符串,统计并输出其中的字母和数字的数量.
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入一串字符: ");

        char[] arr = input.nextLine().toCharArray();
        int str = arr.length;
        int word = 0, num = 0;

        while (str-- > 0) {
            char c = arr[arr.length - str - 1];
            if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
                word++;
            } else if (c >= '0' && c <= '9') {
                num++;
            }
        }
        System.out.println("字符串中的字母数量为:" + word);
        System.out.println("字符串中的数字数量为:" + num);
        input.close();
    }
}

4、水果价格查询
  通过一个循环菜单让用户选择不同的水果,并显示其价格。当用户选择退出时,程序结束。
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        boolean isLoop = true;
        Scanner input = new Scanner(System.in);

        System.out.println("[1] apple");
        System.out.println("[2] pear");
        System.out.println("[3] orange");
        System.out.println("[4] grape");
        System.out.println("[0] exit");

        while (isLoop) {
            int n = input.nextInt();
            switch (n) {
                case 0:
                    isLoop = false;
                    break;
                case 1:
                    System.out.println("price=" + "3.00元/斤");
                    break;
                case 2:
                    System.out.println("price=" + "2.50元/斤");
                    break;
                case 3:
                    System.out.println("price=" + "4.10元/斤");
                    break;
                case 4:
                    System.out.println("price=" + "10.20元/斤");
                    break;
                default:
                    System.out.println("没有该选项,请重新输入");
            }
        }
        input.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值