Beginner programming, ideas are clear, but can't write the code?

Keywords: Python Java Programming

The initial contact programming can be directly used. Instead of the large piles of books on the front floors, they may not be able to help you write code. If you don't say, they may directly persuade you to retreat.

First, look at the problem. It shows that you have ideas and can figure out all the problems. Then there is a very simple method:

Record the implementation ideas while analyzing them

(when analyzing the implementation ideas, write while analyzing, and write in Chinese, the more detailed the better), for example:

  //What is the first step
  //Section 1.1 what to do
  //Section 1.2 what to do
  //....
  //The second step is what to do
  //Section 2.1 what to do
  //Section 2.2 what to do 
  //....
  //... and so on 

When you encounter something you can't write down, see if there is a problem with your thinking. If there is no thinking, brush your thinking again.

This forces everyone to think about the code at any time and think carefully.

When all the ideas are clear, write the comments according to the ideas and steps, and then write the code. If you only know the first few steps, it doesn't matter. You can write as much code as you can think of a clear idea. Take your time

For example, let's first talk about the learning ideas suitable for beginners, and then say a simple comprehensive case. Please refer to the following contents according to your learning progress.

Case 1: array traversal summation

Requirements:
The sales of 5 employees in a department are 16, 26, 36, 6 and 100 respectively. Please calculate the total sales of their department.

analysis:
Step 1: take these five data to the program - > use array

int[] money = {16, 26, 36, 6, 100};

Step 2: traverse each data in the array, and then define summation variables outside to add them up.

int sum = 0;
for (int i = 0; i < money.length; i++) {
     // i = 0 1 2 3 4
     sum += money[i];
 }

Case 2: buying air tickets

Requirements:

The ticket price is charged according to the off-season and peak season, first-class and economy class. Enter the original ticket price, month and first-class or economy class
The fare shall be calculated according to the following rules:
From May to October, 10% off for first class and 8.5% off for economy class;
From November to April, 7% off for first class and 6.5% off for economy class in the off-season;

analysis:

Define a method for keyboard entry of original ticket price, month and cabin type.
Use if to judge whether the month is a peak season or a low season, and use the switch branch to judge whether it is first class or economy class.
Select the corresponding discount to calculate and return the calculation result.

Write code

package com.itheima;

import java.util.Scanner;

/**
     Demand: the ticket price is charged according to the off-season and peak season, first-class and economy class. Enter the original ticket price, month and first-class or economy class.
          Calculate the ticket price according to the following rules: 10% off for first class in peak season (may October), 8.5% off for economy class, 7% off for first class and 6.5% off for economy class in off-season (November to next April).
 */
public class Test1 {
    public static void main(String[] args) {
        // 3. Enter the purchase information and call the method to get the final result
        Scanner sc = new Scanner(System.in);
        System.out.println("Original ticket price:");
        double price = sc.nextDouble();
        System.out.println("month:");
        int month = sc.nextInt();
        System.out.println("Bin type (first class, economy class):");
        String type = sc.next();

        double rs = calc(price, month, type);
        System.out.println("Your current ticket price is:" + rs);
    }

    /**
        1,Define a method: return value type declaration of formal parameters (original price, month, first class economy class): double
     */
    public static double calc(double money, int month, String type){
        // 2. Judge whether the month is off-season or peak season
        if(month >= 5 && month <= 10){
            // busy season
            switch (type){
                case "economy class":
                    money *= 0.85;
                    break;
                case "First class":
                    money *= 0.9;
                    break;
                default:
                    System.out.println("The bin you entered is incorrect~~");
                    money = -1; // The price cannot be calculated at present!
            }
        }else if(month == 11 || month == 12 || month >= 1 && month <= 4){
            switch (type){
                case "economy class":
                    money *= 0.65;
                    break;
                case "First class":
                    money *= 0.7;
                    break;
                default:
                    System.out.println("The bin you entered is incorrect~~");
                    money = -1; // The price cannot be calculated at present!
            }
        }else {
            System.out.println("Month has a problem");
            money = -1;
        }

        return money;
    }
}

If you still can't write code after the above practice, this usually happens to beginners. In fact, there is only one reason is that there is too little code. Use the above method to write more flexible, analyze the writing ideas again, and finally write code.

When most people can't write code, they just write it first and write as much as they can.

Posted by oceans on Fri, 12 Nov 2021 13:56:07 -0800