Java程序设计编程题目
一般题:
1、 编写一个应用程序,对程序中给定的四个double型数据求其最大值和最小值。
import java.util.*;
public class no1
{
public static void main(String args[])
{
System.out.println("input the number:");
Scanner input = new Scanner(System.in);
double[] x=new double[4];
for (int i=0;i<4;i++)
x[i]=input.nextDouble();
double max=Math.max(Math.max(x[0], x[1]), Math.max(x[2], x[3]));
double min=Math.min(Math.min(x[0], x[1]), Math.min(x[2], x[3]));
System.out.println("max:"+max+" min:"+min);
}
}
2、 一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6的因子为1、2、3,而6=1+2+3。因此6是“完数”。编写一个小应用
程序求1000之内的所有完数
public class no2
{
public static void main(String args[])
{
int sum;
for (int i=1;i<=1000;i++)
{
sum=0;
for (int j=1;j<=i/2;j++)
{
if (i%j==0) sum+=j;
}
if (sum==i)
{
System.out.println(i);
}
}
}
}
3、 要求1000!(1000*999*998...*2*1)的值。
import java.math.BigInteger;
public class no3
{
public static void main(String args[])
{
BigInteger x = BigInteger.valueOf(1);
for(int i=2; i<=1000; i++) {
x = x.multiply(BigInteger.valueOf(i));
}
System.out.println(x);
}
}
4、
设计一个能随机产生100个大写英文字母的方法,在该方法中统计产生了多少个元音字母,并输出这个数字。
import java.util.*;
public class no4
{
public static void main(String args[])
{
int a=(int)'A';
int y[]=new int[26];
int t=0;
Random r=new Random();
for (int i=0;i<100;i++)
{
t=Math.abs(r.nextInt())%26;
y[t]++;
System.out.print((char)(a+t)+" ");
}
System.out.println("");
for (int yi=0;yi<26;yi++)
{
System.out.print((char)(a+yi)+" "+y[yi]+" ");
}
}
}
5、 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第3个月后每个月又生一对兔子,假如兔子不死,问每个月的兔子总数为多少?
public class no5
{
public static void main(String args[])
{
int a=0,b=0,c=2;
int x,y,z;
for (int i=0;i<24;i++)
{
z=a/2;
y=c;
x=a+b;
a=x;
b=y;
c=z;
System.out.println(a+b+c);
}
}
}
6、 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:
153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
public class no6
{
public static void main(String args[])
{
int y;
int t;
for (int x=100;x<=999;x++)
{
t=x;
y=0;
while(t>=1)
{
y+=Math.pow(t%10,3);
t/=10;
}
if (y==x) System.out.println(x);
}
}
}
7、 输入一行字
符,分别统计出其中英文字母、空格、数字和其它字符的个数。
import java.util.Scanner;
public class no7
{
public static void main(String args[])
{
int y[]=new int[4];
char x;
Scanner input=new Scanner(System.in);
String s=input.nextLine();
int l=s.length()-1;
while(l>=0)
{
x= s.charAt(l);
if ((x-'a'>=0&;&;x-'a'<26) || (x-'A'>=0&;&;x-'A'<26))
{
y[0]++;
}
else
if (x-'0'<10&;&;x-'0'>0)
{
y[1]++;
}
else
if (x==' ')
{
y[2]++;
}
else
y[3]++;
l--;
}
for (int i=0;i<4;i++)
{
System.out.println(y[i]);
}
}
}
8、 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
public class no8
{
public static void main(String args[])
{
dum(100,0,10);
}
public static void dum(int h,int s,int t)
{
s+=h;
h/=2;
t--;
if (t>0) dum(h,s,t);
else System.out.print