新手java练习题100(1-5)

新手java练习题100(1-5)

1、编程实现:根据以下函数关系,对输入的X值计算输出对应的y值。

x的值对应y的值
x<00
0<=x<10x
10<=x<200.5*x+18
x>=20100
class test {
	public static void main(String[] args) {
		double x,y;      //定义参数类型
		x=0,y=0;         //数据初始化
		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);
		//输出
	}
}

2、编写程序计算1!+2!+3!+…+n!,并输出计算结果。byGaoshiguo112

import java.util.Scanner;
public class test2 {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数:");
        int number = 0;
        int jc = 1;
        int result = 0;
        if (scanner.hasNextInt()) {
            number = scanner.nextInt();
            System.out.println("您输入的数字是:" + number);
            for (int i = 1; i <= number; i++) {
                for (int j = 1; j <= i; j++) {
                    jc *= j;
                }

                result = result + jc;
                jc = 1;

            }
            System.out.printf("小于整数%d的所有正整数阶乘之和是:%d", number, result);
        } else {
            System.out.println("您输入的字符不合法");
        }
        scanner.close();
    }

}

3已知圆周率PIE的计算公式为 。要求计算圆周率PIE值(精度为1e-6)。PS:精度是最后一项的绝对值小于1e-6即abs(1/n)<1e-6。

public class test1 {

	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);
	    }
	}
	}

4、从键盘输入一行字符串(以换行符结束),要求分别统计里面英文字符的总个数和数字的总个数,并分别输出.

	import java.util.Scanner;
		public class test3 {
		    public static void main(String[] args) {
		        Scanner input = new Scanner(System.in);
		        
		        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();
		    }
		

	}

5 查询水果价格
给定四种水果,分别是苹果(apple)、梨(pear)、桔子(orange)、葡萄(grape),单价分别对应为3.00元/公斤、2.50元/公斤、4.10元/公斤、10.20元/公斤。
首先在屏幕上显示以下菜单:
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
用户可以输入编号1~4查询对应水果的单价,用户输入0即退出;输入其他编号,显示此水果没有出售。

在这里插入图片描述

import java.util.Scanner;

public class test4 {
    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();
    }
}

如有错误请多多指教

  • 44
    点赞
  • 408
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值