2014-02-22 53 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

StudentRecWithInput

update(int credit, double gpa) 

期待int和double,但你與

StudentRecWithInput.update(Student1); 

需要簽名改爲調用它的簽名:

能夠將學生信息傳遞到方法中。

你也應該閱讀更多關於靜態和非靜態方法的區別,因爲現在你的代碼沒有編譯的變化。

+0

謝謝!它做了編譯,但我現在實際上遇到的問題是它使用學生3的信息,即使我選擇了學生1.任何想法爲什麼? – Charles211

+0

這很可能是因爲你使用了 private static double gpa; private static int credit; static表示該類有一個單獨的字段,而不是每個實例。嘗試刪除靜態關鍵字 – zibi

+0

我曾試過,主要是要求它改回靜態時調用StudentRecWithInput.update(); – Charles211

0

你可能想要調整你的策略。考慮使用以下步驟:

  1. 將您的輸入文件讀入一個StudentRecord向量中。 (你的代碼試圖準備輸入文件每次創建StudentRec)
  2. 然後問什麼記錄應更新
  3. 然後索要更新數據,該記錄

所以你的主要可能有Vector<StudentRec>

雖然您的StudentRec類將有一個update方法(無參數),當您需要更改StudentRec時調用。

確切的細節留給提問者練習,因爲這似乎是一個學校的任務。