我正在学习Java,我正在使用Java类Cycles,它模仿伦敦鲍里斯自行车。创建一个计算使用自行车站的时间的接口。我想要的输出是这样的;
- 请输入第1周期的停靠站号码 - 0
- 请输入第2周期的停靠站号码 - 1
- 请输入第3周期的停靠站号码 - 2
系统中当前循环次数@ 355da254
我想要实现的目标是
- 请输入第1周期的停靠编号 0
- 请输入第2周期的停靠站号码 1
- 请输入第3周期的停靠站号码 2
系统3中当前的循环次数
这是源代码Cycle Class;
/**
* Created by devops05 on 28/02/16.
*/
public class Cycle {
private int ID;
private int dockingStationID;
private static int lastAssignedNumber = 100;
//private int count;
public Cycle()
{
lastAssignedNumber ++;
ID = lastAssignedNumber;
dockingStationID = 0;
}
public int getID()
{
return ID;
}
public boolean pickup()
{
if (dockingStationID == 0)
{
return false;
}
else
{
return true;
}
}
public boolean park(int dockSID)
{
if (dockingStationID == 0)
{
dockingStationID = dockSID;
return true;
}
else
{
return false;
}
}
public String getDockingStationNo()
{
if(dockingStationID == 0)
{
return " is in use ";
}
else
{
return " is at docking station "+ dockingStationID;
}
}
public int getNumberOfCycles()
{
int cycle1 = 1;
int cycle2 = 1;
int cycle3 = 1;
int cycleTotal = cycle1 + cycle2 + cycle3;
//for(int count = 0; count<3; count++)
return cycleTotal;
}
}
第二个Java surce Class CycleTest;
/**
* Created by devops05 on 28/02/16.
*/
import java.util.Scanner;
public class CycleTest {
public static void main(String[] args)
{
Cycle cycle1 = new Cycle();
//int limit = 3;
//int count = 0;
Scanner in = new Scanner(System.in);
System.out.println("Please enter docking No. for Cycle 1");
int dockSID = in.nextInt();
cycle1.park(dockSID);
//for(int i=0; i < dockSID; i++)
{
//count++;
if (cycle1.park(dockSID))
{
System.out.println("Cycle with ID: " + cycle1.getID() + " is in use");
}
else
{
System.out.println("Cycle with ID: " + cycle1.getID() + cycle1.getDockingStationNo());
}
Cycle cycle2 = new Cycle();
System.out.println("Please enter docking No. for Cycle 2");
dockSID = in.nextInt();
cycle2.park(dockSID);
if (cycle2.park(dockSID))
{
System.out.println("Cycle with ID: " + cycle2.getID() + " is in use");
}
else
{
System.out.println("Cycle with ID: " + cycle2.getID() + cycle2.getDockingStationNo());
}
Cycle cycle3 = new Cycle();
System.out.println("Please enter docking No. for Cycle 3");
dockSID = in.nextInt();
cycle3.park(dockSID);
if (cycle3.park(dockSID))
{
System.out.println("Cycle with ID: " + cycle3.getID() + " is in use");
}
else
{
System.out.println("Cycle with ID: " + cycle3.getID() + cycle3.getDockingStationNo());
}
//Cycle cycleTotal = new Cycle();
//System.out.print("Number of cycles currently in the systems " + cycleTotal);
Cycle numberOfCycles = new Cycle();
System.out.print("Number of cycles currently in the systems " + numberOfCycles);
}
System.out.println();
//System.out.println("Number of cycles currently in the system is " + cycle.getNumberOfCycles());
//System.out.println("Number of cycles currently in the system is " + count);
}
}
答案 0 :(得分:1)
为了清楚起见,您的问题似乎是如何打印总周期数。它似乎是硬编码为3,所以你可以打印出“3”!
但是我假设你想要对你的设计有更深入的评论。关键问题是您为每个循环使用单独的变量,而不是将它们存储在集合中。理想情况下,您的代码应该更像:
class System {
private final List<Cycle> cycles = new ArrayList<>();
public void addCycle(Cycle cycle) {
cycles.add(cycle);
}
public int getCycleCount() {
return cycles.size();
}
}
然后你的主要方法可以更清楚:
System system = new System();
for (int i = 0; i < 3; i++) {
Cycle cycle = new Cycle(i);
system.add(cycle);
System.out.println("Please enter docking number for cycle " + i);
int dock = in.nextInt();
if (dock > 0)
cycle.park(dock);
}
System.out.println("The number of cycles in the system is" + system.getCycleCount());
你可以在Cycle
类中将循环列表或循环数作为静态变量,但许多设计师会将其视为糟糕的设计。避免这些类型的全局状态有几个原因,但最明显的是您可能希望创建不在系统中的循环。
答案 1 :(得分:0)
创建一个辅助计数器,用于存储到目前为止创建的周期数:
public class Cycle {
...
public static int numberOfCycles;
...
}
public class CycleTest {
public static void main(String[] args) {
Cycle cycle1 = new Cycle();
Cycle.numberOfCycles++;
...
Cycle cycle2 = new Cycle();
Cycle.numberOfCycles++;
...
Cycle cycle3 = new Cycle();
Cycle.numberOfCycles++;
...
System.out.print("Number of cycles currently in the systems " + Cycle.numberOfCycles);
}
}