基本数据与计算习题总结

本文总结了关于基本数据与计算的几道编程习题,包括摄氏度转华氏度、圆柱体面积体积计算、数字相加、时区时间转换、复利计算以及两点间距离和三角形面积的计算。通过实例解析,帮助读者理解和掌握相关算法。
摘要由CSDN通过智能技术生成

哈哈哈,大家好!可爱又迷人的我又来了,今天我们来说一下关于“基本数据与计算”方面的习题,这次比上次就稍微有了一点点难度,但也是挺简单的。

思路分析:我们可以看到,这道题和之前的题有所不同,它不是让我们直接输出,而是要求我们先从控制台读入double型的摄氏温度,然后将其转换成华氏温度,最后再显示结果。下面就具体来看看吧。

第一步:既然是要从控制台读入数据,那么就要用到输入,所以这里就需要导入Java中的输入包,import java.util.Scanner;紧接着,我们需要提示用户从键盘输入摄氏温度这个数据;

第二步:题目中有华氏温度和摄氏温度这两个变量,而且温度有小数,那我们首先就得创两个double型的变量,摄氏温度是直接从控制台读入的,华氏温度需要我们根据读入的摄氏温度来进行相应运算;

第三步:题目中已经给出了它们两者之间的关系,所以我们直接运算就可以了;

第四步:编写代码。

import java.util.Scanner;
public class Demo02_01{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a degree in Celsius: ");
        double Celsius = scanner.nextDouble();//创建double型的变量Celsius
        double Fahrenheit = (9.0 / 5) * Celsius + 32;//计算华氏温度
        System.out.println(Celsius + " Celsius" + " is " + Fahrenheit + " Fahrenheit");
    }
}

运行结果:

Enter a degree in Celsius: 43
43.0 Celsius is 109.4 Fahrenheit

 

思路分析:这道题是要我们从控制台得到两个数据,分别是圆柱体的半径和高,并用题目中给出的公式来计算圆柱体的面积和体积,来具体看看。

第一步:因为半径和高不一定都是整数,所以我们先创建两个double型的变量,分别表示圆柱体的半径和高。然后提示用户从键盘输入半径和高;

第二步:根据题目中给出的两者的计算公式进行计算;

第三步:编写代码。

import java.util.Scanner;
public class Demo02_02{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the radius and length of a cylinder: ");
        double radius = scanner.nextDouble();//输入半径
        double length = scanner.nextDouble();//输入高
        //final double p = 3.14159;//定义一个常量p(圆周率)
        double area = radius * radius * 3.14159;
        double volume = area * length;
        System.out.println("The area is " + area);
        System.out.println("The volume is " + volume);
    }
}

运行结果:

The area is 95.0330975
The volume is 1140.39717

 

思路分析:这道题是让用户输入0~1000之间的一个整数,然后将该整数的各位数字相加求和,具体来看。

第一步:创建一个整型number变量,然后提示用户输入number,例如是一个三位数;

第二步:整个计算过程中用到的原理就是:利用%来分解数字,然后再用 / 去掉分解的数字;

第三步:首先创建一个变量a,然后让a=number%10,这样就把个位数字取出来了,然后去掉个位数字,num /= 10;

第四步:创建一个变量b,让b=number%10,这就取出十位数字,然后去掉十位数字,num /= 10;

第五步:创建一个变量c,让c=number%10,这就取出百位数字;

第六步:创建一个变量sum,让sum=a+b+c,这样就求出它们的和;

第七步:编写代码。

import java.util.Scanner;
public class Demo02_03{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number between 0 and 1000: ");
        int num = scanner.nextInt();
        int a = num % 10;//取出个位
        num /= 10;
        int b = num % 10;//取出十位
        num /= 10;
        int c = num % 10;//取出百位
        int sum = a + b + c;
        System.out.println("The sum of digits is " + sum);
    }
}

运行结果:

Enter a number between 0 and 1000: 999
The sum of digits is 27

 

结合这个方法,我们可以让用户输入自己所在时区,然后输出所在时区的时间,请编写代码。

思路分析:这个题挺有意思的,我们只需要使用这个方法获得毫秒数,然后一步一步计算就可以了。具体方法详见代码:

import java.util.Scanner;
public class Demo02_04{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the time zone offset to GMT: ");
        int Offset = scanner.nextInt();
        //总毫秒数
        long totalMilliseconds = System.currentTimeMillis();
        //总秒数
        long totalSeconds = totalMilliseconds / 1000;
        //当前秒数
        long currentSecond = totalSeconds % 60;
        //总分钟数
        long totalMinutes = totalSeconds / 60;
        //当前分钟数
        long currentMinute = totalMinutes % 60;
        //总小时数
        long totalHours = totalMinutes / 60;
        //当前小时数
        long currentHour = (totalHours + 8) % 24;
        System.out.println("Current time is " + currentHour + ":"
        + currentMinute + ":" + currentSecond);
    }
}

运行结果:中国在东八区,所以输入8,我运行的时候时间是17:41:1,所以它返回的也就是这个时间。

Enter the time zone offset to GMT: 8
Current time is 17:41:1

 

 

 思路分析:每个月向银行账户存100$,然后计算六个月后账户上的钱数,这个其实就是每一个月在前一个月的基础上进行计算,具体来看。

第一步:首先创建一个变量monthDeposit用来表示每月的存款,提示用户输入monthDeposit;

第二步:创建一个变量accountValue用来表示用户账户上面的钱数,初始值等于第一个月存入的钱,也就是monthDeposit;

第三步:利用题目当中给出的方法进行计算,第一个月就等于accountValue = accountValue * (1 + 0.00417),第二个月是在第一个月的基础上进行计算,就是accountValue = (accountValue + monthDeposit) * (1 + 0.05 / 12),以此类推......;

第四步:编写代码。

import java.util.Scanner;
public class Demo02_05{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter monthly saving amount: ");
        double monthDeposit = scanner.nextDouble();   //monthDeposit表示月存款
        double accountValue = monthDeposit;
        accountValue = accountValue * (1 + 0.00417);//第一个月
        accountValue = (accountValue + monthDeposit) * (1 + 0.05 / 12);//第二个月
        accountValue = (accountValue + monthDeposit) * (1 + 0.05 / 12);//第三个月
        accountValue = (accountValue + monthDeposit) * (1 + 0.05 / 12);//第四个月
        accountValue = (accountValue + monthDeposit) * (1 + 0.05 / 12);//第五个月
        accountValue = (accountValue + monthDeposit) * (1 + 0.05 / 12);//第六个月
        System.out.println("After the sixth month, the account value is " + accountValue);
    }
}

运行结果:

Enter monthly saving amount: 100
After the sixth month, the account value is 608.81

 

 

 思路分析:这道题就是输入两个点的坐标,然后让我们计算两点间的距离,计算方法题目中已经给出,直接计算就好,具体来看。

第一步:创建四个变量x1,y1,x2,y2,分别表示两个点的坐标,然后分别提示用户输入两个点,输入一个点之后换行;

第二步:创建一个变量distance,用来表示两点间的距离,然后根据题目中给出的公式进行计算,就是double distance = Math.pow((x2 - x1) * (x2 - x1) +(y2 - y1) * (y2 - y1), 0.5);

第三步:编写代码。

import java.util.Scanner;
public class Demo02_06{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        /*输入第一个点*/
        System.out.print("Enter x1 and y1: ");
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        /*输入第二个点*/
        System.out.print("Enter x2 and y2: ");
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();
        double distance = Math.pow((x2 - x1) * (x2 - x1) +(y2 - y1) * (y2 - y1), 0.5);//计算两点间距离
        System.out.println("The distance of the two points is " + distance);
    }
}

运行结果:

Enter x1 and y1: 1.5 -3.4
Enter x2 and y2: 4 5
The distance of the two points is 8.764131445842194

 

思路分析:这道题是让我们计算三角形的面积,其实是建立在上一道题的基础上来进行计算的,用的是海伦公式。按照上一道题的计算方法算出三条边的长度,然后按照海伦公式进行计算,具体来看:

第一步:创建六个变量x1,y1,x2,y2,x3,y3用来表示三个点的坐标,提示用户输入三个点;

第二步:创建三个变量side1,side2,side3,分别表示三角形三条边的长度,按照上一道题的计算方法计算三条边的长度;

第三步:创建一个变量s=(side1+side2+side3) / 2,然后按照题目当中给出的公式来计算三角形面积;

第四步:编写代码。

import java.util.Scanner;
public class Demo02_07{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter three points for a triangle: ");
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();
        double x3 = scanner.nextDouble();
        double y3 = scanner.nextDouble();
        /*计算三条边 */
        double side1 = Math.pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5);
        double side2 = Math.pow((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1), 0.5);
        double side3 = Math.pow((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2), 0.5);
        /*计算s和面积 */
        double s = (side1 + side2 + side3) / 2;
        double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
        System.out.println("The area of the triangle is " + area); 
    }
}

运行结果:

Enter three points for a triangle: 1.5 -3.4 4.6 5 9.5 -3.4
The area of the triangle is 33.6

 

好了,以上就是关于“基本数据与计算”这方面的习题了,后续我还会讲到很多知识点的相关习题,欢迎大家关注!!! 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值