scala读取data.txt,第六列为成绩列,求出语文、数学、英语相加后的三门总成绩
时间: 2024-04-29 22:25:19 浏览: 9
您好!这个问题是关于Scala读取txt文件并计算总成绩的问题。可以使用Scala的文件读取和字符串分割功能,将每行数据分割成数组,再将第6列的成绩转换为整数类型相加,得到每个学生的总成绩。代码示例如下:
```scala
import scala.io.Source
val fileName = "data.txt"
val lines = Source.fromFile(fileName).getLines()
// 计算总成绩
for (line <- lines) {
val cols = line.split("\t")
val sum = cols(3).toInt + cols(4).toInt + cols(5).toInt
println(s"学生${cols(0)}的总成绩为$sum")
}
```
希望能够帮到您!
相关问题
scala编程,读取data.txt,第六列为成绩列,求出语文数学英语三门总成绩,并统计总成绩大于150分的13班的男生有几个
很好的问题!以下是一个简单的贪吃蛇游戏的C语言实现:
```
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define width 20
#define height 20
int x, y, foodX, foodY, score, gameover;
int tailX[100], tailY[100];
int nTail = 0;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
enum eDirection dir;
void Setup()
{
gameover = 0;
dir = STOP;
x = width / 2;
y = height / 2;
foodX = rand() % width;
foodY = rand() % height;
score = 0;
}
void Draw()
{
system("cls");
for (int i = 0; i < width + 2; i++)
printf("#");
printf("\n");
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
printf("#");
if (i == y && j == x)
printf("O");
else if (i == foodY && j == foodX)
printf("F");
else
{
int printTail = 0;
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
printf("o");
printTail = 1;
}
}
if (!printTail)
printf(" ");
}
if (j == width - 1)
printf("#");
}
printf("\n");
}
for (int i = 0; i < width + 2; i++)
printf("#");
printf("\n");
printf("Score: %d\n", score);
}
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'x':
gameover = 1;
break;
}
}
}
void Logic()
{
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++)
{
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
}
if (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;
for (int i = 0; i < nTail; i++)
if (tailX[i] == x && tailY[i] == y)
gameover = 1;
if (x == foodX && y == foodY)
{
score += 10;
foodX = rand() % width;
foodY = rand() % height;
nTail++;
}
}
int main()
{
Setup();
while (!gameover)
{
Draw();
Input();
Logic();
Sleep(50);
}
return 0;
}
```
希望这个C语言实现能够让你更好地理解贪吃蛇游戏的实现过程!
使用scala读取word.txt 数据,将数据放到缓存区。
可以使用以下代码实现:
import scala.io.Source
val filename = "word.txt"
val bufferedSource = Source.fromFile(filename)
val lines = bufferedSource.getLines.toList
bufferedSource.close()
// 将数据放到缓存区
val cache = lines.mkString("\n")
这段代码会读取名为 "word.txt" 的文件,将文件中的内容读取到缓存区中。注意,这里使用了 Scala 的 Source 类来读取文件内容。