2010年HDU复试笔试真题(Java)
程序员文章站
2022-06-07 23:34:15
...
试题一
import java.util.Random;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Random rand=new Random();
int num=rand.nextInt(900)+100;//三位随机数
int k=10;
System.out.println("猜数字");
while(k-->0) {
int n=in.nextInt();
if(num==n) {
System.out.println("恭喜你,猜对了");
break;
}
else {
if(num>n)
System.out.println("猜小了,请继续");
else
System.out.println("猜大了,请继续");
}
}
if(k<=0)
System.out.println("Bye Bye");
}
}
考点直击:随机数Random的应用
int num=rand.nextInt(900)+100;//三位随机数
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String str=in.nextLine();
int len=str.length();
int num=0;
int start=0,end=0;
for(int i=0;i<len;) {
if(str.charAt(i)<='9'&&str.charAt(i)>='0') {
start=i;
end=i;
for(int j=i+1;j<len;j++) {
if(str.charAt(j)<='9'&&str.charAt(j)>='0') {
end=j;
}
else
break;
}
String tmp=str.substring(start, end+1);
int temp=Integer.parseInt(tmp);
num+=temp;
i=end+1;
}
else
i++;
}
System.out.println(num);
}
}
试题三
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main{
static int k;
public static void main(String[] args) throws IOException{
Scanner in=new Scanner(System.in);
String path="C:\\Users\\Administrator\\Desktop";
File fr=new File(path,"student.txt");
BufferedReader br=new BufferedReader(new FileReader(fr));
String temp;
temp=br.readLine();
Student stu[]=new Student[100];把100改成3即可(假设有3个人)
temp=br.readLine();
k=0;
while(temp!=null) {
String tmp[]=temp.split("\\t");
stu[k++]=new Student(tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],tmp[5]);
temp=br.readLine();
}
Arrays.sort(stu, new Comparator<Student>() {
@Override
public int compare(Student o1,Student o2) {
if(o1.score<o2.score)
return 1;
else
return -1;
}
});
for(int i=0;i<k;i++) {
int j=i+1;
System.out.println(stu[i].name+" "+stu[i].score+" "+j);
}
}
}
class Student{
String name,sno;
int english_score,police_score,math_score,major_score,score;
public Student(String name,String sno,String english_score,String police_score,String math_score,String major_score) {
this.name=name;
this.sno=sno;
this.english_score=Integer.parseInt(english_score);
this.police_score=Integer.parseInt(police_score);
this.math_score=Integer.parseInt(math_score);
this.major_score=Integer.parseInt(major_score);
}
}
上一篇: Java中遍历数组使用foreach循环还是for循环
下一篇: 东华复试上机踩坑记-15年第三题
推荐阅读