求高手 实现文字竖排 本帖最后由 jiasuer008 于 2011-03-04 23:24:36 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package day05;import java.util.Arrays;import java.util.Scanner;public class ToVolign { /** * 实现文字的竖排 */ public static void main(String[] args) { Scanner sc =new Scanner(System.in); while(true){ System.out.print("输入一行方字:"); String str=sc.nextLine(); if(str.equals("quit")){ System.out.println("下次再见,bye!"); break; } String s=valign(str,4); System.out.print(s); } } /** * 将输入文字竖排输出为字符串 * * @param str * 输入的文本 * @param size * 每个竖排包含的文字数量 * @return 竖排文本, */ public static String valign(String str, int size) { int rows = size; // int cols=str.length()%size==0 ? // str.length()/size:str.length()/size+1; int cols = str.length() / size; //cols是列 //System.out.println(size); if (str.length() % size != 0) { cols++; } System.out.println("列数是"+cols); char[] src = new char[cols * rows]; //rows 是行数 Arrays.fill(src, '~');// 将src数组变成(~~~~~~~~~~~~~}12个 System.arraycopy(str.toCharArray(), 0, src, 0, str.length()); // 复制数组变成{开始测试我们的软件系统~} // 如果输入"开始测试我们的软件系统" 将str数组的零位开始赋给src数组的零位 数组长度 int i = 0; char[] chs = new char[cols * rows]; for (int col = cols - 1; col >= 0; col--) { for (int row = 0; row <= rows - 1; row++) { int idx = row * cols + col;// 目标数组位置序号 //System.out.println(row+"+"+cols+"+"+col+"+"+idx ); chs[idx] = src[i++];// str.charAt(i++) } } String s = ""; for (int j = 0; j < chs.length; j++) { char c = chs[j]; System.out.println(j); s += c; //System.out.println(s); if ((j + 1) % cols == 0) { s += "\n"; } } return s; }}我的没用2维数组 package day04;import java.util.Scanner;/** * 打印一首诗 * * */public class Valign { public static void main(String[] args) { Scanner console=new Scanner(System.in); String str=console.nextLine(); char[][] arr=new char[5][4]; int count=0; System.out.println("原序:"); for (int i = arr[0].length-1; i >=0; i--) { for (int j = 0; j < arr.length; j++) { arr[j][i]=str.charAt(count++); System.out.print(arr[j][i]); } System.out.println(); } System.out.println("现序:"); for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { System.out.print(arr[i][j]); } System.out.println(); } }}这个也是刚写的,只能写规定字符数的 NumberFormatException异常问题,求解释。 java循环问题 [业内传闻]java太简单,学java全是垃圾,天天用别人的API,自己就是个民工. io基础,你懂的 利用线程技术来制作动画 请问在这个程序中的main方法中到底有几个并发线程啊? 学安卓要学JAVA SE的SWING和AWT么? JAVA,,,初学者,, 使用str.replaceAll("\'", "\\\'")为什么不能正确替换? java里Frame框架如何退出 1,2,4,8,32....,有这么一组数。请求出第32位数值是多少? 多窗口中的键盘监视响应问题
import java.util.Scanner;public class ToVolign { /**
* 实现文字的竖排
*/
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
while(true){
System.out.print("输入一行方字:");
String str=sc.nextLine();
if(str.equals("quit")){
System.out.println("下次再见,bye!");
break;
}
String s=valign(str,4);
System.out.print(s);
}
} /**
* 将输入文字竖排输出为字符串
*
* @param str
* 输入的文本
* @param size
* 每个竖排包含的文字数量
* @return 竖排文本,
*/
public static String valign(String str, int size) {
int rows = size;
// int cols=str.length()%size==0 ?
// str.length()/size:str.length()/size+1;
int cols = str.length() / size; //cols是列
//System.out.println(size);
if (str.length() % size != 0) {
cols++;
}
System.out.println("列数是"+cols);
char[] src = new char[cols * rows]; //rows 是行数
Arrays.fill(src, '~');// 将src数组变成(~~~~~~~~~~~~~}12个
System.arraycopy(str.toCharArray(), 0, src, 0, str.length()); // 复制数组变成{开始测试我们的软件系统~}
// 如果输入"开始测试我们的软件系统" 将str数组的零位开始赋给src数组的零位 数组长度
int i = 0;
char[] chs = new char[cols * rows];
for (int col = cols - 1; col >= 0; col--) {
for (int row = 0; row <= rows - 1; row++) {
int idx = row * cols + col;// 目标数组位置序号
//System.out.println(row+"+"+cols+"+"+col+"+"+idx );
chs[idx] = src[i++];// str.charAt(i++)
}
}
String s = "";
for (int j = 0; j < chs.length; j++) {
char c = chs[j];
System.out.println(j);
s += c;
//System.out.println(s);
if ((j + 1) % cols == 0) {
s += "\n";
}
}
return s;
}
}我的没用2维数组
/**
* 打印一首诗
*
*
*/
public class Valign {
public static void main(String[] args) {
Scanner console=new Scanner(System.in);
String str=console.nextLine();
char[][] arr=new char[5][4];
int count=0;
System.out.println("原序:");
for (int i = arr[0].length-1; i >=0; i--) {
for (int j = 0; j < arr.length; j++) {
arr[j][i]=str.charAt(count++);
System.out.print(arr[j][i]);
}
System.out.println();
}
System.out.println("现序:");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
}这个也是刚写的,只能写规定字符数的