我尝试从文本文件中读取整行,并以不同的方式显示它们.例如,
123456J;Gabriel;12/12/1994;67;67;89;
Run Code Online (Sandbox Code Playgroud)
但控制台的结果如下:
123456J Gabriel 72(which is average of three numbers)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
public class Student{
String adminNo;
String name;
GregorianCalendar birthDate;
int test1,test2,test3;
public Student(String adminNo,String name,String birthDate,int test1, int test2, int test3){
this.adminNo = adminNo;
this.name = name;
this.birthDate = MyCalendar.convertDate(birthDate);
this.test1 = test1;
this.test2 = test2;
this.test3 = test3;
}
public Student(String studentRecord){
String strBirthDate;
Scanner sc = new Scanner(studentRecord);
sc.useDelimiter(";");
adminNo = sc.next();
name = sc.next();
strBirthDate = sc.next();
birthDate = MyCalendar.convertDate(strBirthDate.toString());
test1 = sc.nextInt();
test2 = …Run Code Online (Sandbox Code Playgroud) 我从数据库中检索数据并通过数组循环它以显示相似的数量.
public void SetUpLikeAmount() {
int likes = 0;
ArrayList <Integer> likeArray = new ArrayList <Integer>();
for (int count = 0; count < likeArray.size();count++){
// Set Up Database Source
db.setUp("IT Innovation Project");
String sql = "Select likeDislike_likes from forumLikeDislike WHERE topic_id = "
+ topicId + "";
ResultSet resultSet = null;
// Call readRequest to get the result
resultSet = db.readRequest(sql);
try {
while (resultSet.next()) {
likeArray.add(Integer.parseInt(resultSet.getString("likeDislike_likes")));
likes += likeArray.get(count);
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}
} …Run Code Online (Sandbox Code Playgroud) 我试图在3个输入中找到最小的数字.这是我的代码:
int main ()
{
double x = 4.0;
double y = 5.0;
double z = 3.0;
smallest(x,y,z);
cout << smallest << endl;
system("PAUSE");
}
double smallest(double x, double y, double z)
{
double smallest;
if ((x < y)||(x< z)) {
smallest = x;
} else if ((y < z)||(y < x)) {
smallest = y;
} else {
smallest = z;
}
return smallest;
}
Run Code Online (Sandbox Code Playgroud)
但是,我一直在收到错误.它声明了我在main方法中使用未声明标识符的最小方法.这在使用eclipse但不是visual studio时有效.有人可以向我解释原因吗?
提前致谢.
更新部分.
所以我试着对这个程序进行验证.我想确保用户只输入号码,这里是我的代码:
double x, y, z;
bool correct_input = false;
do{ …Run Code Online (Sandbox Code Playgroud) 我试图反转数组中的元素.例如,我得到了:
15 69 94 52 97 51 17 18 50 18
Run Code Online (Sandbox Code Playgroud)
它会在:
18 50 18 17 51 97 52 94 69 15 (which is reversed)
Run Code Online (Sandbox Code Playgroud)
但是,这是我从我的代码中获得的:
51 69 94 52 97 17 18 50 18 15 (the sequence are jumbled out which I have no idea why)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
void reverse(int num_array[], const int& size);
int main ()
{
const int size = 10;
int num_array[size];
srand (time(NULL));
for (int count = 0; count< size ; count++){
/* generate secret number …Run Code Online (Sandbox Code Playgroud) 我在C++中有一个关于指针的问题:
编写一个函数来计算浮点数据数组的平均值:
双倍平均值(double*a,int size)
在函数中,使用指针变量而不是整数索引来遍历数组元素.
这是我的解决方案:
int main()
{
const int size = 5;
double num_array[] = {2,2,3,4,5};
double* a = num_array;
cout << average(a,size);
cout << endl;
system("PAUSE");
return 0;
}
double average(double* a,const int size)
{
double total = 0;
for (int count =0; count< size; count++){
total = total + *(a + count);
}
return total/size;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我对平均函数中指针的for循环有疑问.如果我用for循环替换语句:
total = total + a*;
Run Code Online (Sandbox Code Playgroud)
(我认为我们应该这样做,将数组中的所有数字加起来但不幸的是它给了我错误的答案)
那么*(a + count)是做什么的?如果可能的话,有人可以简单介绍一下它的工作原理吗?
提前致谢.
我试图循环数组并在C++中获取元素.这是我的代码:
int result;
int index_array [] = {11,12,13,14,15,16,17,18,19,20};
for (int count =0; count < index_array.length() ; count++){
if(count%2 == 0){
cout << "Elements at the even index are " << index_array[count] << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我将for循环更改为:
for (int count =0; count < 10 ; count++){
Run Code Online (Sandbox Code Playgroud)
没有错误,因为我的数组只包含10个项目.但是,如果我使用该.length()方法,则表示必须具有类类型的错误.我不知道它是什么,因为如果它在Eclipse中,则包含更详细的错误描述.有人可以告诉我有什么问题吗?
更新的答案:
for (int count =0; count < sizeof(index_array)/sizeof(index_array [0]) ; count++){
if((count+1)%2 == 0){
cout << "Elements at the even index are " << index_array[count] << endl;
} …Run Code Online (Sandbox Code Playgroud)