如何在Java中打印多个可变行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23584563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print multiple variable lines in Java
提问by Harshini
I'm trying to print the test data used in webdriver test inside a print line in Java
我正在尝试在 Java 的打印行内打印 webdriver 测试中使用的测试数据
I need to print multiple variables used in a class inside a system.out.print
function (printf
/println
/whatever).
我需要打印内的课堂上使用多个变量system.out.print
函数(printf
/ println
/不管)。
public String firstname;
public String lastname;
firstname = "First " + genData.generateRandomAlphaNumeric(10);
driver.findElement(By.id("firstname")).sendKeys(firstname);
lastname = "Last " + genData.generateRandomAlphaNumeric(10);
driver.findElement(By.id("lastname")).sendKeys(lastname);
I need those print in a print statement as:
First name: (the variable value I used)
Last name: (the variable value I used)
我需要在打印语句中打印为:
名字:(我使用的变量值)
姓氏:(我使用的变量值)
Using something like below gives the exact result.
But I need to reduce the number of printf
lines and use a more efficient way.
使用类似下面的东西给出了确切的结果。
但是我需要减少行数printf
并使用更有效的方式。
System.out.printf("First Name: ", firstname);
System.out.printf("Last Name: ", lastname);
Thanks!
谢谢!
采纳答案by Totò
You can do it with 1 printf
:
你可以用 1 来做到printf
:
System.out.printf("First Name: %s\nLast Name: %s",firstname, lastname);
回答by Justin Jasmann
System.out.println("First Name: " + firstname);
System.out.println("Last Name: " + lastname);
or
或者
System.out.println(String.format("First Name: %s", firstname));
System.out.println(String.format("Last Name: %s", lastname));
回答by Reborn
Or try this one:
或者试试这个:
System.out.println("First Name: " + firstname + " Last Name: "+ lastname +".");
Good luck!
祝你好运!
回答by Yan Khonski
You can create Class Person
with fields firstName
and lastName
and define method toString()
. Here I created a util method which returns String presentation of a Person
object.
您可以创建类别Person
与领域firstName
,并lastName
和定义方法toString()
。在这里,我创建了一个 util 方法,它返回Person
对象的字符串表示。
This is a sample
这是一个样本
Main
主要的
public class Main {
public static void main(String[] args) {
Person person = generatePerson();
String personStr = personToString(person);
System.out.println(personStr);
}
private static Person generatePerson() {
String firstName = "firstName";//generateFirstName();
String lastName = "lastName";//generateLastName;
return new Person(firstName, lastName);
}
/*
You can even put this method into a separate util class.
*/
private static String personToString(Person person) {
return person.getFirstName() + "\n" + person.getLastName();
}
}
Person
人
public class Person {
private String firstName;
private String lastName;
//getters, setters, constructors.
}
I prefer a separate util method to toString()
, because toString()
is used for debug.
https://stackoverflow.com/a/3615741/4587961
我更喜欢单独的 util 方法toString()
,因为toString()
它用于调试。
https://stackoverflow.com/a/3615741/4587961
I had experience writing programs with many outputs: HTML UI, excel or txt file, console. They may need different object presentation, so I created a util class which builds a String depending on the output.
我有编写具有多种输出的程序的经验:HTML UI、excel 或 txt 文件、控制台。他们可能需要不同的对象表示,所以我创建了一个 util 类,它根据输出构建一个字符串。
回答by Jigar
Suppose we have variable date , month and year then we can write it in the java like this.
假设我们有变量 date ,month 和 year ,那么我们可以像这样在 java 中编写它。
int date=15,month=4,year=2016;
System.out.println(date+ "/"+month+"/"+year);
output of this will be like below:
输出如下:
15/4/2016
15/4/2016
回答by sdesai8243
Answer can be like this
答案可以是这样的
int a=1, b=1, c=1;
System.out.println(a+""+b+""+c);
output
输出
111
111