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+"次");
}
}
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+"次");
}
}
相关文章推荐
- JAVA初学基本简单的程序----适合初学者(二)
- 【疑问1】java程序中JDBC工具类(采用c3p0配置文件)中的疑问?适合初学者
- JAVA初学基本简单的程序----适合初学者(一)
- java实现最简单的web聊天室程序源代码,适合初学者
- Java 程序编码规范(初学者要牢记)
- Java基础视频教程(最适合初学者入门)
- JRE 安装和配置,以适合JAVA程序运行所具备的环境[zt]
- JXL介绍 Java操作Excel--读取Excel(简单步骤,适合初学者)
- JAVA练习题系列(2 ) 关于for嵌套循环的!最适合初学者!
- Java视频教程(最适合Java初学者的经典入门视频)_讲 师:张孝祥
- java基础关于Javac.exe与java.exe(适合与初学者)
- 豆瓣电影搜索爬虫程序,爬虫小程序,适合初学者
- 仅900行的Java简易《炸弹人》(无AI,适合初学者源码剖析)
- java中最常用jar包的用途说明,适合初学者
- Java 程序编码规范(初学者要牢记)
- Java内存空间划分初识(适合初学者)
- 常用ASP脚本程序集锦(适合初学者)
- 通过游戏学java(适合初学者)
- java 利用注释和反射写一个简单的SQL语句拼接程序,很简单初学者
- 初学者dos界面编译运行Java程序