【问题标题】:Java code gives negative outputJava 代码给出负输出
【发布时间】:2018-04-08 19:52:37
【问题描述】:

我对这段代码的输出有疑问:

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。

【问题讨论】:

  • 你的问题是?
  • 然后用 inut 写一个@Test,然后对你的代码进行错误处理。然后,您将 1. 找到错误 2. 修复它并 3. 进行测试以确保它不会再次出现!

标签: java class methods output


【解决方案1】:

因为您同时使用了 Num_Spots_Num_Spots 变量。您从未将 Num_Spots 的值分配给 _Num_Spots,因此您在比较错误的整数。

除此之外,您可能还想学习 Java 编码标准。变量和方法(函数)名称从不以大写字母开头,也从不包含下划线。您可以在您的代码中看到,由于您使用的是自己的编码标准,因此突出显示会变得混乱。

【讨论】:

  • 我正在努力学习有关 Java 编码标准的更多信息。您能详细解释一下如何使用 Num_Spots 修改 _Num_Spots 变量吗?谢谢。
  • 您的代码存在很多问题。这是可以理解的,因为这对你来说都是新的。但是您应该做的是决定是在静态main 方法中还是在Parking 类的实例中执行所有操作。现在你两者都做,这会导致问题。您使用Num_Spots 变量实例化Parking 实例,然后在main 方法中继续与该变量进行比较,而每次调用CarFlow_Num_Spots 变量都会获得不同的值。最简单的解决方法是与 _Num_Flow 而不是 Num_Flow 进行比较。
  • 话虽如此,它之所以会起作用是因为_Num_Flow 是静态的,这实际上是一个相当大的设计问题。因此,我的建议是在main 中执行此操作,或者在Parking 实例的非静态方法中执行所有操作。
【解决方案2】:

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

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

很容易看出 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);
}

编码愉快!

【讨论】:

  • 感谢您的回答。我认为这对我来说太高级了。我还是个初学者。
【解决方案3】:

我在这个声明中看到了问题

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);

输出:

【讨论】:

  • 这不起作用。它给了我更多奇怪的输出。
  • 只是想确定您是否以这种方式修改了代码ideone.com/DjTHfl
  • 是的,我做到了。这是我得到的一个输出示例: 这是你提供的关于这个停车场的信息: 停车场的名称是:hh 停车场的地址是:hh 这个停车场的可用停车位数量是:10 如何许多汽车正在退出? 2 有多少辆车进入? 12 出车数量为 2 入车数量为 12 目前可用车位数量为:-10 按“Q”退出,任意键继续
  • @Jallous55 查看我在 Eclipse 中得到的输出。
猜你喜欢
  • 2011-09-30
  • 2014-08-06
  • 2016-06-27
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 2011-09-21
  • 2017-05-19
  • 1970-01-01
相关资源
最近更新 更多