[英]How do I test out my program in the main method?
This will probably sound like a dumb question to many of you but I'm a new student and I am trying to learn. 对于你们中的许多人来说,这听起来可能是一个愚蠢的问题,但是我是一名新学生,我正在尝试学习。 This is a program that takes a roman numeral input from a user and converts it to it's decimal value.
这是一个程序,它接受用户输入的罗马数字并将其转换为十进制值。 I am trying to test out this program, but I don't know exactly what I have to do in my main method in order to do so.
我正在尝试测试该程序,但我不完全知道要执行该操作的主要方法。 I have the other methods for the calculating but now how am I supposed to test it out?
我还有其他计算方法,但是现在应该如何测试呢? Let me show you what I have:
让我告诉你我所拥有的:
public class RomanNumeralConverter {
public String getUserInput() {
Scanner numberInput = new Scanner (System.in);
System.out.print("Enter a roman numeral in uppercase: ");
String userInput = numberInput.next();
numberInput.close();
return userInput;
}
public static void romanToDecimal(String userInput) {
int decimal = 0;
int lastNumber = 0;
userInput = userInput.toUpperCase();
for (int x = userInput.length() - 1; x >= 0 ; x--) {
char convertToDecimal = userInput.charAt(x);
switch (convertToDecimal) {
case 'M':
decimal = processDecimal(1000, lastNumber, decimal);
lastNumber = 1000;
break;
case 'D':
decimal = processDecimal(500, lastNumber, decimal);
lastNumber = 500;
break;
case 'C':
decimal = processDecimal(100, lastNumber, decimal);
lastNumber = 100;
break;
case 'L':
decimal = processDecimal(50, lastNumber, decimal);
lastNumber = 50;
break;
case 'X':
decimal = processDecimal(10, lastNumber, decimal);
lastNumber = 10;
break;
case 'V':
decimal = processDecimal(5, lastNumber, decimal);
lastNumber = 5;
break;
case 'I':
decimal = processDecimal(1, lastNumber, decimal);
lastNumber = 1;
break;
}
}
System.out.println(decimal);
}
public static int processDecimal(int decimal, int lastNumber, int lastDecimal) {
if (lastNumber > decimal) {
return lastDecimal - decimal;
} else {
return lastDecimal + decimal;
}
}
public static void main(String[] args) {
romanToDecimal(getUserInput);
}
}
You could see that I tried plugging in getUserInput
in to romanToDecimal
but I know that I don't have those parameters in the main method, and I don't even think Java allows me to do that. 你可以看到,我试图在封堵
getUserInput
中romanToDecimal
但我知道,我没有在main方法的参数,我甚至不认为Java允许我这样做。 But, I think this represents what I'm trying to do. 但是,我认为这代表了我正在尝试做的事情。 Really what I want to do is:
我真正想做的是:
System.out.println("The number you entered is " + userInput
System.out.println("The converted number is " + romanToDecimal
Maybe I am supposed to put this in a separate method? 也许我应该将其放在单独的方法中?
There are a few changes you need: 您需要进行一些更改:
getUserInput
method from main
, you either need to make it static
or create an instance of your class. main
调用getUserInput
方法,则需要使其成为static
或创建类的实例。 I'd suggest making it a static method. romanToDecimal
method prints out the result - but it would be neater (in my view) if instead it returned the result, so you can print it in main
romanToDecimal
方法会打印出结果-但如果返回结果,它会更整洁(在我看来),因此您可以在main
中将其打印出来 romanToDecimal(getUserInput)
you're trying to use getUserInput
as if it's a variable, but it's a method. romanToDecimal(getUserInput)
您尝试使用getUserInput
,就像它是一个变量一样,但这是一个方法。 After changing getUserInput
to be static
, and changing romanToDecimal
to return a String
instead of printing it, your main
method could look like this: 将
getUserInput
更改为static
,并将romanToDecimal
更改为返回String
而不是打印它后,您的main
方法可能如下所示:
public static void main(String[] args) {
String input = getUserInput();
String result = romanToDecimal(input);
System.out.println("The number you entered is " + input);
System.out.println("The converted number is " + result);
}
That would be fine as a program . 作为一个程序就可以了。 Once you've got
romanToDecimal
as a method returning the result, you could also easily write unit tests for it, where the input was hard-coded into the test, and you checked the result. 一旦将
romanToDecimal
作为返回结果的方法,您还可以轻松地为其编写单元测试 ,其中将输入硬编码到测试中,然后检查结果。 For example: 例如:
public void test5() {
String result = RomanNumeralConverter.romanToDecimal("V");
Assert.assertEquals(5, result);
}
... and lots more tests for everything you can think of. ...还有更多您可以想到的测试。 (Depending on the unit test framework you choose, you might be able to write a single piece of test code , and specify the input and expected results very compactly as parameters.)
(根据您选择的单元测试框架,您也许可以编写单个测试代码 ,并非常紧凑地将输入和预期结果指定为参数。)
Do this in the main method: 在主要方法中执行此操作:
String input = getUserInput();
romanToDecimal(input);
This should get the code working like it should. 这样可以使代码正常工作。
Just write unit tests testing out the romanToDecimal method with various inputs. 只需编写单元测试,即可使用各种输入测试出romanToDecimal方法。 In Java JUnit is the most popular framework for doing this.
在Java中, JUnit是最流行的框架。
The test would look something like this: 测试看起来像这样:
@Test
public void testMyMethod() {
assertEquals("expected result", romanToDecimal("input"));
}
PS: In order to do this successfully you will need to return a String in your romanToDecimal method! PS:为了成功完成此操作,您将需要在romanToDecimal方法中返回一个String!
try this: 尝试这个:
public static void main(String[] args) {
RomanNumeralConverter rnc = new RomanNumeralConverter();
String userInput = rnc.getUserInput(); // get user input
System.out.println(userInput); // print user input
Tests.romanToDecimal(userInput); // call the romanToDecimal static method to convert and print converted decimal
}
getUserInput
is a function name. getUserInput
是一个函数名称。 getUserInput()
is a call to the function, called getUserInput
, passing no arguments to it (empty parenthesis). getUserInput()
是对函数的调用 ,称为getUserInput
,不getUserInput
传递任何参数(空括号)。 That's what you want: call that function, and use the string it returns. 那就是您想要的:调用该函数,并使用它返回的字符串。
romanToDecimal(getUserInput());
in your main function should work. 在您的主要职能应该工作。 You can also assign it to a variable first, so that you could print it out before calling
romanToDecimal
, and also, make romatToDecimal return a String rather than just printing it out. 您还可以先将其分配给变量,以便可以在调用
romanToDecimal
之前将其打印出来,并且还可以使romatToDecimal返回一个String而不是仅打印出来。 Then you could do something like this: 然后,您可以执行以下操作:
public static void main(String argv[]) {
String input = getUserInput();
String output = romanToDecimal(input);
System.out.ptinln("You enetered: " + input + "\nThe converted number is: " + output);
}
Oh, and as someone has pointed out, you need to make both methods you are calling static. 哦,正如有人指出的那样,您需要将两个方法都设为静态。
If you put the userInput method into your main method then pass userInput to romanToDecimal(getUserInput); 如果将userInput方法放入主方法,则将userInput传递给romanToDecimal(getUserInput); in the main method it will work here is the code for the main method
在main方法中,它将在这里工作的是main方法的代码
public static void main(String[] args) {
Scanner numberInput = new Scanner (System.in);
System.out.print("Enter a roman numeral in uppercase: ");
String userInput = numberInput.next();
numberInput.close();
romanToDecimal(userInput);`
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.