java编程题

Python017

java编程题,第1张

1.package test

import java.util.Iterator

import java.util.Map

import java.util.TreeMap

public class Test {

public static void main(String[] args) {

String str = "afdjasdg&&&&&$$jfsjdfjdjjdjdjdjdjdj"

int max=0

Object chars=null

Map tree = new TreeMap()

for (int i = 0i <str.length()i++) {

char ch = str.charAt(i)

if ((ch >= 1 &&ch <= 255) ) {

if (!tree.containsKey(ch)) {

tree.put(ch, new Integer(1))

} else {

Integer in = (Integer) tree.get(ch) + 1

tree.put(ch, in)

}

}

}

Iterator tit = tree.keySet().iterator()

while (tit.hasNext()) {

Object temp = tit.next()

if(max<=Integer.parseInt(tree.get(temp)+""))

{

max=Integer.parseInt(tree.get(temp)+"")

chars=temp

}

}

System.out.print(chars.toString() + "出现" + max + "次")

}

}

只要用assic码做范围就可以了.任何字符都可以过滤.

2.方法很多,hashmap或是arraylist,数组都可以的.就是对应关系而已.

package test

public class ListTest {

static String[] to_19 = { "zero", "one", "two", "three", "four", "five",

"six", "seven", "eight", "nine", "ten", "eleven", "twelve",

"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",

"eighteen", "nineteen" }

static String[] tens = { "twenty", "thirty", "forty", "fifty", "sixty",

"seventy", "eighty", "ninety" }

static String[] denom = { "", "thousand ", "million", "billion",

"trillion", "quadrillion", "quintillion", "sextillion",

"septillion", "octillion", "nonillion", "decillion", "undecillion",

"duodecillion", "tredecillion", "quattuordecillion",

"sexdecillion", "septendecillion", "octodecillion",

"novemdecillion", "vigintillion" }

public static void main(String[] argv) throws Exception {

long tstValue = 12345

ListTest itoe = new ListTest()

System.out.println(itoe.english_number(tstValue))

}

private String convert_nn(int val) {

if (val <20) return to_19[val]

int flag = val / 10 - 2if (val % 10 != 0)

return tens[flag] + "-" + to_19[val % 10]

else return tens[flag]

}

private String convert_nnn(int val) {

String word = ""

int rem = val / 100

int mod = val % 100

if (rem >0) {word = to_19[rem] + " hundred "}

if (mod >0) {word = word + convert_nn(mod)}

return word

}

public String english_number(long val) {

if (val <100) {System.out.println((int) val)return convert_nn((int) val)}

if (val <1000) {return convert_nnn((int) val)}

for (int v = 0v <denom.lengthv++) {

int didx = v - 1

long dval = new Double(Math.pow(1000, v)).longValue()

if (dval >val) {

long mod = new Double(Math.pow(1000, didx)).longValue()

int l = (int) (val / mod)

long r = (long) (val - (l * mod))

String ret = convert_nnn(l) + " " + denom[didx]

if (r >0) {ret = ret + ", " + english_number(r)}

return ret

}

}

return null

}

}

http://hi.baidu.com/mysolarstar/blog

1、JAVA语言是由哪个软件公司开发?( B )

A、 sum B、sun C、IBM D、联想

2、以下描述错误的是?( C )

A、 JAVA能编写独立运行的应用程序

B、 JAVA支持多线程

C、 JAVA是一网络应用软件

D、 JAVA能运行于windows 系统

3、程序中添加注释的作用?( B )

A、使得程序运行更高效

B、 增加程序的可读性和可理解性

C、 可以使用#号来注释

D、 提高程序编译的速度

4、HelloWorld.java编译成功后会在当前目录中生成一个什么文件?(B )

A、 Hello.java B、HelloWorld.class C、Helloworld.class D、helloWorld.class

5、编写一个java application程序,其中类声明为public class StringDemo,那么该程序应该以什么文件名来保存?( A )

A、StringDemo.java B、StringDemo.class C、Strindemo.java D、StringDemo.txt

6、以下描述正确的是?( C )

A、 JAVA不可以用来开发手机程序

B、 在JAVA程序中java.awt包是唯一自动引入的类库

C、 java.io包是JAVA语言的输入输出类库

D、 JAVA的包中可以有相同的类名

7、在声明变量时以下那个选项是不允许使用?( A )

A、char B、Int C、Hello D、Public

8、以下语句错误的是?( B )

A、int a=10 B、int ab=10.4f C、float b=10 D、double c=10.0

9、以下产生信息丢失的类型转换是?( B )

A、float a=10 B、int a=(int)884656565.0f C、byte a=10int b=a double d=100

10、以下运行符中属三元运算符的是?( C )

A、+ B、% C、条件运算符 D、*

11、以下关于数组的描述有误的是?( D)

A、 整型数组中的各元素的值必须是整型

B、 数组是有序数据的集合

C、 如数组名为ab,即ab.length可以取得该数组的长度

D、 数组的下标从1开始

12、以下对于标识符的描述有误的是?( D )

A、 常量用大写字母,变量用小写字母

B、 JAVA的标识符严格区分大小写

C、 第一个字符不是数字

D、 标识符中任何位置都不能用数字

13、以下描述有误的是?( B )

A、 package <包名>这个语句必须在程序的第一行。

B、 当方法用void声明时,方法必须返回一个整型的值

C、 return语句执行后,当前方法将被终止执行

D、 当方法用int声明时,方法必须返回一个整型的值

14、以下语句正确的是?( D)

A、x+1=5 B、i++=1C、a++b=1 D、 x+=1

15、以下结果为真(true)的是?( B )

A、10>’a’ B、’a’>20 C、 !true D、(3<5) &&(4>10)

16、int a=’2’a的值是多少?(B )

A、1 B、50 C、49 D、2

17、’a’%3的值是多少?( D )

A、1 B、97 C、3 D、2

18、以下关于循环语句描述正确的是?( D )

A、 for循环不可能产生死循环

B、 while循环不可能产生死循环

C、 for循环不能嵌套while循环

D、 即使条件不满足do……while循环体内的语句也至少执行一次

19、以下对选择语句的描述错误的是?( )

A、 根据某一条件重复执行一部分代码直到满足终止循环条件为止

B、 可以根据条件控制程序流程,改变程序执行的顺序

C、 选择语句可以嵌套使用

D、 当条件满足时就会执行相应的语句

20、能判断出年份是否是闰年的选项是?( A )

A、 能被4整除且不能被100整除

B、 不能被4整除但能被100整除

C、 能4整除

D、 被100整除

一、程序阅读题(每题6分)

1、

public class Kao

{

public static void main(String args[])

{

int a

int i=1

a=++i

System.out.println("a="+a)

System.out.println("i="+i)

}

}

a的值是多少?i的值是多少?

a=2i=2

2、

public class Kao1

{

public static void main(String args[])

{

int a=7

System.out.println(a+”的平方是:”+a*a)

}

}

输出结果是什么? 7的平方是:49

3、

public class Kao2

{

public static void main(String args[])

{

int sum=0

for(int i=1i<=100i+=2)

sum+=i

System.out.println(sum)

}

}

sum的值最后是多少?2500

4、

public class Kao3

{

public static void main(String args[])

{

int a[]=new int[5]

for(int i=0i<a.lengthi++)

a[i]=i+2

for(int j=0j<4j++)

System.out.println("a["+j+"]="+a[j])

}

}

运行后程序显示结果是?

a[0]=2

a[1]=3

a[2]=4

a[3]=5

a[4]=6

5、

public class Kao4

{

public static void main(String args[])

{

int a=0650

System.out.println(a)

}

}

在屏幕上输出的值是多少?424 //为什么是这样的情况呢?

二、编程题 编写程序计算一个年份是否是闰年。

return ((year%4 == 0) &&(year%100 != 0)) || (year%400 == 0) ? true : false

}

//把你的那个表作成test3.txt放到D盘根,跑程序就好了

import java.io.BufferedReader

import java.io.FileInputStream

import java.io.InputStreamReader

import java.util.ArrayList

import java.util.Collections

import java.util.Iterator

import java.util.List

public class MyTest3 {

List stuInfoList = new ArrayList()

public MyTest3(){

printResult()

}

public void readFile() {

try {

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\test3.txt")))

String line = ""

int i=0

while ((line = br.readLine()) != null) {

if(i++>0){ //略过头上的汉字行

StudentInfo student = new StudentInfo(line.split(" "))

stuInfoList.add(student)

}

}

} catch (Exception e) {

e.printStackTrace()

}

}

class StudentInfo implements Comparable{

public int stuId

public double pings

public double qizhong

public double qimo

public double bishi

public double zhongFeng

public StudentInfo(){}

public StudentInfo(String[] info){

this.stuId = Integer.parseInt(info[0])

this.pings = Integer.parseInt(info[1])

this.qizhong = Integer.parseInt(info[2])

this.qimo = Integer.parseInt(info[3])

this.bishi = Integer.parseInt(info[4])

this.zhongFeng = pings*0.1+qizhong*0.25+qimo*0.15+bishi*0.5

}

public String getPingJunFeng(int size){

return pings/size +" "+qizhong/size+" "+qimo/size+" "+bishi/size+" "+zhongFeng/size

}

public String toString(){

return stuId + " " +pings + " "+qizhong+" "+qimo+" "+bishi+" "+zhongFeng

}

public int compareTo(Object arg0) {

StudentInfo info = (StudentInfo)arg0

return (int)(info.zhongFeng-this.zhongFeng)

}

}

public void printResult(){

readFile()

System.out.println("学号 平时 期中 期末 笔试 总评分")

for(Iterator it=stuInfoList.iterator()it.hasNext()){

System.out.println(it.next())

}

System.out.println("-----------80分以上---------------\r\n学号 总评分")

for(Iterator it=stuInfoList.iterator()it.hasNext()){

StudentInfo info = (StudentInfo)it.next()

if(info.zhongFeng>=80)

System.out.println(info.stuId + " "+info.zhongFeng)

}

System.out.println("-----------没有及格---------------\r\n学号 总评分")

for(Iterator it=stuInfoList.iterator()it.hasNext()){

StudentInfo info = (StudentInfo)it.next()

if(info.zhongFeng<60)

System.out.println(info.stuId + " "+info.zhongFeng)

}

Collections.sort(stuInfoList)

System.out.println("-----------排序之后---------------\r\n学号 平时 期中 期末 笔试 总评分")

for(Iterator it=stuInfoList.iterator()it.hasNext()){

System.out.println(it.next())

}

StudentInfo pinjunfeng = new StudentInfo()

for(Iterator it=stuInfoList.iterator()it.hasNext()){

StudentInfo info = (StudentInfo)it.next()

pinjunfeng.bishi+=info.bishi

pinjunfeng.pings+=info.pings

pinjunfeng.qimo+=info.qimo

pinjunfeng.qizhong+=info.qizhong

pinjunfeng.zhongFeng+=info.zhongFeng

}

System.out.println("-----------平均分---------------\r\n平时 期中 期末 笔试 总评分")

System.out.println(pinjunfeng.getPingJunFeng(stuInfoList.size()))

}

public static void main(String[] args) throws Exception {

new MyTest3()

}

}