1, a到z的字母用ASCII码表示,显示出来
public static void main(String[] args) { char j = 97; for (char i = 0; i < 26; i++) { System.out.println(j++ +" "+(97+(int)i)+" "); } }
2,求一元二次方程的根
public static void main(String[] args) { //2,求一元二次方程的根(10分) //a*x*x+b*x+c = 0 Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int delta = b*b - 4*a*c; double deltad = Math.sqrt(delta); double x1 = ((-b)+deltad)/2*a; double x2 = ((-b)-deltad)/2*a; System.out.println(x1+" "+x2); }
3,写一个函数,从小到大排序一个数组
快速排序
public static void main(String[] args) { // 3,写一个函数,从小到大排序一个数组(10分) int[] a = {1,1,2,1,2,3,3,4,3}; //Arrays.sort(a); mysort(a,0,a.length-1); System.out.println(Arrays.toString(a)); } private static void mysort(int[] a,int left,int right) { if(left == right) return; int i = left; int j = right; int k = a[left]; while (i <= j){ while (i<=j && a[i] <= k){ i++; }//i指向a[i]>K的数,要放到左边去 while (i<=j && a[j] >= k){ j--; }//j指向要放到右边的数 if(i<=j){ int temp = a[i]; a[i] = a[j]; a[j] = temp; i++; j--; }//交换 } int temp = a[j]; a[j] = k; a[left] = temp; int c = j+1; int c1 =j-1;if(left<=c1){ mysort(a,left,c1); } if(c<=right){ mysort(a,c,right); } }
4,写一个异常处理 以c=a/b为例
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c ; try{ c = a/b; System.out.println(c); }catch (ArithmeticException e ){ System.out.println("nonono"+e); } }
5,打印9*9乘法表
public static void main(String[] args) { //5,打印9*9乘法表(10分) for(int i = 1;i<10;i++){ for(int j = 1;j<=i;j++){ System.out.print(j+"*"+i+"="+i*j+"\t"); } System.out.println(); } }
6,写一个递归函数: f(0)=0,f(1)=1,f(n)=f(n-1)+f(n-2);
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int c = f(n); System.out.println(c); } private static int f(int n) { if(n == 0) return 0; if (n == 1) return 1; return f(n-1)+f(n-2); }
7,新建一个关于学生的类或结构体,包含学号,姓名,语文,数学,英语成绩; 并输入一个整数n,分配一个类的数组。
public static void main(String[] args) { // 7,新建一个关于学生的类或结构体,包含学号,姓名,语文,数学,英语成绩; // 并输入一个整数n,分配一个类的数组。 Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Student2[] shuzu =new Student2[n]; for (int i = 0; i < shuzu.length; i++) { shuzu[i] = new Student2(i,"张三"+i,90+i,80+i,70+i); } System.out.println(Arrays.toString(shuzu)); } } class Student2{ int number; String name; int grade_chinese; int grade_math; int grade_english; @Override public String toString() { return "Student2{" + "number=" + number + ", name='" + name + '\'' + ", grade_chinese=" + grade_chinese + ", grade_math=" + grade_math + ", grade_english=" + grade_english + '}'; } public Student2(int number, String name, int grade_chinese, int grade_math, int grade_english) { this.number = number; this.name = name; this.grade_chinese = grade_chinese; this.grade_math = grade_math; this.grade_english = grade_english; }
8,运用第7题的类,要求写三个函数
使用的字符流
public static void main(String[] args) throws IOException {
// 8,运用第7题的类,要求写三个函数。(均用二进制输入输出)
//(1)把一个学生信息存到一个文件中
//(2)从一个文件中提取所有学生信息,并打印
//(3)从一个文件中复制所有学生信息,复制到另外一个文件。
File file = new File("src\\student2.txt");
File file1 = new File("src\\studentcopy.txt");
Student2 student2 = new Student2(98,"lisi",88,77,99);
inputFile(student2,file);
String str = outputFile(file);
System.out.println(str);
copyFile(file,file1);
}
private static void copyFile(File file, File file1) throws IOException {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String str = bufferedReader.lines().toString();
FileWriter fileWriter = new FileWriter(file1);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
while ((str = bufferedReader.readLine()) != null) {
bufferedWriter.write(str);
}
bufferedWriter.close();
bufferedReader.close();
fileWriter.close();
fileReader.close();
}
private static String outputFile(File file) throws IOException {
String str;
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
str = bufferedReader.readLine();
bufferedReader.close();
fileReader.close();
return str;
}
private static void inputFile(Student2 student2, File file) throws IOException {
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String str = student2.toString();
bufferedWriter.write(str);
bufferedWriter.close();
fileWriter.close();
}
}
class Student2 {
int number;
String name;
int grade_chinese;
int grade_math;
int grade_english;
@Override
public String toString() {
return "Student2{" +
"number=" + number +
", name='" + name + '\'' +
", grade_chinese=" + grade_chinese +
", grade_math=" + grade_math +
", grade_english=" + grade_english +
'}';
}
public Student2(int number, String name, int grade_chinese, int grade_math, int grade_english) {
this.number = number;
this.name = name;
this.grade_chinese = grade_chinese;
this.grade_math = grade_math;
this.grade_english = grade_english;
}
}
使用的字节流--即输入是二进制而不是字符串。
public static void main(String[] args) throws IOException{
File file = new File("src/student2.txt");
File file1 = new File("src\\studentcopy1.txt");
Student2 student2 = new Student2(00,"lisi",88,77,99);
copyFile(file, file1);//文件复制
inputFile(student2,file1);//二进制输入文件保存信息
outputFile(file1);//读取文件二进制输出
}
private static void outputFile(File file1) throws IOException {
InputStream inputStream = new FileInputStream(file1);
byte[] bytes = new byte[1024];
inputStream.read(bytes);
String s = conver2(bytes);
System.out.println(s);
}
//byte转二进制
public static String conver2(byte [] b)
{
StringBuffer result = new StringBuffer();
for(int i = 0;i<b.length;i++)
{
result.append(Long.toString(b[i] & 0xff, 2)+",");//采用long中的toString方法, &0xff是指与11111111相与,radix = 2是指基数为二,在这里是指转二进制。
}
return result.toString().substring(0, result.length()-1);
}
private static void inputFile(Student2 student2, File file1) throws IOException{
String str = student2.toString();
char[] chars = str.toCharArray();
OutputStream outputStream = new FileOutputStream(file1);
String result="";
for(int i=0;i<chars.length;i++){
result +=Integer.toBinaryString(chars[i])+ " ";
}
byte[] chars1 = result.getBytes();
outputStream.write(chars1);
// 刷新缓冲区
outputStream.flush();
// 关闭流
outputStream.close();
}
private static void copyFile(File file, File file1) {
file.renameTo(file1);
}
}
class Student2 {
int number;
String name;
int grade_chinese;
int grade_math;
int grade_english;
@Override
public String toString() {
return "Student2{" +
"number=" + number +
", name='" + name + '\'' +
", grade_chinese=" + grade_chinese +
", grade_math=" + grade_math +
", grade_english=" + grade_english +
'}';
}
public Student2(int number, String name, int grade_chinese, int grade_math, int grade_english) {
this.number = number;
this.name = name;
this.grade_chinese = grade_chinese;
this.grade_math = grade_math;
this.grade_english = grade_english;
}