JAVA程序设计(复习题)
Posted 未定_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA程序设计(复习题)相关的知识,希望对你有一定的参考价值。
JAVA
第10章 输入输出流与文件读写
例1:
package T1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class tm1
public static void a() throws FileNotFoundException
Scanner cin=new Scanner(System.in);
File f=new File("E:/文件操作/zfx/tm1_1.txt");
PrintWriter cout=new PrintWriter(f);
System.out.println("请依次输入姓名、数学、物理、化学:");
cout.println("姓名\\t数学\\t物理\\t化学");
String xm=cin.next();
while(!xm.equals("end"))
int a=cin.nextInt();
int b=cin.nextInt();
int c=cin.nextInt();
cout.println(xm+'\\t'+a+'\\t'+b+'\\t'+c);
xm=cin.next();
System.out.println("end!!!");
cin.close();
cout.close();
package T1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class tm2
public static int[] ps(int y[],int n)
int p[]=new int[n];
int t;
for(int i=0;i<n;i++)
p[i]=i;
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(y[i]>y[j])
t=y[i];
y[i]=y[j];
y[j]=t;
t=p[i];
p[i]=p[j];
p[j]=t;
return p;
public static void b() throws FileNotFoundException
File a=new File("E:/文件操作/zfx/tm1_1.txt");
File b=new File("E:/文件操作/zfx/tm1_2.txt");
Scanner cin=new Scanner(a);
PrintWriter cout=new PrintWriter(b);
cout.println("姓名\\t数学\\t物理\\t化学\\t总成绩");
String bt=cin.nextLine();
Object x[][]=new Object[100][5];
int i=0;
int y[]=new int[100];
while(cin.hasNext())
x[i][0]=cin.next();
x[i][1]=cin.nextInt();
x[i][2]=cin.nextInt();
x[i][3]=cin.nextInt();
x[i][4]=Integer.parseInt(x[i][1].toString())+Integer.parseInt(x[i][2].toString())+Integer.parseInt(x[i][3].toString());
y[i]=Integer.parseInt(x[i][4].toString());
//cout.println(x[i][0]+"\\t"+x[i][1]+"\\t"+x[i][2]+"\\t"+x[i][3]+"\\t"+x[i][4]);
i++;
int q[]=ps(y,4);
for(int j=0;j<4;j++)
cout.println(x[q[j]][0]+"\\t"+x[q[j]][1]+"\\t"+x[q[j]][2]+"\\t"+x[q[j]][3]+"\\t"+x[q[j]][4]);
System.out.println("end!!!");
cin.close();
cout.close();
package T1;
import java.io.FileNotFoundException;
public class Text
public static void main(String[] args) throws FileNotFoundException
tm1.a();
tm2.b();
例2:文件合并
package T3;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class tm1
public static void main(String[] args) throws IOException
FileWriter a = new FileWriter("E:/文件操作/T3/result.txt",true);
BufferedWriter cout = new BufferedWriter(a);
FileReader b = new FileReader("E:/文件操作/T3/result1.txt");
BufferedReader cin = new BufferedReader(b);
String s = cin.readLine();
while (s != null)
cout.write(s);
cout.newLine();
s = cin.readLine();
System.out.println("end!!!");
cin.close();
cout.close();
例3:统计文本文件中字符的个数(不包括空格、制表符、回车符)
package T3;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class tm3
public static void main(String[] args) throws FileNotFoundException
File a=new File("E:/文件操作/T3/result1.txt");
Scanner cin=new Scanner(a);
int dz=0,xz=0,sz=0,i=0;
while(cin.hasNext())
i=0;
String s=cin.nextLine();
while(i<s.length())
Character c=s.charAt(i);
if(Character.isDigit(c)==true)
sz++;
else if(Character.isUpperCase(c)==true)
dz++;
else if(Character.isLowerCase(c)==true)
xz++;
i++;
System.out.println("大写字母:"+dz+"\\n小写字母"+xz+"\\n数字"+sz);
cin.close();
第9章 java集合类及其应用
约瑟夫环
package T1;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class tm2
public static void main(String[] args)
Scanner cin = new Scanner(System.in);
List<Integer> l1 = new LinkedList<Integer>();
int n, w, s, k = 0;
System.out.println("请输入小孩个数、第几个开始、数到几出列:");
n = cin.nextInt();
w = cin.nextInt();
s = cin.nextInt();
for (int i = 1; i <= n; i++)
Integer a = new Integer(i);
l1.add(a);
for (int i = w-1; i < l1.size(); i++)
k++;
if (k == s)
System.out.println(l1.get(i));
l1.remove(l1.get(i));
k=0;
i--;//缺一不可
if (l1.size() == 1)
System.out.println(l1.get(0));
break;
if (i == l1.size()-1)
i = -1;
cin.close();
/*1 2 3 4 5 :3
1 2 4 5 :1
2 4 5 :5
2 4 :2
4*/
101进制转r进制
package T2;
import java.util.ArrayList;
import java.util.List;
public class tm2g
public static void main(String[] args)
List a=new ArrayList();
int x=10,r=2;
char c;
while(x!=0)
int y=x%r;
if(y>9)
y=y+55;
c=(char)y;
a.add(0,c);
else a.add(0,y);
x=x/r;
for(int i=0;i<a.size();i++)
System.out.print(a.get(i));
System.out.println();
第7章 java常用类及其使用
运行时间
package T1;
public class tm1
public static void main(String[] args)
long st=System.currentTimeMillis();
for (int i = 0; i < 1000; i++)
System.out.println(i);
long ed = System.currentTimeMillis();
System.out.println("运行时间:" + (ed - st) + "ms");
单词划分及反转
package T1;
public class tm2
public static void main(String[] args)
String s="I am a student.";
String b[]=s.split(" ");
System.out.println("该语句有"+b.length+"个单词。");
for(int i=0;i<b.length;i++)
System.out.println((i+1)+"-----"+b[i]);
System.out.println("----------------------");
StringBuffer ss=new StringBuffer("I am a student.");
System.out.println(ss+"前后转换为:"+ss.reverse());
记事本打开10秒
package T1;
public class tm4
public static void main(String[] args)
Process p=null;
try
p=Runtime.getRuntime().exec("notepad.exe E:/文件操作/第9章 Java集合类/Student.txt");
Thread.sleep(10000);
p.destroy();
catch (Exception e)
e.printStackTrace();
int转二进制
String str=Integer.toBinaryString(i);
package T2;
public class tm6
public static void main(String[] args)
String s1 = "you are a student.";
String s2[] = s1.split(" ");
String s3[] = new String[s1.length()];
for (int i = 0; i < s2.length; i++)
s3[i] = s2[i].substring(0, 1).toUpperCase() + s2[i].substring(1) + " ";
System.out.print(s3[i]);
System.out.println();
System.out.println("----------------------");
StringBuffer s = new StringBuffer("ada132sum");
char a, b;
for (int i = 1; i < s.length(); i++)
a = s.charAt(i);
b = s.charAt(i - 1);
if ((Character.Java基础50道经典练习题(26)——求星期