如何在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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:09:08  来源:igfitidea点击:

How to print multiple variable lines in Java

javaprintfprintln

提问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.printfunction (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 printflines 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 Personwith fields firstNameand lastNameand define method toString(). Here I created a util method which returns String presentation of a Personobject.

您可以创建类别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