{{1}}
我需要这个用实际字母打印出实际的字符串,所以如果丹麦是更大的字符串我需要它打印出来给用户。我该怎么做?
此致
标记
答案 0 :(得分:0)
如果我正确理解你,最简单的方法就是用System.out.print()
内的变量连接你需要打印的任何字符串。例如,如果我有一个名为String
的{{1}}类型的变量,我会使用代码行:
myString
此外,除非您希望一行中的所有内容确保您在字符串的末尾添加换行符或使用System.out.print("This is my string variable" + myString);
。
答案 1 :(得分:0)
您实际上还应检查输入的字符串长度是否相等(如果使用>=
或小于或等于<=
,可以缩短以下内容以检查是否大于或等于所需的):
import java.util.Scanner;
class Bigger {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// get user name from the user
System.out.print("Please enter your user name: ");
String userName = input.nextLine();
// get second name from the user
System.out.print("Please enter your second name: ");
String secondName = input.nextLine();
// use an appropriate method to find the number of letters and prompt user
if(userName.length() == secondName.length()) {
System.out.println(userName + " is equal in length than " + secondName);
} else if(userName.length() > secondName.length()) {
System.out.println(userName + " is longer in length than " + secondName);
} else {
System.out.println(userName + " is shorter in length than " + secondName);
}
}
}
使用示例:
Please enter your user name: MarkDoherty
Please enter your second name: Denmark
MarkDoherty is longer in length than Denmark
或者您也可以使用字符串格式:
// use an appropriate method to find the number of letters
if(userName.length() == secondName.length()) {
System.out.printf("%s (%d characters long) is equal in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length());
} else if(userName.length() > secondName.length()) {
System.out.printf("%s (%d characters long) is longer in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length());
} else {
System.out.printf("%s (%d characters long) is shorter in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length());
}
使用示例:
Please enter your user name: MarkDoherty
Please enter your second name: Denmark
MarkDoherty (11 characters long) is longer in length than Denmark (7 characters long)
试试here!