无法将程序打印到命令行或报告文件



我这里有一个程序可以编译,但我无法将其打印到命令行或报告文件中。如有任何协助,我们将不胜感激。这是我得到的错误:

错误:在类乡中找不到主方法,请将主方法定义为:public static void main(String[]args(或者JavaFX应用程序类必须扩展JavaFX.application.application按任意键继续。

import java.util.*;
import java.io.*;

//Township class
class Township
{
//instance variables
String name;
int households;
int bicycles;
double average;
String tier;
double funding;
//constructor
public Township(String name, int households, int bicycles)
{
this.name = name;
this.households = households;
this.bicycles = bicycles;
}
//calculate average bikes per household
public void AverageBikes()
{
average = (double)bicycles/households;
}
//calculate the funding
public void Funding()
{
if(average>=3.0) funding = 50000.00;
else if(average>=2.0) funding = 40000.00;
else if(average>=1.0) funding = 30000.00;
else if(average>=0.5) funding = 20000.00;
else funding = 0.00;
}
//calculate the tier
public void Tier()
{
if(average>=3.0) tier = "One";
else if(average>=2.0) tier = "Two";
else if(average>=1.0) tier = "Three";
else if(average>=0.5) tier = "Four";
else tier = "Five";
}
//sort the townships in alphabetical order by township name
public static void sort(Township t[], int n)
{
for(int i=0; i<n-1; i++){
for(int j=i+1; j<n; j++){
if(t[i].name.compareTo(t[j].name) > 0){
Township tmp = t[i];
t[i] = t[j];
t[j] = tmp;
}
}
}
}
}
//Driver class
class Driver
{
//method to print the report
public static void printReport(Township t[], int n)
{
//sort the list
Township.sort(t, n);
System.out.println ("Township           Number       Bicycles   Average Bikes   Proposed       Tier");
System.out.println ("Name               Households   reported   Per household   Funding           Designation");
//print the report
for(int i=0; i<n; i++)
{
System.out.printf ("%-20s%-12d%-12d%-16.2f%-16.2f%sn", t[i].name, t[i].households,
t[i].bicycles, t[i].average, t[i].funding, t[i].tier);
}
}
//method to get input from user
public static Township getInput(Scanner sc)
{
System.out.print ("Enter the name of Township: ");
String name = sc.nextLine();
System.out.print ("Enter the number households: ");
int household = sc.nextInt();
System.out.print ("Enter the bicycles reported: ");
int bicycles = sc.nextInt();
Township town = new Township(name, household, bicycles);
town.AverageBikes();
town.Funding();
town.Tier();
return town;
}
//method to get input from file
public static int readFile(Township town[]) throws IOException
{
//open the file
Scanner sc = new Scanner(new File("town.txt"));
int i=0;
while(sc.hasNext())
{
String name = sc.nextLine();
int household = sc.nextInt();
sc.nextLine();
int bicycles = sc.nextInt();
sc.nextLine();
town[i] = new Township(name, household, bicycles);
town[i].AverageBikes();
town[i].Funding();
town[i].Tier();
i++;     
}
return i;
}
//method to write the report to the file
public static void writeFile(Township t[], int n) throws IOException
{
//sort the list
Township.sort(t, n);
FileWriter writer = new FileWriter("report.txt");
PrintWriter pout = new PrintWriter(writer);

pout.printf ("Township           Number       Bicycles   Average Bikes   Proposed       Tiern");
pout.printf ("Name               Households   reported   Per household   Funding           Designationn");
for(int i=0; i<n; i++)
{
pout.printf ("%-20s%-12d%-12d%-16.2f%-16.2f%sn", t[i].name, t[i].households,
t[i].bicycles, t[i].average, t[i].funding, t[i].tier);
}
pout.close();
}
//main method
public static void main (String[] args) throws IOException
{
//create an instance of Scanner class
Scanner sc = new Scanner(System.in);
//create an array of Townships
Township town[] = new Township[10];
int i= readFile(town);
//processing
while(true)
{
//menu
System.out.println ("1. Inputn2. Reportn3. Exit");
//prompt to enter a choice
System.out.print("Enter choice: ");
int n = sc.nextInt();
switch(n)
{
case 1:
//get input from user
town[i] = getInput(sc);
i++;
break;
case 2:
//print the report
printReport(town, i);
break;
case 3:
//write to file
writeFile(town, i);
System.out.println ("Goodbye!");
return;
}
}      
}
}

编辑

感谢回复,我更新了错误和一些代码。

这是我得到的新错误:

错误:位于的线程"main"java.util.InputMismatchException中出现异常java.base/java.util.Scanner.sthrowFor(Scanner.java:939(位于java.base/java.util.Scanner.next(Scanner.java:1594(java.base/java.util.Scanner.nextInt(Scanner.java:2258(java.base/java.util.Scanner.nextInt(Scanner.java:2212(位于Town.readFile(Town.java:143(位于Town.main(Town.java:20(

代码:

import java.util.*;
import java.io.*;
//Township class
public class Township{
//instance variables
String name;
int households;
int bicycles;
double average;
String tier;
double funding;
public static void main (String[] args) throws IOException{
//create an instance of Scanner class
Scanner sc = new Scanner(System.in);
//create an array of Townships
Township town[] = new Township[10];
int i= readFile(town);
//processing
while(true)
{
//menu
System.out.println ("1. Inputn2. Reportn3. Exit");
//prompt to enter a choice
System.out.print("Enter choice: ");
int n = sc.nextInt();
switch(n)
{
case 1:
//get input from user
town[i] = getInput(sc);
i++;
break;
case 2:
//print the report
printReport(town, i);
break;
case 3:
//write to file
writeFile(town, i);
System.out.println ("Goodbye!");
return;
}
}
}
//==================================================================
//constructor
public Township(String name, int households, int bicycles)
{
this.name = name;
this.households = households;
this.bicycles = bicycles;
}
//calculate average bikes per household
public void AverageBikes()
{
average = (double)bicycles/households;
}
//calculate the funding
public void Funding()
{
if(average>=3.0) funding = 50000.00;
else if(average>=2.0) funding = 40000.00;
else if(average>=1.0) funding = 30000.00;
else if(average>=0.5) funding = 20000.00;
else funding = 0.00;
}
//calculate the tier
//==================================================================
public void Tier()
{
if(average>=3.0) tier = "One";
else if(average>=2.0) tier = "Two";
else if(average>=1.0) tier = "Three";
else if(average>=0.5) tier = "Four";
else tier = "Five";
}
//sort the townships in alphabetical order by township name
public static void sort(Township t[], int n)
{
for(int i=0; i<n-1; i++){
for(int j=i+1; j<n; j++){
if(t[i].name.compareTo(t[j].name) > 0){
Township tmp = t[i];
t[i] = t[j];
t[j] = tmp;
}
}
}
}
//==================================================================
//method to print the report
public static void printReport(Township t[], int n)
{
//sort the list
Township.sort(t, n);
System.out.println ("Township           Number       Bicycles   Average Bikes   Proposed       Tier");
System.out.println ("Name               Households   reported   Per household   Funding           Designation");
//print the report
for(int i=0; i<n; i++)
{
System.out.printf ("%-20s%-12d%-12d%-16.2f%-16.2f%sn", t[i].name, t[i].households,
t[i].bicycles, t[i].average, t[i].funding, t[i].tier);
}
}
//==================================================================
//method to get input from user
public static Township getInput(Scanner sc)
{
System.out.print ("Enter the name of Township: ");
String name = sc.nextLine();
System.out.print ("Enter the number households: ");
int household = sc.nextInt();
System.out.print ("Enter the bicycles reported: ");
int bicycles = sc.nextInt();
Township town = new Township(name, household, bicycles);
town.AverageBikes();
town.Funding();
town.Tier();
return town;
}
//==================================================================
//method to get input from file
public static int readFile(Township town[]) throws IOException
{
//open the file
Scanner sc = new Scanner(new File("bicycledata.txt"));
int i=0;
while(sc.hasNext())
{
String name = sc.nextLine();
int households = sc.nextInt();
sc.nextLine();
int bicycles = sc.nextInt();
sc.nextLine();
town[i] = new Township(name, households, bicycles);
town[i].AverageBikes();
town[i].Funding();
town[i].Tier();
i++;
}
return i;
}
//==================================================================
//method to write the report to the file
public static void writeFile(Township t[], int n) throws IOException{
//sort the list
Township.sort(t, n);
FileWriter writer = new FileWriter("report.txt");
PrintWriter pout = new PrintWriter(writer);

pout.printf ("Township           Number       Bicycles   Average Bikes   Proposed       Tiern");
pout.printf ("Name               Households   reported   Per household   Funding           Designationn");
for(int i=0; i<n; i++)
{
pout.printf ("%-20s%-12d%-12d%-16.2f%-16.2f%sn", t[i].name, t[i].households,
t[i].bicycles, t[i].average, t[i].funding, t[i].tier);
}
pout.close();
}//end method
}//end class

试试这个:

class Township {
//instance variables
String name;
int households;
int bicycles;
double average;
String tier;
double funding;
//constructor
public Township(String name, int households, int bicycles) {
this.name = name;
this.households = households;
this.bicycles = bicycles;
}
// Rest of Township class code...
public static void main(String[] args) {
// Do things...
}
}

看看这个:Essentials

每个应用程序都需要一个带有主方法的类。这个班是程序的入口点,是传递给java解释器命令来运行应用程序。


IOException由于另一个原因被抛出给您。你的问题已经回答了。

我认为您正在编译没有main((方法的类Town。您应该编译Driver类,因为类Driver有main((方法,并创建类Town的对象。

最新更新