【问题标题】:java student gpa files and arraysjava学生gpa文件和数组
【发布时间】:2014-11-30 06:03:51
【问题描述】:

有人可以帮我处理这些代码吗?

这是文件

57363 Joy  Ryder    D D C P H H C D

72992 Laura Norder  H H H D D H H H

71258 Eileen Over   C F C D C C C P

我正在尝试将 ID 存储在 5 位数组中,并将字符转换为数字并计算 H=7D=6C=6P=4F=0 的平均 gpa 并存储这在第二个数组中。然后将这两个数组从最大值到最小值排序,并将这些值写入输出文本文件中。这是我到目前为止的代码

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class StudentGPA_17396934Demo {

public static void main(String[] args) throws IOException {

    Scanner kb = new Scanner(System.in);

    String fileName;
    char grade[] = new char[8];
    File myfile;
    int num = 0;

    do{
    System.out.println("Enter the name of the file: ");
    fileName = kb.next();

    myfile = new File(fileName);
    if (myfile.exists()) {
        Scanner infile = new Scanner(myfile);
        while (infile.hasNext()) {

            int ID = infile.nextInt();
            String name = infile.next();
            String surname = infile.next();

            // it is converting the characters to numbers but not reading all characters
            also how do I get average of this numbers and store in appropriate array 
            also how to store ID in array
            for (int i = 0; i < grade.length; i++) {
                grade[i] = infile.next().charAt(0);

            if (grade[i] == 'H')
            num = 7;
            else if (grade[i] == 'D')
            num = 6;
            else if (grade[i] == 'C')
            num = 5;
            else if (grade[i] == 'P')
            num = 4;
            else if (grade[i] == 'F')
            num = 0;
            }

            System.out.println(ID + "\t" +name + "\t" +surname + "\t" + num);


        }

    }
    else{

        System.out.println("file not found...");
    }
    }while(!myfile.exists());

}

}

我的输出是

Enter the name of the file: 
grades

57363   Joy Ryder   6

72992   Laura   Norder  7

71258   Eileen  Over  4

【问题讨论】:

  • 什么困扰着你?问题是什么?如果这是你的作业,没有人会为你做的:)
  • 我不知道如何将 id 和 Grades 存储到数组中并写入文件
  • 让您了解您的问题。首先如何存储在数组中,其次如何写入文件。所以试着用谷歌搜索它们并学习它们。如果您被困在某个地方,请告诉我们。你必须学会​​如何研究
  • 我已经尝试了一个星期,但到目前为止没有运气..我根本无法理解
  • 快点换个帖子,说你哪部分有问题,我们可以解释并帮助你理解:)

标签: java arrays file


【解决方案1】:

让我们做一个例子作为你的蓝图,这样你就可以跟随和学习

如您所知,您需要一个array

让我们看看arrayJava 中是什么?

数组是一个容器对象,它包含固定数量的值 单一类型

1。它有一个长度,这意味着您可以在其中保存一定数量的数据

2。对象的类型必须相同

让我们看看如何在Java中定义一个array

dataType[] arrayRefVar ;

例如:int[] arryInt;

在 Java 中创建数组

arrayRefVar = new dataType[arraySize];

例如arrayInt = new int[5];

如何为数组的每个索引赋值

例如arrayInt[2] = 23;

如果你想使用 for 循环来填充你的数组,你可以这样做

 for(int i =0; i < arrayInt.length ; i++){
        arrayInt[i] = i ;
        System.out.print(" " + i);
 }

输出:0 1 2 3 4

注意:Java中数组索引从零开始

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    相关资源
    最近更新 更多