Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要这个来返回输入的内容吗_Java - Fatal编程技术网

Java 需要这个来返回输入的内容吗

Java 需要这个来返回输入的内容吗,java,Java,我需要这个打印出实际的字符串和实际的字母,所以如果丹麦是更大的字符串,我需要它打印出来给用户。我该怎么做 问候, 标记如果我理解正确,最简单的方法就是将需要打印的字符串与System.out.print()中的变量连接起来。例如,如果我有一个名为myString的String类型的变量,我将使用以下代码行: import java.util.Scanner; public class Bigger{ public static void main(String [] args) { // de

我需要这个打印出实际的字符串和实际的字母,所以如果丹麦是更大的字符串,我需要它打印出来给用户。我该怎么做

问候,


标记

如果我理解正确,最简单的方法就是将需要打印的字符串与
System.out.print()中的变量连接起来。例如,如果我有一个名为
myString
String
类型的变量,我将使用以下代码行:

import java.util.Scanner;
public class Bigger{

public static void main(String [] args)
{
// declare variables
Scanner keyboardIn = new Scanner(System.in);
String userName = new String();
String fName = new String();
int numberLetters = 0;
int bigLetters=0;
char firstLetter;

// get user name from the user
System.out.print("Please enter your user name: ");
userName = keyboardIn.nextLine();

 // get second name from the user
System.out.print("Please enter your second name: ");

 fName = keyboardIn.nextLine();

 // use an appropriate method to find the number of letters
 numberLetters = userName.length();
 bigLetters = fName.length();

if(numberLetters > bigLetters)
{
    System.out.print("String 1 Is the longest string ");
}
else
{
    System.out.print("String 2 Is the longest string "); 
 }
 }
 }

此外,除非您希望所有内容都在一行中,否则请确保在字符串末尾追加换行符,或者使用
System.out.println()

您还应该检查输入的字符串的长度是否相等(以下内容可以缩短为使用
=
检查大于或等于或小于或等于
开始时,请告诉我们您遇到的问题。这里不是代码检查的地方。如果您有关于代码的特定问题,则需要显式询问。否则,人们不太可能调试您的代码you.So…
System.out.print(用户名+“是最长的字符串”);
?谢谢你们的帮助。你们都帮了大忙。我的问题得到了回答。我只需要程序接受用户的两个名称,然后打印出最长的名称。我只能让它显示哪个变量的名称最长,所以它只是说变量1更大或变量2更大。我想要它所以它将返回最大的变量和该变量的内容。既然我知道你们都很敏感,下次我会更加小心地问一个问题。
System.out.print("This is my string variable" + myString);
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)