# Summary of basic data and calculation exercises

Keywords: Java

Hello, everyone! I'm lovely and charming again. Today, let's talk about the exercises on "basic data and calculation". This time, it's a little more difficult than last time, but it's also quite simple.

Thought analysis: we can see that this question is different from the previous one. It doesn't let us directly output, but requires us to read in the double type Celsius temperature from the console, then convert it to Fahrenheit temperature, and finally display the results. Let's see in detail.

Step 1: since you want to read data from the console, you need to use input, so you need to import the input package in Java, import java.util.Scanner; next, we need to prompt the user to input the data of Celsius temperature from the keyboard;

Step 2: there are two variables, Fahrenheit temperature and centigrade temperature, and the temperature has decimals. First, we have to create two double variables. The centigrade temperature is read directly from the console. The Fahrenheit temperature needs to be calculated according to the read centigrade temperature;

Step 3: the relationship between them has been given in the title, so we can do it directly;

Step 4: write code.

```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();//Create a variable of type double, Celsius
double Fahrenheit = (9.0 / 5) * Celsius + 32;//Calculate Fahrenheit temperature
System.out.println(Celsius + " Celsius" + " is " + Fahrenheit + " Fahrenheit");
}
}```

Operation result:

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

Train of thought analysis: this problem is to get two data from the console, namely, the radius and height of the cylinder, and use the formula given in the topic to calculate the area and volume of the cylinder to have a specific look.

Step 1: because radius and height are not necessarily integers, we first create two double variables to represent the radius and height of the cylinder. Then prompt the user to input the radius and height from the keyboard;

Step 2: calculate according to the formula given in the title;

Step three: write code.

```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 length = scanner.nextDouble();//Input high
//final double p = 3.14159; / / define a constant P (PI)
double volume = area * length;
System.out.println("The area is " + area);
System.out.println("The volume is " + volume);
}
}```

Operation result:

```The area is 95.0330975
The volume is 1140.39717```

Train of thought analysis: this problem is to let the user input an integer between 0 and 1000, and then add all the numbers of the integer to sum, specifically.

Step 1: create an integer number variable, and then prompt the user to enter number, for example, a three digit number;

Step 2: the principle used in the whole calculation process is: use% to decompose the number, and then use / remove the decomposed number;

Step 3: first create a variable a, and then let a=number%10, so that the number of bits is taken out, and then remove the number of bits, num / = 10;

Step 4: create a variable b, let b=number%10, take out the ten digit number, and then remove the ten digit number, num / = 10;

Step 5: create a variable c, let c=number%10, and then take out the hundred digit number;

Step 6: create a variable sum, let sum=a+b+c, and then find their sum;

Step 7: write code.

```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;//Take out a bit.
num /= 10;
int b = num % 10;//Take out ten bits.
num /= 10;
int c = num % 10;//Remove 100 bits
int sum = a + b + c;
System.out.println("The sum of digits is " + sum);
}
}```

Operation result:

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

Thought analysis: deposit \$100 into the bank account every month, and then calculate the amount of money on the account six months later. This is actually to calculate every month on the basis of the previous month, specifically.

Step 1: first, create a variable, monthDeposit, to represent the monthly deposit, and prompt the user to enter monthDeposit;

Step 2: create a variable accountValue to represent the amount of money on the user account. The initial value is equal to the money deposited in the first month, that is, the month deposit;

Step 3: use the method given in the title to calculate, the first month is equal to accountValue = accountValue * (1 + 0.00417), the second month is calculated on the basis of the first month, that is, accountValue = (accountValue + monthdeposit * (1 + 0.05 / 12), and so on;

Step 4: write code.

```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();   //Monthly deposit means monthly deposit
double accountValue = monthDeposit;
accountValue = accountValue * (1 + 0.00417);//First month
accountValue = (accountValue + monthDeposit) * (1 + 0.05 / 12);//Second months
accountValue = (accountValue + monthDeposit) * (1 + 0.05 / 12);//Third months
accountValue = (accountValue + monthDeposit) * (1 + 0.05 / 12);//Fourth months
accountValue = (accountValue + monthDeposit) * (1 + 0.05 / 12);//Fifth months
accountValue = (accountValue + monthDeposit) * (1 + 0.05 / 12);//Sixth months
System.out.println("After the sixth month, the account value is " + accountValue);
}
}```

Operation result:

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

Train of thought analysis: this problem is to input the coordinates of two points, and then let's calculate the distance between two points. The calculation method has been given in the problem, so it's better to directly calculate. Let's see specifically.

Step 1: create four variables x1, y1, x2, y2 to represent the coordinates of the two points respectively, and then prompt the user to enter two points respectively, enter a point and wrap the line;

Step 2: create a variable distance to represent the distance between two points, and then calculate it according to the formula given in the title, that is, double distance = Math.pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5;

Step three: write code.

```import java.util.Scanner;
public class Demo02_06{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
/*Enter first point*/
System.out.print("Enter x1 and y1: ");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
/*Enter second point*/
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);//Calculate the distance between two points
System.out.println("The distance of the two points is " + distance);
}
}```

Operation result:

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

Train of thought analysis: this problem is to let us calculate the area of triangles, in fact, it is based on the previous problem, using Helen formula. Calculate the length of the three sides according to the calculation method of the previous question, and then calculate according to Helen's formula. For details:

Step 1: create six variables x1,y1,x2,y2,x3,y3 to represent the coordinates of three points, and prompt the user to enter three points;

Step 2: create three variables, side1, side2, side3, to represent the length of three sides of a triangle, and calculate the length of the three sides according to the calculation method of the previous problem;

Step 3: create a variable s=(side1+side2+side3) / 2, and then calculate the triangle area according to the formula given in the title;

Step 4: write code.

```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();
/*Calculate three sides */
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);
/*Calculate s and area */
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);
}
}```

Operation result:

```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```

Well, the above is about "basic data and calculation" exercises. I will talk about many related exercises of knowledge points later. Welcome to pay attention!!!

Published 5 original articles, won praise 9, visited 534

Posted by zarp on Wed, 05 Feb 2020 23:30:39 -0800