初级
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. 写程序计算: