How to enable assertions?

痴心易碎 提交于 2020-12-25 09:46:56

问题


In my online Java programming class I have to write a program where the user enters their age, and checks if it's between 0 and 125 - if it isn't, it shows an error code, and I need to use assertions to do that. Here is my code:

import java.util.Scanner;

public class eproject1 {

   public static void main(String args[]) {

      Scanner input = new Scanner(System.in);

      System.out.print("What is your age? ");

      int num = input.nextInt();

      System.out.println(num);

      assert(num >= 1 && num < 125);

      System.out.printf("You entered %d\n", num); // NEED TO FIX, SOMETHING ISN'T RIGHT

   }

}

The problem is that it tells you what age you entered, even if it's out of the range 1 to 124. Research says that I need to actually enable assertions, but the results I found online were very unhelpful as they involved some Eclipse stuff - I'm sure it's some kind of rookie mistake, since i'm not very good at programming.

So ... how do you get the assertion stuff to work?


回答1:


java -ea <program_name> should do it on the command prompt.

You can try the following for intellJ and eclipse respectively.



来源:https://stackoverflow.com/questions/27435729/how-to-enable-assertions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!