JAVA编程题全集(100题及答案)

题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。 import java.util.*;

public class lianxi48 {

public static void main(String args[]) { Scanner s = new Scanner(System.in); int num=0,temp; do{

num = s.nextInt();

}while (num<1000||num>9999); int a[]=new int[4];

a[0] = num/1000; //取千位的数字 a[1] = (num/100); //取百位的数字 a[2] = (num/10); //取十位的数字 a[3] = num; //取个位的数字 for(int j=0;j<4;j++) { a[j]+=5; a[j]%=10; }

for(int j=0;j<=1;j++) {

temp = a[j];

a[j] = a[3-j]; a[3-j] =temp; }

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

【程序49】

题目:计算字符串中子串出现的次数 import java.util.*; public class lianxi49 {

public static void main(String args[]){ Scanner s = new Scanner(System.in);

String str1 = s.nextLine();

String str2 = s.nextLine(); int count=0;

if(str1.equals(\ {

System.exit(0); }

else {

for(int i=0;i<=str1.length()-str2.length();i++) {

if(str2.equals(str1.substring(i, str2.length()+i))) //这种比法有问题,会把\看成有2个\子串。 count++; } } } }

【程序50】

题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文件 \中。 import java.io.*; import java.util.*; public class lianxi50 {

public static void main(String[] args){ Scanner ss = new Scanner(System.in); String [][] a = new String[5][6]; for(int i=1; i<6; i++) {

a[i-1][0] = ss.nextLine();

a[i-1][1] = ss.nextLine(); for(int j=1; j<4; j++) {

a[i-1][j+1] = ss.nextLine(); } }

//以下计算平均分 float avg; int sum;

for(int i=0; i<5; i++) { sum=0;

for(int j=2; j<5; j++) {

sum=sum+ Integer.parseInt(a[i][j]); }

avg= (float)sum/3;

a[i][5]=String.valueOf(avg); }

//以下写磁盘文件 String s1; try {

File f = new File(\ if(f.exists()){

}else{

f.createNewFile();//不存在则创建 }

BufferedWriter output = new BufferedWriter(new FileWriter(f)); for(int i=0; i<5; i++) { for(int j=0; j<6; j++) { s1=a[i][j]+\ output.write(s1); } }

output.close();

} catch (Exception e) { e.printStackTrace(); } } }

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4