java 技术题_Java 基础题目

1、请编写一个Application,其功能为:取一个小于800的正随机整数,将其以如下形式输出:

所取随机数为:***

它的百位数字为:*

它的十位数字为:*

它的个位数字为:*

@程序

//import java.io.*;

public class Class1

{

public static void main (String[] args)

{

int x=(int)(Math.random( )*800);

System.out.println("所取随机数为:"+x);

System.out.println("它的百位数字为:"+x/100);

System.out.println("它的十位数字为:"+x0/10);

System.out.println("它的个位数字为:"+x);

//try{ System.in.read( ); }catch(IOException e){ }

}

}

2、请编写一个Application实现如下功能:在主类中定义方法f1(int n)

和方法f2(int n),它们的功能均为求 n!,方法f1( ) 用循环实现,方法f2(

)用递归实现 。在主方法main( ) 中,以4为实在参数分别调用方法f1( ) 和方法f2( ),并输出调用结果。

@程序

//import java.io.*;

public class Class1

{

public static void main (String[] args)

{

System.out.println("用循环实现求 n! 的结果(n=4):"+f1(4));

System.out.println("用递归实现求 n! 的结果(n=4):"+f2(4));

//try{ char c=(char)System.in.read( );}catch(IOException e){ }

}

static long f1(int n)

{

long k=1;

for(int i=1;i<=n;i++)

k*=i;

return k;

}

static long f2(int n)

{

if(n==1) return 1;

else return n*f2(n-1);

}

}

3、编写应用程序,功能为:从命令行传入一个包含十个字符的字符串,把该字符串与程序中给定的字符串"bacdbcabca"依次比较,统计两个串中对应字符相等的数目。然后输出命令行输入的字符串,并把两个串中对应字符不相等的字符替换为'@'后生成的新串输出,最后输出两个串中相等字符的数目。

@程序

public class Class1

{

public static void main(String args[]) throws

IOException

{

if(args[0].length()<10) System.out.print("Enter a string:include 10 chars");

else

{ String str=new String("bacdbcabca");

int n=0;

System.out.print("The Standard answer:");

for (int x=0;x

System.out.print(" "+str.charAt(x));

System.out.println();

System.out.print("The Student answer:");

for (int x=0;x

{

if (args[0].charAt(x)==str.charAt(x))

{ n++; System.out.print(" "+args[0].charAt(x)); }

else System.out.print(" "+"@");

}

System.out.println( );

System.out.println("The correct answers:"+n);

}

}

}

4、请编写一个Application实现如下功能:接受命令行中给出的一个字母串,先将该串原样输出,然后判断该串的第一个字母是否为大写,若是大写则统计该串中大写字母的个数,并将所有大写字母输出;否则输出信息串”第一个字母不是大写字母!”。

@程序

public class Class1

{

public static void main(String args[])

{

int count=0;

if(args.length!=0)

{

System.out.println(args[0]);

if((int)args[0].charAt(0)>=65

&&

(int)args[0].charAt(0)<=90)

{

for(int i=0;i

if((int)args[0].charAt(i)>=65

&&

(int)args[0].charAt(i)<=90)

{ System.out.print(args[0].charAt(i)); Count++; }

System.out.println( );

System.out.println(“共有 ”+count+” 个大写字母。”);

}

else System.out.println("第一个字母不是大写字母!");

}

else System.out.println("请给出命令行参数!");

}

}

5、请编写一个类,类名为subThread ,它是Thread 类的子类。该类中定义了含一个字符串参数的构造函数和run(

)方法,方法先在命令行显示线程的名称,然后随机休眠小于1秒的时间,最后显示线程结束信息:

"finished"+线程名。编写Application,在其中创建subThread类的三个对象t1、t2、t3,它们的名称分别为"First"、"

Second"、" Third",并启动这三个线程。

public class Class1

{

public static void main( String[ ] args )

{

Thread t1 = new subThread(

"First" );

Thread t2 = new subThread(

"Second" );

Thread t3 = new subThread(

"Third" );

t1.start( );t2.start( );t3.start(

);

}

}

class subThread extends Thread

{ public subThread( String str

)

{ super( str ); }

public void run(

)

{

System.out.println( " " + getName( ) );

try

{ sleep( ( int )( Math.random( ) * 1000 ) ); }

catch( InterruptedException e )

{ }

System.out.println( "Finished!" + getName( ) );

}

}

6、请编写一个类,类名为subThread ,它是Thread 类的子类。该类中定义了含一个字符串参数的构造函数和run(

)方法,方法中有一个for循环,循环一共进行5次,循环体中先在命令行显示该线程循环到了第几次,然后随机休眠小于1秒的时间,循环结束后显示线程结束信息:

线程名+"finished"。编写Application,在其中创建subThread类的三个对象t1、t2、t3,它们的名称分别为"First"、"

Second"、" Third",并启动这三个线程。

public class Class1

{ public static void main( String[ ] args )

{

Thread t1 = new subThread(

"First" );

Thread t2 = new subThread(

"Second" );

Thread t3 = new subThread(

"Third" );

t1.start(

); t2.start( ); t3.start(

);

}

}

class subThread

extends Thread

{

public subThread( String str

)

{ super( str ); }

public void run(

)

{

for(int i=1;i<=5;i++)

{

System.out.println( getName( )+", "+i+" times" );

try

{ sleep( ( int )( Math.random( ) * 1000 ) ); }

catch( InterruptedException e )

{ }

}

System.out.println( "Finished!" + getName( ) );

}

}

7、请编写一个类,类名为MulThread

,类中定义了含一个字符串参数的构造函数,并实现了Runnable接口,接口中的run(

)方法如下实现:方法中先在命令行显示该线程信息,然后随机休眠小于1秒的时间,最后后显示线程结束信息:

"finished"+线程名。编写Application,在其中通过Runnable创建MulThread类的三个线程对象t1、t2、t3,并启动这三个线程。

public class Class1

{

public static void main( String[ ] args )

{

Runnable r1 =new MulThread( "First" );

Runnable r2 =new MulThread( "Second" );

Runnable r3 =new MulThread( "Third" );

Thread t1 = new Thread( r1

);

Thread t2 = new Thread( r2

);

Thread t3 = new Thread( r3

);

t1.start( );t2.start( );t3.start( );

}

}

class MulThread implements Runnable

{

String s;

public MulThread(String str)

{ s=str;}

public void run( )

{

System.out.println(s);

try

{

Thread.sleep( ( int ) ( Math.random( ) * 1000 ) );

}

catch( InterruptedException e

){ }

System.out.println( "Finished! " + s

);

}

}

8、编写小程序实现一个数字时钟。

import java.awt.*;

import java.applet.*;

import java.util.Calendar;

public class Applet1 extends Applet implements Runnable

{

Thread timeThread;

Font wordFont;

int year,month,day;

int weekday;

int hour,minute,second;

public void init()

{

this.setBackground(Color.black);

wordFont=new Font("楷体_gb2312",Font.BOLD,50);

}

public void start()

{

if(timeThread==null)

{

timeThread=new Thread(this);

timeThread.start();

}

}

public void stop()

{

if(timeThread!=null

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值