1.编写一个程序。要求定义一种比较直观的文本文件格式,用户可以按这种格式在一个文本文件中输入学生姓名和成绩。然后,程序可以打开指定的文本文件,从中读取所有的学生姓名和成绩,计算并输出平均考试成绩、最高考试成绩,以及取得最高考试成绩的学生名单。这些输出结果要求同时能够输出到一个指定的文本文件中。2、编程把一个数字表示的4285如何转成文字肆千贰百捌拾伍元。
3、请在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。
4、编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根据内容为[1][2][3]的数组形成内容为"1,2,3"的字符串。 5、编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。 十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2,这次得到的余数就是次低位,如此循环,直到被除数为0为止。其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。 6、请编写一个字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。 7、编写一个程序,当用户输入一个目录时,信息后,该程序能列出该目录下的所有子目录和文件。8、编程把一个数字表示的4285如何转成文字肆千贰百捌拾伍元。
3、请在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。
4、编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根据内容为[1][2][3]的数组形成内容为"1,2,3"的字符串。 5、编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。 十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2,这次得到的余数就是次低位,如此循环,直到被除数为0为止。其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。 6、请编写一个字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。 7、编写一个程序,当用户输入一个目录时,信息后,该程序能列出该目录下的所有子目录和文件。8、编程把一个数字表示的4285如何转成文字肆千贰百捌拾伍元。
2.参考http://blog.csdn.net/lip009/archive/2006/10/19/1340814.aspx,带角分的
3.参考public class Test
{
public int searchChar(char chars[],char c){
if(chars==null){
throw new IllegalArgumentException("数组为null");
}
int result=-1;
for(int i=0;i<chars.length;i++){
if(chars[i]==c){
result=i;
break;
}
}
return result;
}
public static void main(String args[]){
Test test=new Test();
System.out.println(test.searchChar(new char[]{'a','b','c'}, 'b'));
System.out.println(test.searchChar(new char[]{'a','b','c'}, 'd'));
System.out.println(test.searchChar(null, 'b'));
}
}
public class Test
{
public static void main(String args[]){
String strs[]=new String[]{"1","2","3"};
String result="";
for(int i=0;i<strs.length;i++){
result=result+","+strs[i];
}
result=result.replaceAll("^,","");
System.out.println(result);
}
}
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;/**
* 1.编写一个程序。要求定义一种比较直观的文本文件格式,用户可以按这种格式在一个文本文件中输入学生姓名和成绩。
* 然后,程序可以打开指定的文本文件,从中读取所有的学生姓名和成绩,计算并输出平均考试成绩、最高考试成绩,以及取得最高考试成绩的学生名单。
* 这些输出结果要求同时能够输出到一个指定的文本文件中。
*
* 2、编程把一个数字表示的4285如何转成文字肆千贰百捌拾伍元。
*
* 3、请在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。
* 要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。
* 在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。
*
* 4、编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根据内容为[1][2][3]的数组形成内容为"1,2,3"的字符串。
*
* 5、编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。
* 十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2,这次得到的余数就是次低位,如此循环,直到被除数为0为止。
* 其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。
*
* 6、请编写一个字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。
*
* 7、编写一个程序,当用户输入一个目录时,信息后,该程序能列出该目录下的所有子目录和文件。
*
* 8、编程把一个数字表示的4285如何转成文字肆千贰百捌拾伍元。
*
* @author 侯磊
*
*/
public class Questions { public static void main(String[] args) throws IOException {
try {
System.out.println("/*** 题目 1 :要求存在测试文件 **/");
Question1.proccess("d:/st1.txt", "d:/st2.txt"); // Q1
System.out.println("/*** 题目 2 **/");
System.out.println(Question2.change(4285)); // Q2
System.out.println("/*** 题目 3 **/");
char[] resouce = { '你', '好', '啊', '哈', '哈', '。' };
System.out.println(Question3.search(resouce, 'a')); // Q3
System.out.println(Question3.search(resouce, '哈')); // Q3
System.out.println(Question3.search(null, '哈')); // Q3
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("/*** 题目 4 **/");
System.out.println(Question4.jion(new int[] { 1, 2, 3 })); // Q4
System.out.println("/*** 题目 5 :要求输入待转换的整数数字 **/");
System.out.println(Question5.printInput()); //Q5
System.out.println("/*** 题目 6 **/");
Question6.test("d:/st1.txt"); // Q6
System.out.println("/*** 题目 7 **/");
Question7.listPath("d:/"); // Q7
}}class Question7 {
public static void listPath(String pathName) {
File path = new File(pathName);
if (path.isDirectory()) {
File[] subPaths = path.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
} });
System.out.println("所有子目录如下:");
for (int i = 0; i < subPaths.length; i++) {
System.out.println("\t" + subPaths[i].getName());
}
File[] subFiles = path.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
});
System.out.println("所有文件如下:");
for (int i = 0; i < subFiles.length; i++) {
System.out.println("\t" + subFiles[i].getName());
}
}
}
}class Question6 {
private BufferedReader reader; private long lineNumber = 0; public Question6(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
} public String readLine() throws IOException {
String str = reader.readLine();
if (str != null) {
lineNumber++;
return lineNumber + ":" + str;
}
return str;
} /**
* 该方法用于测试
*
* @throws IOException
*/
public static void test(String fileName) throws IOException {
Question6 reader = new Question6(new FileInputStream(fileName));
String str;
while ((str = reader.readLine()) != null) {
System.out.println(str);
}
}
}class Question5 {
public static String printInput() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
br.close();
int num = Integer.parseInt(line);
StringBuffer sb = new StringBuffer();
while (num != 0) {
sb.insert(0, (num & 1));
num = num >>> 1;//左补0的右移运算,对应于题目要求的除2运算,要比题目要求的好些。
}
return sb.toString();
}
}class Question4 {
public static String jion(int[] nums) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < nums.length; i++) {
sb.append(nums[i]);
if (i < nums.length - 1)
sb.append(",");
}
return sb.toString();
}
}class Question3 {
public static int search(char[] resouce, char c)
throws IllegalAccessException {
if (resouce == null)
throw new IllegalAccessException("所传入的参数为null");
for (int i = 0; i < resouce.length; i++) {
if (c == resouce[i])
return i;
}
return -1;
}
}/**
* 第2个与第8个相同
*/
class Question2 {
private static String[] nums = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒","捌", "玖" };
private static String[] units = { "元", "拾", "佰", "仟", "万" };
public static String change(int money) {
StringBuffer sb = new StringBuffer();
if (money < 0)
return null;
int tm;
for (int i = 1, j = 0; true; i *= 10, j++) {
tm = money % (i * 10);
tm = tm / i;
if (tm == 0)
break;
sb.insert(0, nums[tm] + units[j]);
}
return sb.toString();
}
}/**
* 文件格式:每一行表示一个学生的一条记录信息。先是学生的姓名,然后是分号,最后是学生的成绩(也就是说,学生姓名和成绩之间以分号分隔)。
* Student类属于本题目
*/
class Question1 {
public static void proccess(String sourceFile, String destFile)
throws IOException {
// 读取所有的学生姓名和成绩
BufferedReader br = new BufferedReader(new FileReader(sourceFile));
LinkedList<Student> ss = new LinkedList<Student>();
String str;
double average = 0.0;
int count = 0;
Student highMarkStu = new Student(null, 0);
while ((str = br.readLine()) != null) {
String record[] = str.split(";|;");
float = Float.parseFloat(record[1]);
if (record[0].equals(""))
continue;
Student std = new Student(record[0], );
ss.add(std);
if ( >= highMarkStu.getMark())
highMarkStu = std;
average += ;
count++;
}
br.close();
average = average / count;
// 输出平均考试成绩、最高考试成绩,以及取得最高考试成绩的学生名单
StringBuffer sb = new StringBuffer();
sb.append("平均考试成绩:\t\t").append(average).append(" 分\r\n");
sb.append("最高考试成绩:\t\t").append(highMarkStu.getMark()).append(" 分\r\n");
sb.append("最高考试成绩的学生:\t").append(highMarkStu.getName()).append("\r\n");
FileWriter writer = new FileWriter(destFile);
writer.write(sb.toString());
writer.close();
}
}class Student implements java.lang.Comparable {
private float = 0.0f;
private String name;
public float getMark() {
return ;
}
public String getName() {
return name;
}
public Student(String name, float ) {
this. = ;
this.name = name;
}
public int compareTo(Object obj) {
if (obj instanceof Student) {
Student std = (Student) obj;
if (this. == std.)
return 0;
if (this. > std.)
return 1;
}
return -1;
}
}
package com.houlei;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
/**
* @author 侯磊
* Questions 类用于测试
*/
public class Questions {
public static void main(String[] args) throws IOException {
try {
System.out.println("/*** 题目 1 :要求存在测试文件 **/");
Question1.proccess("d:/st1.txt", "d:/st2.txt"); // Q1
System.out.println("/*** 题目 2 **/");
System.out.println(Question2.change(4285)); // Q2
System.out.println("/*** 题目 3 **/");
char[] resouce = { '你', '好', '啊', '哈', '哈', '。' };
System.out.println(Question3.search(resouce, 'a')); // Q3
System.out.println(Question3.search(resouce, '哈')); // Q3
System.out.println(Question3.search(null, '哈')); // Q3
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("/*** 题目 4 **/");
System.out.println(Question4.jion(new int[] { 1, 2, 3 })); // Q4
System.out.println("/*** 题目 5 :要求输入待转换的整数数字 **/");
System.out.println(Question5.printInput()); //Q5
System.out.println("/*** 题目 6 **/");
Question6.test("d:/st1.txt"); // Q6
System.out.println("/*** 题目 7 **/");
Question7.listPath("d:/"); // Q7
}}class Question7 {
public static void listPath(String pathName) {
File path = new File(pathName);
if (path.isDirectory()) {
File[] subPaths = path.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
} });
System.out.println("所有子目录如下:");
for (int i = 0; i < subPaths.length; i++) {
System.out.println("\t" + subPaths[i].getName());
}
File[] subFiles = path.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
});
System.out.println("所有文件如下:");
for (int i = 0; i < subFiles.length; i++) {
System.out.println("\t" + subFiles[i].getName());
}
}
}
}class Question6 {
private BufferedReader reader; private long lineNumber = 0; public Question6(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
} public String readLine() throws IOException {
String str = reader.readLine();
if (str != null) {
lineNumber++;
return lineNumber + ":" + str;
}
return str;
} /**
* 该方法用于测试
*
* @throws IOException
*/
public static void test(String fileName) throws IOException {
Question6 reader = new Question6(new FileInputStream(fileName));
String str;
while ((str = reader.readLine()) != null) {
System.out.println(str);
}
}
}class Question5 {
public static String printInput() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
br.close();
int num = Integer.parseInt(line);
StringBuffer sb = new StringBuffer();
while (num != 0) {
sb.insert(0, (num & 1));
num = num >>> 1;//左补0的右移运算,对应于题目要求的除2运算,要比题目要求的好些。
}
return sb.toString();
}
}class Question4 {
public static String jion(int[] nums) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < nums.length; i++) {
sb.append(nums[i]);
if (i < nums.length - 1)
sb.append(",");
}
return sb.toString();
}
}class Question3 {
public static int search(char[] resouce, char c)
throws IllegalAccessException {
if (resouce == null)
throw new IllegalAccessException("所传入的参数为null");
for (int i = 0; i < resouce.length; i++) {
if (c == resouce[i])
return i;
}
return -1;
}
}/**
* 第2个与第8个相同
*/
class Question2 {
private static String[] nums = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒","捌", "玖" };
private static String[] units = { "元", "拾", "佰", "仟", "万" };
public static String change(int money) {
StringBuffer sb = new StringBuffer();
if (money < 0)
return null;
int tm;
for (int i = 1, j = 0; true; i *= 10, j++) {
tm = money % (i * 10);
tm = tm / i;
if (tm == 0)
break;
sb.insert(0, nums[tm] + units[j]);
}
return sb.toString();
}
}/**
* 文件格式:每一行表示一个学生的一条记录信息。先是学生的姓名,然后是分号,最后是学生的成绩(也就是说,学生姓名和成绩之间以分号分隔)。
* Student类属于本题目
*/
class Question1 {
public static void proccess(String sourceFile, String destFile)
throws IOException {
// 读取所有的学生姓名和成绩
BufferedReader br = new BufferedReader(new FileReader(sourceFile));
LinkedList<Student> ss = new LinkedList<Student>();
String str;
double average = 0.0;
int count = 0;
Student highMarkStu = new Student(null, 0);
while ((str = br.readLine()) != null) {
String record[] = str.split(";|;");
float = Float.parseFloat(record[1]);
if (record[0].equals(""))
continue;
Student std = new Student(record[0], );
ss.add(std);
if ( >= highMarkStu.getMark())
highMarkStu = std;
average += ;
count++;
}
br.close();
average = average / count;
// 输出平均考试成绩、最高考试成绩,以及取得最高考试成绩的学生名单
StringBuffer sb = new StringBuffer();
sb.append("平均考试成绩:\t\t").append(average).append(" 分\r\n");
sb.append("最高考试成绩:\t\t").append(highMarkStu.getMark()).append(" 分\r\n");
sb.append("最高考试成绩的学生:\t").append(highMarkStu.getName()).append("\r\n");
FileWriter writer = new FileWriter(destFile);
writer.write(sb.toString());
writer.close();
}
}class Student implements java.lang.Comparable {
private float = 0.0f;
private String name;
public float getMark() {
return ;
}
public String getName() {
return name;
}
public Student(String name, float ) {
this. = ;
this.name = name;
}
public int compareTo(Object obj) {
if (obj instanceof Student) {
Student std = (Student) obj;
if (this. == std.)
return 0;
if (this. > std.)
return 1;
}
return -1;
}
}
import java.io.*;
import java.util.*;public class ChengJi {
ArrayList chengJiAL = new ArrayList(); //在不考虑速度的情况,随便创建List,用Map 会好点
String[] chengJiBiao;
//用于读成绩
public void duChengJi(String weiZhi) {
try {
FileReader fr = new FileReader(weiZhi); //用此是读取字符流 + Buffered 方便点可以 一读一行
BufferedReader br = new BufferedReader(fr); // 还方便可以解决 读中文的 出乱麻的情况
String chengJi;
while((chengJi=br.readLine()) != null) { //读到文件为Null为止
chengJiAL.add(chengJi); //象list中加元素
}
br.close();
} catch(Exception a) {
a.printStackTrace();
System.exit(0);
}
}
//在屏幕上打印 和 向 wenJianMing 中写入
public void daYingXieChengJi(String wenJianMing) {
this.zhuanHuanString();
this.paiXu();
try {
for(int i=0; i<chengJiBiao.length;i++) {
System.out.println(chengJiBiao[i]);
}
FileWriter fw = new FileWriter(wenJianMing); //写入字符流 ,可以写字符串
for(int i=0; i<chengJiBiao.length;i++) {
fw.write(chengJiBiao[i]);
fw.write("\n"); //忘记怎么写入换行了,不好意思
}
fw.close();
} catch(Exception a) {
a.printStackTrace();
System.exit(0);
}
}//下面将 list 转成 String
public void zhuanHuanString(){
Object[] chengJiTemp = chengJiAL.toArray(); //将list转成Objcet
chengJiBiao = new String[chengJiTemp.length]; //转换成String
for(int i=0; i<chengJiTemp.length;i++) { //现在有种 好像叫枚举的 可以很方便的用toArray()转换
chengJiBiao[i] = (String)chengJiTemp[i];
}
}
//最菜的排序
public void paiXu() {
for(int i=0; i<chengJiBiao.length;i++) {
String ssi = chengJiBiao[i].substring(panDuanDouHao(chengJiBiao[i])+1,chengJiBiao[i].length());
//substring() 重新构建String
//panDuanDouHao() 自己定义的一个 判断是中文 还是 英文 逗号的方法 ,并返回逗号所在值
Integer sii = new Integer(ssi);
for(int j=0; j<chengJiBiao.length;j++) {
String ssj = chengJiBiao[j].substring(panDuanDouHao(chengJiBiao[j])+1,chengJiBiao[j].length());
Integer sjj = new Integer(ssj);
if(sii.compareTo(sjj) < 0) { //compareTo()比较Integer 对象
String temp = chengJiBiao[i];
chengJiBiao[i] = chengJiBiao[j];
chengJiBiao[j] = temp;
}
}
}
}//判断中文或英文逗号并返回逗号所在值
public static int panDuanDouHao(String s) {
int index = 0;
if(s.indexOf(',') >=0) {
index = s.indexOf(',');
}else if(s.indexOf(',') >=0) {
index = s.indexOf(',');
}else {
index = -1;
}
return index;
}
public static void main(String[] args) {
ChengJi cj = new ChengJi();
cj.duChengJi("ChengJi.txt");//在文本中 加入
//王五,100
//张三,110
//历史,10
cj.daYingXieChengJi("ChengJiBiao.txt");
}
}
//本人很菜,刚自学不久,写的不好,不好意思 ,显丑了~~!!!
char[] daXieShuZi = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
char[] danWei = {'元','拾','佰','仟','万'};
public void zhuanHuan(int shuZi) {
if(shuZi > 100000 || shuZi<0) {
System.out.print("输入数大于10万 或 小于0");
return;
}
String shuZiString = ""+shuZi; //将数字转换为字符串
char[] danGeShuZi = shuZiString.toCharArray();//字符串转换为 字符数组, 将 shuZiString字符串
//转换成字符数组 danGeShuZi
int len = shuZiString.length(); //读取字符串位数,也就是钱的位数
for(int i=0 ;i<danGeShuZi.length; i++) {
String sTemp = new String(danGeShuZi[i]+""); //将字符转化为字符串,为了 转换为 Integer
Integer iTemp = new Integer(sTemp);
System.out.print(daXieShuZi[iTemp.intValue()]); //iTemp.intValue()将Integer 转化为int型
//char[]danGeShuZi的数字是多少,就取char[] daXieShuZi
//的多少,就可以拿到对应的大写
System.out.print(danWei[--len]); //上面用len,得到了位数,因为数组是0开始的,所以每次取值前自-1
}
}
public static void main(String[] args) {
ShuZiZhuanHuan szzh = new ShuZiZhuanHuan();
szzh.zhuanHuan(0);
}
}
public static int panDuan(char[] c1,char c2){
if(c1==null)
throw new IllegalArgumentException("传入的是NULL");//抛出异常
String s = new String(c1); //因为String类中有查找的方法,所以将char转成String
int len = s.indexOf(c2); //indexOf()方法是返回指定字符在此字符串中第一次出现处的索引
return len; //将len返回
}
public static void main(String[] args) {
char[] c = {'a','b','c','d'};
char c1 = 'e';
int len = PanDuan.panDuan(null,c1);
System.out.println(len);
}
}
public static void zhuanHuanPrint(int shuZi) {
String s = ""+shuZi; //转换成String,String中可以将字符串转化成字符数组
char[] c = s.toCharArray(); //转换成字符数组
for(int i=0; i<c.length; i++) {
System.out.print(c[i]);
if(i == c.length-1) { //判断是不是倒数第1个,是就退出循环,不打印最后一个
//逗号
break;
}
System.out.print(",");
}
}
public static void main(String[] args) {
int shuZi = 123456;
ZhuanHuan.zhuanHuanPrint(shuZi);
}
}
public static String zhuanHuanPrint(int shuZi) {
String s = ""+shuZi; //转换成String,String中可以将字符串转化成字符数组
char[] c = s.toCharArray(); //转换成字符数组
StringBuffer sb = new StringBuffer(); //有字符串缓冲区方便添加删除
for(int i=0; i<c.length; i++) {
sb.append(c[i]); //添加 数字
if(i == c.length-1) { //判断是不是倒数第2个,是就退出循环
break;
}
sb.append(",");//添加逗号
}
String ss = new String(sb);//满足题目转换成String
return ss;
}
public static void main(String[] args) {
int shuZi = 123456;
String s =ZhuanHuan.zhuanHuanPrint(shuZi);
System.out.println(s);
}
}
//编译javac ZhuanHuan2JinZhi.java
//运行java ZhuanHuan2JinZhi 123
public class ZhuanHuan2JinZhi {
public static void main(String[] args) {
if(args.length > 0 && args.length <= 1 ) { //利用args参数读取,命令行cmd输入的数字
//保证cmd只能输入1个
try {
Integer it =Integer.decode(args[0]); //如果不是纯数字会抛出异常,将args用Integer.decode()
//转化成int的封装类
int i = it.intValue(); //转化成数字
String s = Integer.toBinaryString(i); //用 Integer.toBinaryString()方法将int的数字
//转化成2进制
System.out.println(args[0]+"的2进制"+s);
}catch (NumberFormatException a) {
System.out.println("请输入数字");
} }else {
System.out.println("请输入1个数字");
}
}
}
//方法2:用System.in读取
public class ZhuanHuan2JinZhi {
public static void main(String[] args) {
try {
System.out.println("输入数字以 q 结束");
int data;
StringBuffer sb = new StringBuffer();
while((data = System.in.read()) != 'q') { //读取键盘输入,因键盘输入时int型,
//所以定义int data接受输入,在强制
//转换为char
sb.append((char)data);
}
String sbZS = new String(sb);
Integer it =Integer.decode(sbZS); //如果不是纯数字会抛出异常,将String sbZS用Integer.decode()
//转化成int的封装类
int i = it.intValue(); //转化成数字
String s = Integer.toBinaryString(i); //用 Integer.toBinaryString()方法将int的数字
//转化成2进制
System.out.println(sbZS+"的2进制"+s); }catch (NumberFormatException a) {
System.out.println("请输入数字");
} catch(Exception a) {
a.printStackTrace(); //打印错误堆栈信息
}
}
}
public static void duQuJavaPrintln(String wenJianMing) {
try {
FileReader fr = new FileReader(wenJianMing); //字符流,
BufferedReader br = new BufferedReader(fr);//缓冲字符流提供readLine()
//读一行的方法
int index = 1;
String s;
while((s =br.readLine()) != null) { //循环到br.readLine()读不到数据
System.out.println(index+": "+s);//打印行+:+内容
index++;//每读一行 +1
}
} catch(Exception a) {
a.printStackTrace();
System.exit(0);
}
}
public static void main(String[] args) {
DuQuJava.duQuJavaPrintln("Test.java");//因为方法是static的所以可以用类或直接调用
//Text.java是你要读取的文件 }
}
和我们写的for (初赋值; 判断; 矢代) 你也可以写成int i=0;
for( ; ; ) {
i++;
if(i==5) {
break;
}
}
//编译javac ZhuanHuan2JinZhi.java
//运行java ZhuanHuan2JinZhi 123
public class ZhuanHuan2JinZhi {
public static void main(String[] args) {
if(args.length > 0 && args.length <= 1 ) { //利用args参数读取,命令行cmd输入的数字
//保证cmd只能输入1个
try {
Integer it =Integer.decode(args[0]); //如果不是纯数字会抛出异常,将args用Integer.decode()
//转化成int的封装类
int i = it.intValue(); //转化成数字
String s = Integer.toBinaryString(i); //用 Integer.toBinaryString()方法将int的数字
//转化成2进制
System.out.println(args[0]+"的2进制"+s);
}catch (NumberFormatException a) {
System.out.println("请输入数字");
} }else {
System.out.println("请输入1个数字");
}
}
}
//方法2:用System.in读取
public class ZhuanHuan2JinZhi {
public static void main(String[] args) {
try {
System.out.println("输入数字以 q 结束");
int data;
StringBuffer sb = new StringBuffer();
while((data = System.in.read()) != 'q') { //读取键盘输入,因键盘输入时int型,
//所以定义int data接受输入,在强制
//转换为char
sb.append((char)data);
}
String sbZS = new String(sb);
Integer it =Integer.decode(sbZS); //如果不是纯数字会抛出异常,将String sbZS用Integer.decode()
//转化成int的封装类
int i = it.intValue(); //转化成数字
String s = Integer.toBinaryString(i); //用 Integer.toBinaryString()方法将int的数字
//转化成2进制
System.out.println(sbZS+"的2进制"+s); }catch (NumberFormatException a) {
System.out.println("请输入数字");
} catch(Exception a) {
a.printStackTrace(); //打印错误堆栈信息
}
}
}