Skip to content

Instantly share code, notes, and snippets.

@zeqing-guo
Last active August 29, 2015 14:08
Show Gist options
  • Save zeqing-guo/a7007deaff983b33a1e0 to your computer and use it in GitHub Desktop.
Save zeqing-guo/a7007deaff983b33a1e0 to your computer and use it in GitHub Desktop.

Lab3说明

刚改完大家提交的Lab3,这里说一下Lab说出现的问题。

编程风格

大多数同学的编程风格都非常得不好,虽然大家写得比较多了之后会慢慢好起来,但请大家从现在开始就养成一些好习惯。

乱打括号、胡乱空格空行的代码非常不利于大家debug,现在代码量比较小大家可能没体会,但当开始做pj会大家就会逐渐明白有一个良好的编程风格的重要性。与此同时,有一个良好的编程风格也是大家今后进入一流IT企业的必要基础,因为大多数IT巨头都很重视code review。

希望大家能重视编程风格,下面是我的一些具体建议:

  • 使用对程序员友好的编辑器,或者使用IDE编程。这类软件大多有自动整理代码的功能,就Eclipse而言,在Windows下快捷键是Shift + Ctrl + F,在Mac OS X下是shift + command + F
  • 请认真阅读Google Java编程风格指南,空格这类细节大家很容易忽略(TA虽然不是处女座,但看到iffor后面不加空格直接加括号还是会觉得挺别扭的=。=)

代码

我觉得大家的代码都写得挺好的,特别是很多接触编程没多久的孩纸能写这么好真的挺不错的。这里针对大家代码中的一些小问题给大家一些建议:

  • 大家不必在main函数的结尾加上System.exit(),这个不是必须的。当然大家也不用特意加上System.out.println("Process finished with exit code 0");这类语句。
  • 当一部分代码看上去是重复的,或者呈等差数列增加或减少时大家需要考虑能不能把它写成forwhile循环。
  • 遇到问题大家可以Google,少用度娘,度娘查中文还是挺好的,但查中英夹杂和纯英文不太准确。
  • 然后有些同学交代码前最好先看看代码有没有语法错误,能不能编译通过,TA也不想debug啊T^T

Lab3的答案

下面是三个问题的参考代码:

Approximate calculation of $e$

public class Lab3_1 {
	public static void main(String[] args){
		double e = 1;
		
		// Compute
		for (int i = 100000; i > 0; --i) {
			e = (1 + e) / i;
		}
		++e;
		
		System.out.println("e = " + e);
	}
}

A star pyramid

import java.util.Scanner;

public class Lab3_2 {
	public static void main(String[] args) {
		int n = 0;
		boolean flag = true;

		// Read the input and check
		Scanner input = new Scanner(System.in);
		while (flag) {
			System.out.println("Input a positive integer:");
			String strN = input.next();

			try {
				n = Integer.parseInt(strN);
				if (n > 0) {
					flag = false;
				} else {
					System.out.println("Invalid input!");
				}
			} catch (NumberFormatException e) {
				System.out.println("Invalid input!");
			}
		}

		// Draw
		for (int i = 1; i <= n; ++i) {
			int blankNum = n - i;
			for (int j = 0; j < blankNum; ++j) {
				System.out.print(" ");
			}
			for (int j = 0; j < i; ++j) {
				System.out.print("* ");
			}
			System.out.println();
		}

		// Close input
		input.close();
	}
}

Palindrome Number

import java.util.Scanner;

public class Lab3_3 {
	public static void main(String[] args) {
		int n = 0;
		int copyN = 0;
		int divisorHigh = 1;
		int divisorLow = 1;
		boolean flag = true;
		boolean isPalindrome = true;
		
		// Read the input and check
		Scanner input = new Scanner(System.in);
		while (flag) {
			System.out.println("Input a positive integer:");
			String strN = input.next();

			try {
				n = Integer.parseInt(strN);
				if (n > 0) {
					flag = false;
				} else {
					System.out.println("Invalid input!");
				}
			} catch (NumberFormatException e) {
				System.out.println("Invalid input!");
			}
		}
		
		// Compute
		copyN = n;
		while (copyN > 9) {
			divisorHigh *= 10;
			copyN /= 10;
		}
		
		// Judge
		while (divisorLow < divisorHigh && isPalindrome) {
			int digitHigh = (n / divisorHigh) % 10;
			int digitLow = (n / divisorLow) % 10;
			
			if (digitHigh != digitLow) {
				isPalindrome = false;
			} else {
				divisorLow *= 10;
				divisorHigh /= 10;
			}
		}
		
		if (isPalindrome) {
			System.out.println(n + " is a palindrome number.");
		} else {
			System.out.println(n + " is not a palindrome number.");
		}
		
		// Close input
        input.close();
	}
}

嗯,半夜写的,错了别怪我=w=

大家还有什么问题的话请直接发邮件给我,我的邮箱是zqguo(at)zqguo.com(把at换成@)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment