bir output.txt dosyasına kullanmayı denedim Uygulamamı bir output.txt dosyasına yazdırarak test etmeye çalışıyorum. Şimdiden dört onur öğrencisi ve en az ikisi de aynı not ortalaması 3.9 olan ve üstelik onur öğrencileri olmayan bir input.txt dosyası var. Sonuçlar çıktı.txt dosyasına gönderilmelidir. Output.txt dosyası şunları içermelidir: 1) Öğrenciler en iyi 2) En iyi öğrenci 3) Listedeki onur öğrenci sayısı 4) Öğrencileri onurlandırmak Oluşturduğum input.txt dosyası aşağıdakileri içerir: sipariş) soyadları, ad adları, kimlik, not ortalaması ve yıl.Bir input.txt dosyasını Java
TestStudents sınıfı, input.txt dosyasını yazdırır. Ancak, yukarıda belirtilen output.txt dosyasını yazdırmak için input.txt dosyasını kullanmak için ona ihtiyacım var. Çok teşekkür ederim.
Öğrenci sınıfı -
public class Student
{
String lastName, firstName, id;
double gpa;
int year;
public Student (String lastName, String firstName, String id,
double gpa, int year)
{
this.lastName = lastName;
this.firstName = firstName;
this.id = id;
this.gpa = gpa;
this.year = year;
}
public String toString()
{
return this.lastName + ", " + this.firstName + ": " + this.id + " "
+ this.gpa + " " + this.year;
}
public double getGPA()
{
return gpa;
}
public boolean isBetter (Student s)
{
return (this.gpa > ((Student)s).getGPA());
}
public boolean isHonors()
{
if (this.gpa >= 3.5)
{
return true;
}
else
{
return false;
}
}
}
CS152 sınıfı -
import java.util.*;
import java.io.*;
public class CS152
{
public static final int MAXSIZE = 22;
private static int size = 0;
public static Student[] createList (Scanner scan) throws IOException
{
Student[] list = new Student [MAXSIZE];
return populateList (list, scan);
}
private static Student[] populateList (Student[] list, Scanner scan)
{
Student s;
if (size < MAXSIZE && scan.hasNext())
{
s = new Student (scan.next(), scan.next(), scan.next(),
scan.nextDouble(), scan.nextInt());
list[size] = s;
size++;
System.out.println (s);
return populateList (list, scan);
}
else
{
return list;
}
}
public static int getSize()
{
return size;
}
// Returns String of all students. Variable n is actual size of the list.
// Assume that n is positive. Recursive code.
public static String toString (Student[] list, int n)
{
String s = " ";
if (n == 1)
{
return s += list[0];
}
else
{
s += list[n].toString() + "\n";
s += "\n";
}
return s + toString (list, n - 1);
}
// Returns the best student. Must use method isBetter in the code.
// Variable n is actual size of the list. Assume that n is positive.
public static Student findBestStudent (Student[] list, int n)
{
if (n == 1)
{
return list[0];
}
else if (list[n].isBetter (list[n - 1]))
{
return list[n];
}
else
{
return findBestStudent (list, n - 1);
}
}
// Returns the number of honor students in the list.
// Must call the method isHonors(). Variable n is actual size of the list.
// Assume that n is positive.
public static int countHonors (Student[] list, int n)
{
if (n == 0)
{
return 0;
}
else if (list[n].isHonors())
{
return 1 + countHonors (list, n - 1);
}
else
{
return countHonors (list, n - 1);
}
}
static ArrayList<Student> studentsList = new ArrayList<Student>();
public static ArrayList <Student> honorsStuds (Student[] list, int n)
{
if (n == 0)
{
return studentsList;
}
else
{
boolean currentIsHonors = list[n - 1].isHonors();
if (currentIsHonors)
{
studentsList.add(list[n - 1]);
return honorsStuds (list, n - 1);
}
else
{
return honorsStuds (list, n - 1);
}
}
}
}
TestStudents sınıfı -
import java.io.*;
import java.util.*;
public class TestStudents
{
public static void main (String[] args) throws IOException
{
File input = new File ("input.txt");
Scanner scan = new Scanner (input);
Student[] studentArray = CS152.createList (scan);
}
}
I TestStudents sınıfına FileWriter dahil. Tüm öğrencilerin bir listesi şimdi görüntülenir. FindBestStudent, countHonors ve honorsStuds yöntemlerini çağırmaya ve bunları TestStudents uygulamasına çağırmaya çalışırken hala zorlanıyorum. İşte revize TestStudents sınıftır:
TestStudents sınıfı -
import java.io.*;
import java.util.*;
public class TestStudents
{
public static void main (String[] args) throws IOException
{
File input = new File ("input.txt");
Scanner scan = new Scanner (input);
System.out.println ("All students: ");
Student[] studentArray = CS152.createList (scan);
File output = new File ("output.txt");
FileWriter fWriter = new FileWriter (output);
PrintWriter pWriter = new PrintWriter (fWriter);
pWriter.println (input);
pWriter.close();
}
}
'statik Öğrenci [] populateList (Öğrenci [] listesi, Tarayıcı taraması) 'tekrarlı olarak adlandırılıyor - her öğrenci için bir seviye - kesinlikle hiçbir sebep yok. Eğer istemiyorsanız (/ * yapılmadı * /) {/ * ... */populateList()} ', sadece bir süre (/ * bitmedi * /) {...}' loop . – AJNeufeld
En iyi öğrenciyi bulmak için kodu zaten yazdınız.Sadece işlevleri çağırmanız ve sonucu yazdırmanız gerekir. – AJNeufeld
Bu benim bir sorunum var. Tüm öğrencileri bastım. Şimdi findBestStudent, countHonors ve honorsStuds yöntemlerini yazdırmak zorundayım. Ne yazık ki, bunları çıktı.txt dosyasında yazdıramam. – Voraptustenellus