假设我有一个方法:
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)
为什么会这样?
我有一个名为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)被打印两次?我怎样才能防止这种情况发生?