java method help
I need help with a method. The method I've written is:
public void readBook(String bookReader, String book);
The method 开发者_运维知识库needs to show both strings of the argument in the console. What else do I need to do to get the method to work?
public void readBook(String bookReader, String book){
System.out.println("Book Reader: " + bookReader);
System.out.println("Book:" + book);
}
This should do it. I recomend google for questions like this though.
Once you've done the above i.e.
public void readBook(String bookReader, String book){
System.out.println("Book Reader: " + bookReader);
System.out.println("Book:" + book);
}
...you need to call it from your main method (or wherever you want) like so:
readBook("The Book Reader", "The Books Name");
in your method body, use
System.out.println( bookReader );
System.out.println( book );
For printing them on separate lines
If all you want is to print the variables, try :
String SPACE = " "; // can make it class level constant.
String separator = SPACE; // can have different string, space is just an example
System.out.println(bookReader + separator + book);
There are lot of ways to embelish it.
There are two ways to print to the console
First:
System.out.print("Your string here");
Second:
System.out.println("Your string here");
The difference between the two is that the second will automatically place the output on a new line in the console.
精彩评论