Printf 在 Java 1.7 中无法正常运行
Printf not functioning properly in Java 1.7
我正在为学校做作业,要求程序通过各种方法(姓氏、名字、地址等)获取用户的个人信息,并通过 main 方法输出它们。
问题:printf 语句在显示名称时工作正常,但在显示其余用户信息时,格式不正确。
import java.util.Scanner;
public class PersonalInfo {
public static String lastName, firstName, address, email, phone;
public static Scanner kb = new Scanner(System.in);
public static void main(String[] args)
{
getLastName();
getFirstName();
getAddress();
getEmail();
getPhone();
displayName();
displayAddress();
displayEmailPhone();
}
//--------GET methods--------
public static void getLastName()
{
System.out.print("What is the last name of the user? ");
lastName = kb.nextLine();
}
public static void getFirstName()
{
System.out.print("Now enter the first name: ");
firstName = kb.nextLine();
}
public static void getAddress()
{
System.out.print("Now enter the address: ");
address = kb.nextLine();
}
public static void getEmail()
{
System.out.print("Now enter the email: ");
email = kb.nextLine();
}
public static void getPhone()
{
System.out.print("Lastly, enter the phone number in the format xxx-xxx-
xxxx: ");
phone = kb.nextLine();
}
//--------DISPLAY methods--------
public static void displayName()
{
System.out.printf("\nName:%15s %s", firstName, lastName);
}
public static void displayAddress()
{
System.out.printf("\nAddress:%12s", address);
}
public static void displayEmailPhone()
{
System.out.printf("\nEmail:%14s", email);
System.out.printf("\nPhone:%14s", phone);
}
}
输出结果:
Name: John Smith
Address:1234 street, city, state 12345
Email:useremail@email.com
Phone: 123-456-7890
可能是什么问题?我希望其余信息与名称一致。
尝试将名字和姓氏组合成一个变量 ( fullName ),然后使用该新变量,而不是使用两个单独的字符串。
因此,当您说“%15s”时,您是在说将字段设为 15 个字符宽,并在需要时添加空格。这适用于名称 - 有一堆空格,然后是名称。
不过,地址远远超过 14 个字符,因此您在地址前面没有空格。 (请注意,它不会在 14 处截断;14 只是此处的最小宽度。)
虽然您还没有定义 "line up" 的意思...您想要直接在 "John" 下的“1234”吗?在这种情况下,您需要做的是将这些空格添加到您的 printf 字符串中,并跳过宽度说明符(将其更改为“%s”而不是“%15s”)。
相反,如果您想要 "Smith" 下的邮政编码“12345”,则需要大大增加宽度说明符。宽度说明符加上前缀("Name:" 或 "Address:")需要加起来相同的数字。例如,使用“%50s”作为名称,使用“%47s”作为地址(因为 "Address:" 比 "Name:" 多三个字符)。
我正在为学校做作业,要求程序通过各种方法(姓氏、名字、地址等)获取用户的个人信息,并通过 main 方法输出它们。
问题:printf 语句在显示名称时工作正常,但在显示其余用户信息时,格式不正确。
import java.util.Scanner;
public class PersonalInfo {
public static String lastName, firstName, address, email, phone;
public static Scanner kb = new Scanner(System.in);
public static void main(String[] args)
{
getLastName();
getFirstName();
getAddress();
getEmail();
getPhone();
displayName();
displayAddress();
displayEmailPhone();
}
//--------GET methods--------
public static void getLastName()
{
System.out.print("What is the last name of the user? ");
lastName = kb.nextLine();
}
public static void getFirstName()
{
System.out.print("Now enter the first name: ");
firstName = kb.nextLine();
}
public static void getAddress()
{
System.out.print("Now enter the address: ");
address = kb.nextLine();
}
public static void getEmail()
{
System.out.print("Now enter the email: ");
email = kb.nextLine();
}
public static void getPhone()
{
System.out.print("Lastly, enter the phone number in the format xxx-xxx-
xxxx: ");
phone = kb.nextLine();
}
//--------DISPLAY methods--------
public static void displayName()
{
System.out.printf("\nName:%15s %s", firstName, lastName);
}
public static void displayAddress()
{
System.out.printf("\nAddress:%12s", address);
}
public static void displayEmailPhone()
{
System.out.printf("\nEmail:%14s", email);
System.out.printf("\nPhone:%14s", phone);
}
}
输出结果:
Name: John Smith
Address:1234 street, city, state 12345
Email:useremail@email.com
Phone: 123-456-7890
可能是什么问题?我希望其余信息与名称一致。
尝试将名字和姓氏组合成一个变量 ( fullName ),然后使用该新变量,而不是使用两个单独的字符串。
因此,当您说“%15s”时,您是在说将字段设为 15 个字符宽,并在需要时添加空格。这适用于名称 - 有一堆空格,然后是名称。
不过,地址远远超过 14 个字符,因此您在地址前面没有空格。 (请注意,它不会在 14 处截断;14 只是此处的最小宽度。)
虽然您还没有定义 "line up" 的意思...您想要直接在 "John" 下的“1234”吗?在这种情况下,您需要做的是将这些空格添加到您的 printf 字符串中,并跳过宽度说明符(将其更改为“%s”而不是“%15s”)。
相反,如果您想要 "Smith" 下的邮政编码“12345”,则需要大大增加宽度说明符。宽度说明符加上前缀("Name:" 或 "Address:")需要加起来相同的数字。例如,使用“%50s”作为名称,使用“%47s”作为地址(因为 "Address:" 比 "Name:" 多三个字符)。