我试图避免使用两个文件,而只是将它们组合成一个文件。
所以我在.java文件中创建了一个类,它的外观如下:
import javax.swing.JOptionPane;
public class Raindrop {
double [] rainfallStats;
public double getTotalRainfall() {
double totalRainfall;
for ( int index = 0; index < rainfallStats.length; index++) {
totalRainfall = totalRainfall + rainfallStats[ index ];
}
return totalRainfall;
}
public double getAverageRainfall() {
return getTotalRainfall() / rainfallStats.length;
}
public double getMostRainMonth () {
double mostRain = rainfallStats [ 0 ];
int mostRainMonth;
for( int index = 0; index < rainfallStats.length; index++ ) {
if ( rainfallStats[ index ] > mostRain ) {
mostRain = rainfallStats [ index ];
mostRainMonth = index + 1;
}
}
return mostRainMonth;
}
public double getLeastRainMonth () {
double leastRain = rainfallStats [ 0 ];
int leastRainMonth;
for( int index = 0; index < rainfallStats.length; index++ ) {
if ( rainfallStats[ index ] > leastRain ) {
leastRain = rainfallStats [ index ];
leastRainMonth = index + 1;
}
}
return leastRainMonth;
}
public Rainfall ( double [] rainfallStatsGiven) {
rainfallStats = new double [ rainfallStatsGiven.length ];
for (int index = 0; index < rainfallStatsGiven; index++) {
rainfallStats[ index ] = rainfallStatsGiven [ index ];
}
}
public static void fillArrayWithUserInput ( double [] arrayGiven ) {
String userInputString;
double userMonthlyRainfallFigure;
for ( int index = 0; index < arrayGiven.length; index++ ){
userInputString = JOptionPane.showInputDialog(
"Please enter rainfall figures for month " + (index + 1) );
userMonthlyRainfallFigure = Double.parseDouble( userInputString );
arrayGiven [ index ] = userMonthlyRainfallFigure;
}
}
public static void main(String[] args) {
final int MONTH_IN_A_YEAR = 12;
double [] rainfallStats = new double [12];
String userOutputString;
fillArrayWithUserInput( rainfallStats );
Rainfall rainfallData = new Rainfall( rainfallStats );
userOutputString = String.format (
"Total rainfall: %f\nAverage monthly rainfall" + ":"
+ "%f\nMonth with most rain: %f\nMonth with least rain: %f",
rainfallData.getTotalRainfall(),
rainfallData.getAverageRainfall(),
rainfallData.getMostRainMonth(),
rainfallData.getLeastRainMonth() );
JOptionPane.showMessageDialog(null, userOutputString );
System.exit( 0 );
}
}
我收到错误:
Raindrop.java:45: error: invalid method declaration; return type required
public Rainfall ( double [] rainfallStatsGiven) {
^
1 error
我尝试将public Rainfall
更改为一个方法,但它无法弄清楚如何做到这一点,想要了解如何使这项工作。我无法弄清楚如何嵌套类ethier,我无法弄清楚如何使公共类成为可行的其他任何东西。
答案 0 :(得分:0)
我能够再次查看我的代码并发现许多错误并修复了它们,在此过程中,由于存在更多问题而修复了我的问题,因此我能够得到我想要的内容。 如果有人也想看看,那么修改后的那个:
import javax.swing.JOptionPane;
public class Rainfall {
double [] rainfallStats;
public double getTotalRainfall() {
double totalRainfall =0;
for ( int index = 0; index < rainfallStats.length; index++) {
totalRainfall = totalRainfall + rainfallStats[ index ];
}
return totalRainfall;
}
public double getAverageRainfall() {
return getTotalRainfall() / rainfallStats.length;
}
public double getMostRainMonth () {
double mostRain = rainfallStats [ 0 ];
int mostRainMonth = 1;
for( int index = 0; index < rainfallStats.length; index++ ) {
if ( rainfallStats[ index ] > mostRain ) {
mostRain = rainfallStats [ index ];
mostRainMonth = index + 1;
}
}
return mostRainMonth;
}
public double getLeastRainMonth () {
double leastRain = rainfallStats [ 0 ];
int leastRainMonth =1;
for( int index = 0; index < rainfallStats.length; index++ ) {
if ( rainfallStats[ index ] < leastRain ) {
leastRain = rainfallStats [ index ];
leastRainMonth = index + 1;
}
}
return leastRainMonth;
}
public Rainfall ( double [] rainfallStatsGiven) {
rainfallStats = new double [ rainfallStatsGiven.length ];
for (int index = 0; index < rainfallStatsGiven.length; index++) {
rainfallStats[ index ] = rainfallStatsGiven [ index ];
}
}
public static void fillArrayWithUserInput ( double [] arrayGiven ) {
String userInputString;
double userMonthlyRainfallFigure;
for ( int index = 0; index < arrayGiven.length; index++ ){
userInputString = JOptionPane.showInputDialog("Please enter rainfall figures for month " + (index + 1) );
userMonthlyRainfallFigure = Double.parseDouble( userInputString );
arrayGiven [ index ] = userMonthlyRainfallFigure;
}
}
public static void main(String[] args) {
final int MONTH_IN_A_YEAR = 12;
double [] rainfallStats = new double [12];
String userOutputString;
fillArrayWithUserInput( rainfallStats );
Rainfall rainfallData = new Rainfall( rainfallStats );
userOutputString = String.format ( "Total rainfall: %f\nAverage monthly rainfall"+
":+ %f\nMonth with most rain: %f\nMonth with least rain: %f",
rainfallData.getTotalRainfall(),
rainfallData.getAverageRainfall(),
rainfallData.getMostRainMonth(),
rainfallData.getLeastRainMonth() );
JOptionPane.showMessageDialog(null, userOutputString );
System.exit( 0 );
}
}