如何解决在结构中动态输入值并打印这些值
我为结构螺柱编写了此代码,该代码将输入要存储其记录的学生人数,然后打印出来。我不确定我到底要去哪里。
#include<stdio.h>
struct stud{
int id;
char name[50];
float points;
}s[1000];
int main(){
int i,n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&s[i].id);
scanf("%c",s[i].name);
scanf("%f",&s[i].points);
}
for(i=0;i<n;i++){
printf("ID: %d\n",s[i].id);
printf("First Name: %c\n",s[i].name);
printf("Points: %f\n",s[i].points);
}
return 0;
}
解决方法
更改
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}
class Board extends React.Component {
renderSquare(i) {
return (
<Square
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
}
render() {
return (
<div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
class Game extends React.Component {
constructor(props) {
super(props);
this.state = {
history: [{
squares: Array(9).fill(null)
}],stepNumber: 0,xIsNext: true,};
}
handleClick(i) {
const history = this.state.history.slice(0,this.state.stepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? 'X' : 'O';
this.setState({
history: history.concat([{
squares: squares,}]),stepNumber: history.length,xIsNext: !this.state.xIsNext,});
}
jumpTo(step) {
this.setState({
stepNumber: step,xIsNext: (step % 2) === 0,});
}
render() {
const history = this.state.history;
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);
// history of moves each as a <li>
const moves = history.map((step,move) => {
const desc = move ?
'Go to move #' + move :
'Go to game start';
return (
<li key={move}>
<Button onClick={() => this.jumpTo(move)}>{desc}</Button>
</li>
);
});
// determine winner
let status;
if (winner) {
status = 'Winner: ' + winner;
} else {
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
}
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={(i) => this.handleClick(i)} // this guy
/>
</div>
<div className="game-info">
<div>{status}</div>
<ol>{moves}</ol>
</div>
</div>
);
}
}
至少
scanf("%c",s[i].name);
,也与 scanf("%49s",s[i].name);
调用相同。 printf()
用于单个%c
,对于字符串,则需要char
。另外,不要忘记检查%s
的返回值是否成功。
话虽如此,最好使用fgets()
进行用户输入。像
scanf()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。