简体   繁体   English

如何用main方法测试程序?

[英]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. 你可以看到,我试图在封堵getUserInputromanToDecimal但我知道,我没有在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: 您需要进行一些更改:

  • If you're going to call your 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. 我建议使其成为静态方法。
  • Currently your 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中将其打印出来
  • In 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.

相关问题 如何在没有 main 方法的情况下在终端中测试我的代码? - How do i test my code in terminal without the main method? 如何从主要方法JAVA测试我的程序 - How to test my program from the main method JAVA 如何在我的主要方法中调用一个方法,以便它可以执行程序? - How do I call a method into my main method so it can execute the program? Java程序。 如何在数组中循环“ isLucky”方法?如何在main方法中打印结果? - Java program. How do I loop my "isLucky method through the array? and how do I print the result in the main method? 如何摆脱main方法中的showUsage()方法? - How do I get out of the showUsage() method in the main method? 如何更改myDate1的值并在主方法中打印出该新值? - How do i change the value of myDate1 and print out that new value within my main method? Java程序。 如何通过我的数组循环我的“isLucky”方法? 另外,如何在 main 方法中正确打印我的结果? - Java program. How do I loop my "isLucky" method through my array? Also, how do I print my results correctly in the main method? 如何将访问器方法打印到主程序中? - How do I print an accessor method into a main program? 如何从方法中获取变量并将其打印在main中? - How do I get a variable out of a method and print it in main? 如何允许我的方法读取我的主要资料 - How do I allow my method to read my main
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM