天天看點

2013年12月21日程式聯系題(初級)

初級

1. 寫程式将” Hello World”列印到螢幕。

package org.com.rainplus;

public class HelloWorld {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.print("hello java world,welcome to my life!");
    }

}
           

2. 寫程式輸入使用者的姓名并用該姓名和他打招呼。

package org.com.rainplus;

import java.util.Scanner;


public class SayHello {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("please enter your name:");
		System.out.println("hello,"+scanner.nextLine()+",welcome to java world!");
	}

}
           

3. 修改上一個程式,使得僅可以與Alice和Bob這兩個使用者用其姓名與之打招呼。

4. 寫程式輸入一個數n并列印出從1到n的和。

package org.com.rainplus;

import java.util.Scanner;

public class Sum {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		main: while (true) {
			long sum = 0;

			System.out.println("please enter the number what you want:");
			try {
				long i = Integer.parseInt(input.nextLine());

				for (int j = 1; j <= i; j++) {
					sum = sum + j;
				}
			} catch (NumberFormatException e) {
				System.out.println("bigdata out of bignumber!");
				continue main;
			}

			System.out.println("The sum of the number is :" + sum);
		}
	}

}
           

5. 修改上個程式,使得求和的數隻包含3或5的倍數,例如n=17,則求和的數為:3, 5, 6, 9, 10, 12, 15。

package org.com.rainplus;

import java.util.Scanner;

public class Sum {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		main: while (true) {
			long sum = 0;

			System.out.println("please enter the number what you want:");
			try {
				long i = Integer.parseInt(input.nextLine());

				for (int j = 1; j <= i; j++) {
					if (j % 5 == 0 || j % 3 == 0) {
						sum = sum + j;
						System.out.print(j+" ");
					}
				}
			} catch (NumberFormatException e) {
				System.out.println("\nbigdata out of bignumber!");
				continue main;
			}

			System.out.println("The sum of the numbers is :" + sum);
		}
	}

}
           

6. 寫個程式,要求使用者輸入一個數n,并機率性的選擇是計算1到n的和還是計算1到n的乘積。

7. 寫程式列印出12×12乘法表。

package org.com.rainiplus;

public class MulTable {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int i = 1; i <=12; i++) {
			for (int j = 1; j <=i; j++) {
				System.out.print(i+"*"+j+"+="+i*j+"   ");
			}
			System.out.println();
		}

	}

}
           

8. 寫程式列印所有的素數。(注意:如果你用的程式設計語言不支援任意大小的數,那麼列印出所有你能表示的素數,包括最大數)

9. 寫一個競猜遊戲,使用者必須猜一個秘密的數字,在每次猜完後程式會告訴使用者他猜的數是太大了還是太小了,直到猜測正确,最後列印出猜測的次數。如果使用者連續猜測同一個數字則隻算一次。

package org.com.rainplus;

import java.util.Random;
import java.util.Scanner;

import javax.swing.text.StyledEditorKit.ForegroundAction;

import sun.security.x509.IPAddressName;

public class GuessNumber {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Random random = new Random();
		Scanner scanner = new Scanner(System.in);
		int num = random.nextInt(100000);
		main: while (true) {
			System.out.println("pealse enter your number:"+num);
			int input = Integer.parseInt(scanner.nextLine());
			if (input == num) {
				System.out.println("congration you! you're winner!");
				continue main;
			} else if (input > num) {
				System.out.print("你猜的數字大了!");
			} else {
				System.out.print("你猜的數字xiao了!");
			}
		}
	}

}
           

10. 寫個程式列印出接下來的20個閏年。

package org.com.rainplus;

public class LeapYear {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int count = 0;
		for (int i = 2013; i < Integer.MAX_VALUE; i++) {
			if (i % 4 == 0) {
				if (i % 100 == 0 && i % 400 == 0) {
				} else {
					System.out.print(i + " ");
					count++;
					if (count >= 600) {
						break;
					}
				}
			}
		}

	}
}
           

11. 寫程式計算: