English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

這個array程式是一次讀一個英文字,然後輸出像沙漏一樣形狀的"fancy word".
舉例來說, 如果輸入
2
HELLO
CAT

輸出就是:

HELLO
E L //E是對齊HELLO的E, L是對齊HELLO的第二個L
L //L是對齊HELLO的第一個L
E L //E是對齊HELLO的E, L是對齊HELLO的第二個L
HELLO

CAT
A //A是對齊CAT的A
CAT

下面是我的code,請程式高手幫我看看(37,38行的錯誤我不會改), 謝謝:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import static java.lang.System.*;

class FancyWord
{
private char[][] mat;
private String sent;

public FancyWord()
{
mat=new char[0][0];
sent="";
}

public FancyWord(String s)
{
s = sent;
int x = s.length();
mat = new Char[x][x];
int length = mat.length();

}

public void makeFancyWord()
{

for(int i=0; i {

int length = mat.length();
int mat [i][i] = sent.charAt(i);
int mat [i][len-(i+1)] = sent.charAt(len-(i+1));
}

}

public String toString()
{
String output="";
for(int r=0; r < mat.length; r++)
for(int c=0; c < mat.length; c++)
output+=mat[r][c];
//new line(separate different words)




return output+"\n\n";
}
}

public class Lab16b
{
public static void main( String args[] ) throws IOException
{
Scanner file = new Scanner(new File("lab16b.dat"));
int size = file.nextInt();
file.nextLine();
for(int i = 0; i {
String sentence = file.nextLine();
FancyWord test = new FancyWord(sentence);
test.makeFancyWord();
out.println(test);
}
}
}

2007-01-26 02:09:04 · 1 個解答 · 發問者 rebeccalee0510 2 in 電腦與網際網路 程式設計

1 個解答

請參考我的做法

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
class FancyWord {
private char[][] mat;
private String sent;

public FancyWord() {
mat=new char[0][0];
sent="";
}

public FancyWord(String s) {
sent = s;
int x = s.length();
mat = new char[x][x];
}

public void makeFancyWord() {
for(int i = 0; i < sent.length(); i++) {
int len = sent.length();
mat[i][i] = sent.charAt(i);
mat[i][len-(i+1)] = sent.charAt(len-(i+1));
}
}

public String toString() {
String output= sent + '\n';
for(int r = 1; r < mat.length - 1; r++) {
for(int c = 0; c < mat[r].length; c++) {
output += String.valueOf(mat[r][c]);
}
output += '\n';
}
//new line(separate different words)
return output + sent + "\n\n";
}
}
public class Lab16b {
public static void main(String args[]) throws IOException {
Scanner file = new Scanner(new File("lab16b.dat"));
int size = file.nextInt();
file.nextLine();
for(int i = 0; i < size; i++) {
String sentence = file.nextLine();
FancyWord test = new FancyWord(sentence);
test.makeFancyWord();
System.out.println(test);
}
}
}

2007-01-29 08:45:46 · answer #1 · answered by ? 7 · 0 0

fedest.com, questions and answers