如何启用断言? [英] How to enable assertions?
问题描述
在我的在线Java编程课程中,我必须编写一个程序,让用户输入年龄,并检查该年龄是否在0到125之间-如果不是,则显示错误代码,我需要使用断言来去做.这是我的代码:
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
}
}
问题是,它告诉您输入的年龄,即使它不在1到124的范围内.研究表明我需要实际启用断言,但是我在网上发现的结果非常无助,因为它们涉及某些Eclipse.东西-我确定这是菜鸟犯的错误,因为我不太擅长编程.
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?
推荐答案
java -ea< program_name>
应该在命令提示符下执行.
java -ea <program_name>
should do it on the command prompt.
对于 intellJ,您可以尝试以下操作和 eclipse .
这篇关于如何启用断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!