小编Tho*_*mas的帖子

用值返回方法打印东西

假设我有一个方法:

public static int square(int x) {
    System.out.print(5);
    return x*x;
}
Run Code Online (Sandbox Code Playgroud)

我在main方法中调用此方法如下:

System.out.print("The square of the number "+7+" is "+square(7));
Run Code Online (Sandbox Code Playgroud)

我期待输出

The square of the number 7 is 549
Run Code Online (Sandbox Code Playgroud)

但是,实际输出是

5The square of the number 7 is 49
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

java methods

5
推荐指数
2
解决办法
105
查看次数

最后一行打印两次(C++)

我有一个名为scores.txt的文本文件,其中包含以下数据:

John T Smith 90
Eric K Jones 103
Ram S Krishnan 25
Marie A Bell 50
Run Code Online (Sandbox Code Playgroud)

我正在尝试从此文件中读取数据并使用以下C++代码进行打印:

ifstream input;
input.open("scores.txt");
string firstName;
char mi;
string lastName;
int score;

while (input) {
    input>>firstName>>mi>>lastName>>score;
    cout<<firstName<<" "<<mi<<" "<<lastName<<" "<<score<<endl;
}

input.close();
cout<<"Done"<<endl;
Run Code Online (Sandbox Code Playgroud)

输出是:

John T Smith 90
Eric K Jones 103
Ram S Krishnan 25
Marie A Bell 50
Marie A Bell 50
Run Code Online (Sandbox Code Playgroud)

为什么最后一行(Marie A Bell 50)被打印两次?我怎样才能防止这种情况发生?

c++

1
推荐指数
1
解决办法
564
查看次数

标签 统计

c++ ×1

java ×1

methods ×1