Day03 - salary conversion

Keywords: Java

preface

        hello everyone! Today is Day03. Nice to meet you again

I wonder if you have ever encountered such a situation. When we need to calculate a large amount of data, it will always be very troublesome and cumbersome, especially the calculation of data related addition, subtraction, multiplication and division and average value. When we use calculators for calculation, it will be annoying, especially when the number of data is increasing. So how should we solve this problem?

I believe the students who have come into contact with computers may have thought that, yes, that is the program. The reason why the program is convenient is that after we input the data, he will process the data according to our designed logic. So today, one of our small tasks is to make a small tool for salary conversion and master some relevant knowledge about Java

  So what knowledge points do we need in Java to make this salary conversion program?

Let's think about how to design this program:

catalogue

preface

How to design

1, First, get the data

2, We need to process the input data

3, Simply output the processed results

Code design and result display

1, Code

2, Result display

  summary

How to design

1, First, get the data

So how to get data in Java?

In the Java language, the easiest way to enter data from the keyboard is to use the scanner class

So how to use it? What is his grammar? Here is the code to use the scanner class

import java.util.Scanner;            //This is the package that needs to be imported



Scanner input = new Scanner(System.in);        //This is the statement that creates the scanner class

double monthmoney = input.nextDouble();        //This statement is the key statement to obtain keyboard input data, in which the type after next is the data type to be obtained

input.close();                                //Finally, call this method to close the flow.

The above is the simplest way to get data from the keyboard

So is there any other way? The answer is yes

Then, the following other methods of reading data from the keyboard are introduced:

1. Read a single character:
Read the next byte of data from the input stream and return the int byte value in the range of 0 ~ 255. If it reaches the end of the input stream, it returns - 1. Therefore, when reading char type, you need to convert int type to char type

char c = (char)System.in.read();
System.out.println(c);

2. Read one line:
BufferedReader reads text from the character input stream and buffers each character, so as to achieve efficient reading of characters, arrays and lines. You can specify the size of the buffer, but in most cases, the default value is sufficient

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
System.out.println(str);

2, We need to process the input data

        In Java, we can use Java statements to process data, and we can create methods to process data

Here, I created two methods to obtain daily salary and annual salary. The specific code is as follows:

//This is the method of getting daily salary
public static double getday(double money) {
		return money/22;
	}


//This is the method of obtaining annual salary
public static double getyear(double money) {
		return money*13;
	}

3, Simply output the processed results

        How to output data in Java?

In Java, System.out.println(); The specific format of data output is as follows:

System.out.println("");                //This statement is a statement that outputs data

Code design and result display

         1, Code

                  Don't say much, just go to the code

                         The code I designed is as follows:

package test;

import java.util.Scanner;

public class Day03 {

	public static void main(String[] args) {
		// Method stub automatically generated by TODO
		System.out.println("*****Salary conversion gadget version1.0*****");
		System.out.println("Please enter your monthly salary(RMB):");
		Scanner input = new Scanner(System.in);
		double monthmoney = input.nextDouble();
		double daymoney=0,yearmoney=0;
		daymoney = getday(monthmoney);
		yearmoney = getyear(monthmoney);
		System.out.println("Your daily salary:"+daymoney);
		System.out.println("Your annual salary:"+yearmoney);
		System.out.println("*****************************");
		input.close();
	}
	public static double getday(double money) 
	{
		return money/22;
	}
	public static double getyear(double money) {
		return money*13;
	}
}

2, Result display

                 If you don't say much, go straight to the screenshot:

                 So far, the code design of the whole salary conversion has been completed

  summary

        Today's task is relatively simple, but the simple task also learned some knowledge. At the same time, it gained a lot by reviewing the Java knowledge learned before. This task is very simple, but it still needs to be analyzed carefully. Analyze how to do each step, What methods can be adopted to make your thinking active. It is more conducive to the analysis of complex events later. Moreover, when analyzing, you can consider analyzing problems from multiple angles, which can often better solve problems

Posted by invinate on Thu, 28 Oct 2021 11:33:43 -0700