Java代码提供负输出

时间:2018-04-08 19:52:38

标签: java class methods output

我对此代码的输出有疑问:

import java.util.Scanner;

public class Parking
{
public static void main(String[]args)
{
    Scanner scan= new Scanner(System.in);

    System.out.println("Enter the name of the parking lot:");
    String Name=scan.nextLine();
    System.out.println("Enter the address of the parking lot:");
    String Address=scan.nextLine();
    System.out.println("Enter the number of spots available for this parking lot:");
    int Num_Spots=scan.nextInt();
    System.out.println();


    Parking myParking= new Parking(Name,Address,Num_Spots);

    System.out.println("This is the information you provided about this parking lot:\n"+ myParking);
    System.out.println();

    String toQuit;
    boolean quit=false;
    while(!quit)
    { 
    System.out.println("How many cars are exiting?");
    int CarsExiting=scan.nextInt();


    System.out.println("How many cars are entering?");
    int CarsEntering=scan.nextInt();
    while(CarsEntering>(Num_Spots+CarsExiting))
    {
        System.out.println("I'm sorry, the number of cars entering must be less than number of spots available.");
        System.out.println("Please inform the remaining customers that this lot is full.");
        System.out.println("How many cars are entering now?");
        CarsEntering=scan.nextInt();
    }
    System.out.println("The number of spots available now is: "+CarFlow(CarsEntering,CarsExiting));

        System.out.println("Press 'Q' to quit and any key to continue");
        toQuit=scan.next();
        if(toQuit.equalsIgnoreCase("Q"))
        {
            quit=true;
        }


    }


}//end main

private String _Name;
private String _Address;
private static int _Num_Spots;

// constructor for the parking class
public Parking(String Name,String Address, int Num_Spots) 
{
   _Name = Name;
   _Address = Address;
   _Num_Spots = Num_Spots;
}
//getters
public String getName() {return _Name;}
public String getAddress() {return _Address;}
public int getNumSpots() {return _Num_Spots;}

//setters
public void setName(String Name) {_Name=Name;}
public void setAddress(String Address) {_Address=Address;}
public void setNumSpots(int Num_Spots) {_Num_Spots=Num_Spots;}


//CarsFlow method()
public static int CarFlow(int CarEn,int CarEx)
{
    _Num_Spots=_Num_Spots+CarEx;
    _Num_Spots= _Num_Spots-CarEn;
    System.out.println("The number of cars exiting is "+CarEx);
    System.out.println("The number of cars entering is "+CarEn);

    return _Num_Spots;
}

public String toString()
{
    String Output="The name of the parking lot is: "+_Name+
                  "\nThe address of the parking lot is: "+_Address+    
                  "\nThe number of spots available in this parking lot   is: "+_Num_Spots;
    return Output;
}

}//end Parking class

输出工作正常,然后在某些时候,当我输入的车数超过可用的点数时,我得到这个输出:

退出的汽车数量为0 进入的汽车数量是10 现在可用的点数是:0 按'Q'退出并按任意键继续 [R 有多少辆车正在退出? 0 有多少车进入? 3 退出的汽车数量为0 进入的车数是3 现在可用的点数是:-3

当程序应该通过while循环并打印一条消息,说明进入的车辆数量大于可用的点数因为3大于0时,我得到一个负数。

3 个答案:

答案 0 :(得分:0)

因为您同时使用Num_Spots_Num_Spots变量。您永远不会将Num_Spots的值分配给_Num_Spots,因此您需要比较错误的整数。

除此之外,您可能想学习Java编码标准。变量和方法(函数)名称永远不会以大写字母开头,它们中永远不会有下划线。您可以在代码中看到突出显示因为您使用自己的编码标准而感到困惑。

答案 1 :(得分:0)

我在本声明中看到了问题

while (CarsEntering > (Num_Spots + CarsExiting))

每次迭代后,您不会更新Num_Spots变量。您可以修改如下

System.out.println("The number of cars exiting is " + CarsExiting); //Take out this statements from CarFlow method
System.out.println("The number of cars entering is " + CarsEntering);//Take out this statements from CarFlow method
Num_Spots = CarFlow(CarsEntering, CarsExiting); //add this line above this statement System.out.println("The number of spots available now is: 
System.out.println("The number of spots available now is: " + Num_Spots);

<强>输出:

output

答案 2 :(得分:0)

您希望将进行计算的类与控制用户输入/输出的主应用程序分开。

然后,您可以轻松地开始使用单元测试来驱动应用程序的行为。这将使您轻松跟踪错误并更改实施。

很容易看出carFlow()可以返回负面结果。这可能是第一次测试。我还认为你的while语句中的检查是错误的,为什么要将现有的卡添加到Num_spots?

这可能是对carFlow()的第一次测试,我认为你想在你的应用程序中为这个方法增加更多的责任。此测试失败,因为-1不是0!

@Test
public void testNumberOfCardEnteringBiggerThanNumberOfSpots() {
    Parking underTest = new Parking("Unit Test", "Some street", 0);
    int actual = underTest.CarFlow(1, 0);

    Assert.assertEquals(0, actual);
}

快乐的编码!