for (int i = 0; i < len / 3; i++) { temp += fee.subSequence(i * 3, 3 + 3 * i) + ","; } feeb = new StringBuffer(temp); student[4] = head + feeb.reverse().toString() + ".00"; System.out.println("添加成功,您输入的学生信息如下: "); System.out .print("First Name\t Last Name\t\t ID\t\t\tMajor\t\t\tFee\n"); toString(student); new studentList().addNewStudent(student, file); System.out.printf("\n"); return true; }
时间: 2024-03-29 16:36:03 浏览: 91
这段代码的作用是将分割后的字符串组合成最终的带有美元符号和两位小数的字符串,并将其添加到学生信息数组的第五个位置。首先,使用for循环遍历分割后的字符串,每三个字符加一个逗号,并将结果添加到temp字符串中。接着,将temp字符串转换成StringBuffer对象feeb,并对其进行倒序操作。最后,将头部字符串head、倒序后的feeb字符串和美元符号以及两位小数组合成完整的费用字符串,并将其添加到学生信息数组的第五个位置。接下来,输出添加成功和学生信息表头。然后,调用toString方法输出学生信息,并将学生信息添加到学生列表中。最后,输出一个换行符并返回添加学生信息成功的布尔值true。
相关问题
String temp = ""; String head = null; int len = fee.length(); if (len % 3 == 0) { len--; head = fee.subSequence(0, 3) + ""; } else { head = fee.subSequence(0, len % 3) + ""; } StringBuffer feeb = new StringBuffer(fee);
这段代码的作用是将费用转换为带有逗号的形式。首先定义一个空字符串temp和一个头部字符串head,然后获取费用字符串的长度len。如果len能被3整除,那么将len减1并将费用字符串中的前三个字符赋值给head。否则,将费用字符串中的前len%3个字符赋值给head。接着,将费用字符串fee转换成StringBuffer对象feeb。
修改下列代码错误#include<iostream> #include<vector> namespace my_seq { class Sequence { public: Sequence() { // 将vector初始化为500个0 data = std::vector<int>(500, 0); size = 0; } void set(std::vector<int>& seq) { size = seq.size(); for (int i = 0; i size size; ++i) { data[i] = seq[i]; } } int findSubSeq(std::vector<int>& subSeq) { int subSeqSize = subSeq.size(); for (int i = 0; i <= size - subSeqSize; ++i) { bool found = true; for (int j = 0; j sub subSeqSize; ++j) { if (data[i + j] != subSeq[j]) { found = false; break; } } if (found) { return i + 1; } } return -1; } private: std::vector<int> data; int size; }; } int main() { int m; std::cin >> m; std::vector<int> seq(m); for (int i = 0; i m m; ++i) { std::cin >> seq[i]; } my_seq::Sequence mySeq; mySeq.set(seq); int n; std::cin >> n; std::vector<int> subSeq(n); for (int i = 0; i < n; ++i) { std::cin >> subSeq[i]; } int start = mySeq.findSubSeq(subSeq); if (start == -1) { std::cout << "Subsequence not found" std std::endl; } else { std::cout << "Subsequence found at position " << start std std::endl; } return 0; }
代码中有两个错误:
1.在第28行,代码少了一个分号。
2.在第15行,应该是 i < size,而不是 i size size。
下面是修改后的代码:
```
#include<iostream>
#include<vector>
namespace my_seq {
class Sequence {
public:
Sequence() {
// 将vector初始化为500个0
data = std::vector<int>(500, 0);
size = 0;
}
void set(std::vector<int>& seq) {
size = seq.size();
for (int i = 0; i < size; ++i) {
data[i] = seq[i];
}
}
int findSubSeq(std::vector<int>& subSeq) {
int subSeqSize = subSeq.size();
for (int i = 0; i <= size - subSeqSize; ++i) {
bool found = true;
for (int j = 0; j < subSeqSize; ++j) {
if (data[i + j] != subSeq[j]) {
found = false;
break;
}
}
if (found) {
return i + 1;
}
}
return -1;
}
private:
std::vector<int> data;
int size;
};
}
int main() {
int m;
std::cin >> m;
std::vector<int> seq(m);
for (int i = 0; i < m; ++i) {
std::cin >> seq[i];
}
my_seq::Sequence mySeq;
mySeq.set(seq);
int n;
std::cin >> n;
std::vector<int> subSeq(n);
for (int i = 0; i < n; ++i) {
std::cin >> subSeq[i];
}
int start = mySeq.findSubSeq(subSeq);
if (start == -1) {
std::cout << "Subsequence not found" << std::endl;
} else {
std::cout << "Subsequence found at position " << start << std::endl;
}
return 0;
}
```
请注意,我只是根据常见错误进行了修改,如果还有其他问题,您需要提供更多信息。
阅读全文