Java 方法以脱机方式打印数组元素,并按顺序使用连字符
因此,我试图打印出一个由用户输入创建的数组内容(大小必须是奇数,介于3和11之间),它是数组的列和行的两倍。在某些地方使用字符来制作图案。除了格式外,一切都很好。它打印正确,但不在正确的位置。由于某些原因,字符被关闭,连字符以错误的顺序打印。它们应该在阵列前后打印。连字符的数量是正确的,只是我应该Java 方法以脱机方式打印数组元素,并按顺序使用连字符,java,arrays,loops,format,output,Java,Arrays,Loops,Format,Output,因此,我试图打印出一个由用户输入创建的数组内容(大小必须是奇数,介于3和11之间),它是数组的列和行的两倍。在某些地方使用字符来制作图案。除了格式外,一切都很好。它打印正确,但不在正确的位置。由于某些原因,字符被关闭,连字符以错误的顺序打印。它们应该在阵列前后打印。连字符的数量是正确的,只是我应该 ----------- * * * * * --------
-----------
*
*
*
*
*
-----------
但是我得到了
*
*
*
*
*
-----------
-----------
我不知道为什么另一条连字符线如此遥远,它的偏离程度几乎是喜剧。这是密码
public static void main (String [] args) {
// instance variables - replace the example below with your own
int dimension = findDimension();
char [] [] array2d = new char [dimension] [dimension];
char star = '*';
array2d = leftDiagonal(star, dimension);
print(array2d);
}
public static int findDimension() {
int dimension = 0;
Scanner keybd = new Scanner(System.in);
do {
System.out.print("Enter an odd integer between 3 and 11 please: ");
dimension = keybd.nextInt();
} while (dimension%2 == 0);
return dimension;
}
这就是问题所在,因为它是完成所有打印但不确定的方法。我把连字符的打印语句放在循环前后,所以我不明白它为什么会这样做。在打印每个元素之前,也应该有一个空格,这就是为什么我在print语句中添加了“”,但它似乎没有任何作用
public static void print(char [] [] arrayParam) {
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
System.out.print("-");
}
System.out.println();
for( int row = 0; row < arrayParam.length; row++) {
for (int column = 0; column < arrayParam.length; column++) {
System.out.print(" " + arrayParam[row][column]);
}
}
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
System.out.print("-");
}
}
公共静态无效打印(char[][]arrayParam){
对于(int-hyphen=0;hyphen<(arrayParam.length*2)+1;hyphen++){
系统输出打印(“-”);
}
System.out.println();
for(int row=0;row
下面是代码的其余部分
public static char [] [] leftDiagonal(char starParam, int dimenParam) {
char [] [] leftD = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
if (i == j) {
System.out.print(starParam);
}
else {
System.out.print(' ');
}
}
System.out.println();
}
return leftD;
}
publicstaticchar[][]左对角(charstarparam,int-dimenParam){
char[][]leftD=新的char[dimenParam][dimenParam];
对于(inti=0;i
更新:在考虑了别人告诉我的情况后,我成功地获得了正确的代码。给你,再次感谢大家的帮助
public static void main (String [] args) {
int dimension = findDimension();
char [] [] array2d = new char [dimension] [dimension];
char star = '*';
array2d = leftDiagonal(star, dimension);
print(array2d);
array2d = rightDiagonal(star, dimension);
System.out.println();
print(array2d);
}
public static int findDimension() {
int dimension = 0;
Scanner keybd = new Scanner(System.in);
do {
System.out.print("Enter an odd integer between 3 and 11 please: ");
dimension = keybd.nextInt();
} while (dimension%2 == 0);
return dimension;
}
public static void print(char [] [] arrayParam) {
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
System.out.print("-");
}
System.out.println();
for(char[] row : arrayParam)
{
for(char c : row)
System.out.print(" " + c);
System.out.printf("\n");
}
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
System.out.print("-");
}
}
public static char [] [] leftDiagonal(char starParam, int dimenParam) {
char [] [] leftD = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
if (i == j)
leftD[i][j] = starParam;
else
leftD[i][j] = ' ';
}
}
return leftD;
}
publicstaticvoidmain(字符串[]args){
int-dimension=findDimension();
char[][]array2d=新的char[dimension][dimension];
字符星='*';
array2d=左对角线(星形,尺寸);
打印(array2d);
array2d=右对角线(星形,尺寸);
System.out.println();
打印(array2d);
}
公共静态int findDimension(){
整数维=0;
Scanner keybd=新扫描仪(System.in);
做{
System.out.print(“请输入一个介于3和11之间的奇数整数:”);
维度=keybd.nextInt();
}而(维度%2==0);
返回维度;
}
公共静态无效打印(字符[][]数组参数){
对于(int-hyphen=0;hyphen<(arrayParam.length*2)+1;hyphen++){
系统输出打印(“-”);
}
System.out.println();
用于(字符[]行:arrayParam)
{
for(字符c:行)
系统输出打印(“+c”);
System.out.printf(“\n”);
}
对于(int-hyphen=0;hyphen<(arrayParam.length*2)+1;hyphen++){
系统输出打印(“-”);
}
}
公共静态字符[][]左对角(字符starParam,int dimenParam){
char[][]leftD=新的char[dimenParam][dimenParam];
对于(inti=0;i
您的代码中有几个问题,我已经修复了它们,并在代码中提到了它们:
import java.util.Scanner;
public class PrintShape
{
public static void main (String [] args)
{
int dimension = findDimension();
char [] [] array2d = new char [dimension] [dimension];
char star = '*';
array2d = leftDiagonal(star, dimension);
print(array2d);
}
public static int findDimension()
{
int dimension = 0;
Scanner keybd = new Scanner(System.in);
do {
System.out.print("Enter an odd integer between 3 and 11 please: ");
dimension = keybd.nextInt();
} while (dimension%2 == 0);
return dimension;
}
public static void print(char [] [] arrayParam)
{
// i cant understand why are you printing so many hyphen "(arrayParam.length*2)+1"
// so i left it on you
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++)
System.out.print("-");
System.out.println();
for(char[] row : arrayParam)
{
for(char c : row)
System.out.print(c);
System.out.printf("\n ");
}
//Problem:
// this "-" starts where the array printing end as you are not printing any newline ..
// it starts printing hyphen on the same line.. that why you get the second row of "-" so far
//Fixed:
System.out.printf("\n");
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++)
System.out.print("-");
}
public static char [] [] leftDiagonal(char starParam, int dimenParam)
{
char [] [] leftD = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++)
{
for (int j = 0; j < dimenParam; j++)
{
if (i == j)
{
// Probelm : you are just printing the "*"and no saving it in the array
// thats why you are getting only blank spaces in the "print()"
System.out.print(starParam);
leftD[i][j] = starParam;
}
else
System.out.print(' ');
// soution : save it in the array
}
System.out.println();
}
return leftD;
}
}
import java.util.Scanner;
公共类打印形状
{
公共静态void main(字符串[]args)
{
int-dimension=findDimension();
char[][]array2d=新的char[dimension][dimension];
字符星='*';
array2d=左对角线(星形,尺寸);
打印(array2d);
}
公共静态int findDimension()
{
整数维=0;
Scanner keybd=新扫描仪(System.in);
做{
System.out.print(“请输入一个介于3和11之间的奇数整数:”);
维度=keybd.nextInt();
}而(维度%2==0);
返回维度;
}
公共静态无效打印(字符[][]数组参数)
{
//我不明白你为什么要打印这么多连字符“(arrayParam.length*2)+1”
//所以我把它留给你了
对于(int-hyphen=0;hyphen<(arrayParam.length*2)+1;hyphen++)
系统输出打印(“-”);
System.out.println();
用于(字符[]行:arrayParam)
{
for(字符c:行)
系统输出打印(c);
System.out.printf(“\n”);
}
//问题:
//当您不打印任何换行时,此“-”从数组打印结束处开始。。
//它开始在同一行上打印连字符。这就是到目前为止第二行是“-”的原因
//固定的:
System.out.printf(“\n”);
对于(int-hyphen=0;hyphen<(arrayParam.length*2)+1;hyphen++)
系统输出打印(“-”);
}
公共静态字符[][]左对角(字符starParam,int dimenParam)
{
char[][]leftD=新的char[dimenParam][dimenParam];
对于(int i=0;i