格式化二维数组以具有加法问题的结构
Formatting a 2D Array to Have the Structure of an Addition Problem
我正在尝试编写一个程序,该程序涉及从 .txt 文件中读取行,并通过将它们转换为大小为 25 的 int[] 数组来将它们加在一起。我决定采用二维数组方法(已经在这个class)中走出了"what we've learned",将多个数组放在一起。
上图是我教授描述的加法。我们将在字符串行中找到的整数附加到具有 25 个零的数组末尾。例如,如果 .txt 文件的一行显示为“204 435 45”,那么我们会 return 这样:
0000000000000000000000204
0000000000000000000000435
0000000000000000000000045
然后,我们将按照照片 link 中提到的 "elementary arithmetic" 进行操作。现在这是我到目前为止得到的:
//This is the total overall size of the arrays (with all the zeroes)
public static final int ARRSIZE = 25;
//This is the majority of numbers to add on the biggest line in the .txt file
//This is kind of irrelevant here, but it means we'll always an 8 long array of arrays
public static final int MAXFACTONALINE = 8;
public static void breakTwo(String line)
{
//Changed the value of the parameter for testing purposes
line = "204 435 45";
int[][] factors = new int[MAXFACTONALINE][25];
//int[] nums = new int[ARRSIZE];
int determine = 0;
boolean isSpace = false;
for(int i = 0; i < line.length(); i++)
{
String breakdown = line.substring(line.length() - 1 - i, line.length() - i);
if(breakdown.equals(" "))
{
isSpace = true;
determine++;
}
if(breakdown.equals(""))
break;
if(!isSpace)
{
int temp = Integer.parseInt(breakdown);
factors[determine][factors.length - i] = temp;
}
isSpace = false;
i = 0;
}
//To do: Implement another method to carry on with the above processing
}
我打算在这里做的是将这三个数字分开(因为它们由空格分隔)并将它们放入自己的数组中,就像我上面输入的 3 个数组一样。
我的输出通常是在随机索引处获得的数字,我真的不知道如何掌握它们的去向。有人可以帮我确定如何像上面示例中那样将它们全部放在右侧吗?非常感谢
您可以利用内置函数 split(String regex)
为您拆分线段。因为你知道它永远是白色的 space,line.split(" ")
将 return 一个 {"204", "435", "45"} 的数组。
之后,计算字符串的长度并将其与仅包含 25 - number.length
个前导 0 的字符串连接起来。
public static void breakTwo(String line)
{
String [] numbers = line.split(" ");
String [] zeros = new String[numbers.length];
for (int i = 0; i < numbers.length; i++) {
zeros[i] = "0";
for (int j = 0; j < ARRSIZE - numbers[i].length() - 1; j++) {
zeros[i] += "0";
}
zeros[i] += numbers[i];
System.out.println(zeros[i]);
}
}
breakTwo("204 435 45")
的输出是
0000000000000000000000204
0000000000000000000000435
0000000000000000000000045
我正在尝试编写一个程序,该程序涉及从 .txt 文件中读取行,并通过将它们转换为大小为 25 的 int[] 数组来将它们加在一起。我决定采用二维数组方法(已经在这个class)中走出了"what we've learned",将多个数组放在一起。
0000000000000000000000204
0000000000000000000000435
0000000000000000000000045
然后,我们将按照照片 link 中提到的 "elementary arithmetic" 进行操作。现在这是我到目前为止得到的:
//This is the total overall size of the arrays (with all the zeroes)
public static final int ARRSIZE = 25;
//This is the majority of numbers to add on the biggest line in the .txt file
//This is kind of irrelevant here, but it means we'll always an 8 long array of arrays
public static final int MAXFACTONALINE = 8;
public static void breakTwo(String line)
{
//Changed the value of the parameter for testing purposes
line = "204 435 45";
int[][] factors = new int[MAXFACTONALINE][25];
//int[] nums = new int[ARRSIZE];
int determine = 0;
boolean isSpace = false;
for(int i = 0; i < line.length(); i++)
{
String breakdown = line.substring(line.length() - 1 - i, line.length() - i);
if(breakdown.equals(" "))
{
isSpace = true;
determine++;
}
if(breakdown.equals(""))
break;
if(!isSpace)
{
int temp = Integer.parseInt(breakdown);
factors[determine][factors.length - i] = temp;
}
isSpace = false;
i = 0;
}
//To do: Implement another method to carry on with the above processing
}
我打算在这里做的是将这三个数字分开(因为它们由空格分隔)并将它们放入自己的数组中,就像我上面输入的 3 个数组一样。
我的输出通常是在随机索引处获得的数字,我真的不知道如何掌握它们的去向。有人可以帮我确定如何像上面示例中那样将它们全部放在右侧吗?非常感谢
您可以利用内置函数 split(String regex)
为您拆分线段。因为你知道它永远是白色的 space,line.split(" ")
将 return 一个 {"204", "435", "45"} 的数组。
之后,计算字符串的长度并将其与仅包含 25 - number.length
个前导 0 的字符串连接起来。
public static void breakTwo(String line)
{
String [] numbers = line.split(" ");
String [] zeros = new String[numbers.length];
for (int i = 0; i < numbers.length; i++) {
zeros[i] = "0";
for (int j = 0; j < ARRSIZE - numbers[i].length() - 1; j++) {
zeros[i] += "0";
}
zeros[i] += numbers[i];
System.out.println(zeros[i]);
}
}
breakTwo("204 435 45")
的输出是
0000000000000000000000204
0000000000000000000000435
0000000000000000000000045