2014-02-22 1 views
1

저는 몇 시간 동안이 일을 해왔으며 다소 좌절했습니다. 누군가 내 코드를 살펴보고 StudentRecWithinput.Update()에서 Student1을 사용할 수없는 이유를 말해 주면 도움의 손길을 좋아할 것입니다.올바른 데이터를 호출 할 수 없음

거기에없이 작동

하지만 단지 내가 읽기하고 데이터의 마지막 줄을 사용하는 것 같다.

주요

package Database; 

import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class Main { 

public static void main(String[] args) throws FileNotFoundException { 
    Scanner Keyboard = new Scanner(System.in); 
    String choice1 = ""; 

    System.out 
      .println("Would you like to update a students GPA? Please Input Yes or NO."); 
    choice1 = Keyboard.next(); 

    if (choice1.charAt(0) == 'y' || choice1.charAt(0) == 'Y') { 
     recupdate(); 
    } else { 
     System.out.println("Alright, Goodbye!"); 
    } 
} 

public static void recupdate() throws FileNotFoundException { 
    Scanner Keyboard = new Scanner(System.in); 
    int choice2; 
    System.out.println("Here are the records!\n"); 

    StudentRec Student1 = new StudentRec(); 
    StudentRec Student2 = new StudentRec(); 
    StudentRec Student3 = new StudentRec(); 

    System.out 
      .println("Who's gpa will you be edditing? Please input (1, 2 or 3)"); 

    choice2 = Keyboard.nextInt(); 
    if (choice2 == 1) { 

     StudentRecWithInput.update(Student1); 

    } 

    if (choice2 == 2) { 
     StudentRecWithInput.update(Student2); 
    } 

    if (choice2 == 3) { 
     StudentRecWithInput.update(Student3); 

    } 
} 
} 

STUDENTREC

package Database; 

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

public class StudentRec { 
// student related// 

private String lastname; 
private String firstname; 
private String major; 
private int age; 
private static double gpa; 
private static int credit; 

// /non student related//// 
File info = new File("studentinfo.txt"); 
Scanner scan = new Scanner(info); 
static int count = 0; 

public StudentRec() throws FileNotFoundException { 

    { 
     count++; 

     if (count == 2) { 
      scan.nextLine(); 

     } 

     else if (count == 3) { 
      scan.nextLine(); 
      scan.nextLine(); 
     } 

     firstname = scan.next(); 
     lastname = scan.next(); 
     age = scan.nextInt(); 
     setGpa(scan.nextDouble()); 
     major = scan.next(); 
     setCredit(scan.nextInt()); 
     System.out.println(firstname + " " + lastname + " " + age + " " 
       + getGpa() + " " + major + " " + getCredit() + ""); 

    } 

} 

public static int getCredit() { 
    return credit; 
} 

public void setCredit(int credit) { 
    this.credit = credit; 
} 

public static double getGpa() { 
    return gpa; 
} 

public void setGpa(double gpa) { 
    this.gpa = gpa; 
} 

} 

STUDENTRECWITHINPUT

package Database; 

import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class StudentRecWithInput extends StudentRec { 
static Scanner keyboard = new Scanner (System.in); 


public StudentRecWithInput() throws FileNotFoundException { 
    super(); 



} 

public static double update(int credit, double gpa) 
{ 
    double pastpoints = getCredit() * getGpa(); 
    int newcredhours = keyboard.nextInt(); 
    double newpoint = keyboard.nextDouble(); 
    double semestergpa = newpoint/newcredhours; 
    double cumulate = (pastpoints + newpoint)/(newcredhours+ getCredit()); 

    System.out.print(pastpoints); 

    return cumulate; 

} 

} 

studentinfo.txt

Bob Bobbers 19 3.5 CPS 55 
John Johners 20 3.7 BIO 70 
Kat Katters 21 3.8 ITC 100 

답변

0

update(int credit, double gpa) 

가 INT 더블 기대,하지만 당신은 당신이 서명을 변경해야

StudentRecWithInput.update(Student1); 

로를 호출 StudentRecWithInput

의 서명 :

학생 정보를 방법에 전달할 수 있습니다.

정적 코드와 비 정적 코드의 차이점에 대해서도 알아야합니다. 코드에 컴파일 변경 사항이 없습니다.

+0

감사합니다. 컴파일을했는데 실제로 문제가되는 것은 학생 1을 선택하더라도 학생 3의 정보를 사용하는 것입니다. 왜 그런가? – Charles211

+0

이것은 주로 개인 정적 double gpa를 사용하기 때문에 발생합니다. 개인 정적 int 신용; static은 각 인스턴스가 아닌 클래스에 대한 단일 필드가 있음을 의미합니다. 정적 키워드 – zibi

+0

을 제거 해보려고 시도했는데 Main이 StudentRecWithInput.update()를 호출 할 때 static으로 다시 변경하도록 요청했습니다. – Charles211

0

아마도 전략을 조정하고 싶을 것입니다. 다음 단계를 사용하는 것이 좋습니다.

  1. 입력 파일을 StudentRecord의 Vector로 읽습니다.
  2. 그런 다음
  3. 그런 다음 그 레코드에 대한 업데이트 데이터를 요청 업데이트해야하는지 기록 요청 (코드는 준비가 사용자의 입력 파일로 할 때마다 당신이 StudentRec을 만드는 시도)

Vector<StudentRec>

가있을 수 있습니다 그래서 주

StudentRec 클래스는 StudentRec을 변경해야 할 때 호출하는 update 메서드 (인수 없음)를 사용합니다.

질문자가 연습 문제로 남긴 정확한 세부 사항은 학교 과제 인 것처럼 보입니다.