Hi this is a beginner Java question about classes, it is for an assignment so I would just like a hint not the answer. I looked around for this answer in other forums but I didn't seem to find the answer, but I apologize if this has already been answered.
I have a class that has three inTime doubles defined in one class, and I need to write the doubles to the screen in another class. The doubles are taken in as input from the keyboard.
My problem is, I have each inTime separated into three different double methods that return them as a double. When I set the new doubles in the writing class, to end the input I have to press enter, but I NEED the inTime inputs to be separated and ended by spaces. The only way I know how to set the variables is like this:
public class Race{
private double timeOne = 0;
private double timeTwo = 0;
private double timeThree = 0;
public double timeOne(){
Scanner keyboard = new Scanner(System.in);
timeOne = keyboard.nextDouble();
return timeOne;
}
public double timeTwo(){
Scanner keyboard = new Scanner(System.in);
timeTwo = keyboard.nextDouble();
return timeTwo;
}
public double timeThree(){
Scanner keyboard = new Scanner(System.in);
timeThree = keyboard.nextDouble();
return timeThree;
}
This is the writing class:
public class RaceReport {
public void writeReport(){
Race race = new Race();
System.out.println("Enter the race times (in seconds): ");
double timeOne = race.timeOne();
double timeTwo = race.timeTwo();
double timeThree = race.timeThree();
Should this be broken into three methods like I have, or only one method? I have no idea how I could yield three variables from one void method, separate return methods were the only thing that made sense.
Here is what the screen should look like when the doubles are being entered:
Enter the race times (in seconds): 85.6 120.5 90.2
The spaces separated should be separating the entries
Aucun commentaire:
Enregistrer un commentaire