您的位置:首页 > 移动开发 > 微信开发

java小程序 适合初学者

2011-04-08 13:03 417 查看
1.各种循环

public class Cycle {
public static void main(String args[]) {
int i = 1, j = 1, k = 1, sum = 0, sum1 = 0, sum2 = 0;
{
for (i = 1; i <= 100; i++)
sum += i;
System.out.println(sum);
}
{
while (j <= 100) {
sum1 += j;
j++;
}
System.out.println(sum1);
}
{
do {
sum2 = sum2 + k;
k++;

} while (k <= 100);
System.out.println(sum2);
}
}
}

2.求阶乘 以及阶乘的和

public class Factorial {
public static void main(String args[]) {
int i, j, s, sum = 0;
for (i = 1; i <= 10; i++) {
s = 1;
for (j = 1; j <= i; j++)
s = s * j;
System.out.print(i + "!=" + s + " ");
sum += s;
}
System.out.println();
System.out.print("1!+2!+……+" + (i - 1) + "!=" + sum);
}
}

3.Fibonacci 序列

public class Fibonacci {
public static void main(String args[]){
long f1=1,f2=1;
for(int i=1;i<=10;i++){
System.out.print(f1+" "+f2+" ");
f1=f1+f2;
f2=f1+f2;
}
}
}

4.显示日期

public class MyDate {
private int day=12;
private int month=6;
private int year=1900;
public MyDate(int d,int m,int y){
year=y;
month=m;
day=d;
}
public void display(){
System.out.println(year+"/"+month+"/"+day);
}
public static void main(String args[]){
MyDate m;
m=new MyDate(22,9,2001);
m.display();
}

}

5.模拟摇奖机 36选7

public class MyMain {
public static void main(String[] args) {
int[] array = new int[36];
for (int i = 0; i < array.length; i++) {
array[i] = i + 1;
}
for (int i = 0; i < 7;) {
int m = (int) (Math.random() * 36);
if (array[m] == 0) {
continue;
} else {
System.out.print(array[m] + " ");
array[m] = 0;
i++;
}
}
}
}

6.三个数排序

public class Select {
public static void main(String args[]) {
int z, x, y, m = 0;
x = 19;
y = 16;
z = 21;
if (y < x) {
m = x;
x = y;
y = m;
}
if (z < y) {
m = y;
y = z;
z = m;
}
if (z < x) {
m = x;
x = z;
z = m;
}
System.out.println(+x + " " + y + " " + z);
}
}

7.求abcde*4=edcba

public class Ti {
public static void main(String arg[]) {
int a, b, c, d, e, i;
for (i = 99999; i > 10000; i--) {
a = i / 10000;
b = (i % 10000) / 1000;
c = (i % 1000) / 100;
d = (i % 100) / 10;
e = i % 10;
if (a == (i * 4) % 10 && b == ((i * 4) % 100) / 10
&& c == ((i * 4) % 1000 / 100)
&& d == ((i * 4) % 10000) / 1000 && e == (i * 4) / 10000)
System.out.println("输出" + i);
}
}
}
8.打个❤的形状

public class Heart {
public static void main(String args[]) {
String a[][] = new String[5][7];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 7; j++) {
if(i==0&&(j==0||j==3||j==6)){
a[i][j]=" ";
}else
if(i==2&&(j==0||j==6)){
a[i][j]=" ";
}else
if(i==3&&(j==0||j==1||j==5||j==6)){
a[i][j]=" ";
}else
if(i==4&&j!=3){
a[i][j]=" ";
}else{a[i][j] = "* ";}
}
}

fun(a);

}
public static void fun(String a[][]){
for(int i=0;i<5;i++){
for(int j=0;j<7;j++){
System.out.print(a[i][j]);
}
System.out.println("");
}
}
}
9.空心◇

public class Kong {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 9;
int b = (a-1)/2;
String s[][] = new String[a][a];
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
s[i][j] = " ";
}
}
for (int k = 0; k < a; k++) {
if (b >= 0) {
s[k][b] = "*";
s[k][a - b - 1] = "*";
b--;
}
if (b < 0) {
s[k][-b-1] = "*";
s[k][a + b] = "*";
b--;
}
}

for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
System.out.print(s[i][j]);
}
System.out.println();
}
}

}

10.最大最小数

public class MaxMin {
public static void main(String args[]) {
int a[] = { 78, 89, 76, 86, 95, 100, 63, 87 };
int max = 0, min = 0;
max = min = a[0];
for (int i = 0; i < a.length; i++) {
if (max < a[i]) {
max = a[i];
}
if (min > a[i]) {
min = a[i];
}
}
System.out.println(max + "dd" + min);
}

}
11.冒泡排序

public class Paixu {
public static void main(String args[]) {
int a[] = { 78, 89, 76, 86, 95, 100, 63, 87 };
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i] < a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + "/t");
}
}
12.汉诺塔HanoiTower】

public class HanoiTower {
static int i = 0;
static void moves(char a, char c) {
System.out.println("Form " + a + " to " + c);
}
static void hanoi(int n, char a, char b, char c) {
if (n == 1) {
moves(a, c);
i++;
} else {
hanoi(n - 1, a, c, b);
moves(a, c);
i++;
hanoi(n - 1, b, a, c);
}
}
public static void main(String args[]) {
int n = 5;
hanoi(n, 'A', 'B', 'C');
System.out.println("一共需要移动"+i+"次");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: