我想知道为什么我不能将我的文本与代码中的变量连接起来的原因。我该如何解决此代码? [重复]

时间:2023-01-06 23:20:26

This question already has an answer here:

这个问题在这里已有答案:

I am trying to solve a problem in string concatenation But i don't understand it that why it only give me output like this While I am Using a "+" operator. Can anyone help me to clarify what is my problem . My code is

我试图解决字符串连接中的问题但我不明白为什么它只给我这样的输出虽然我使用“+”运算符。任何人都可以帮我澄清我的问题。我的代码是

public static void main(String[] args) {
       int a;
       double b;
       String c;

       Scanner sc=new Scanner(System.in);
       a=sc.nextInt();
       b=sc.nextDouble();
       c=sc.nextLine();
       System.out.println(a+4);
       System.out.println(b+4.0);
       System.out.println("Hackerrank"+" "+c);


    } 

My input is:

我的意见是:

12

4.0

is the best place to learn and practice coding!

是学习和练习编码的最佳场所!

My output is :

我的输出是:

16

8.0

Hackerrank

But Expected Output is:

但预期产出是:

16

8.0

HackerRank is the best place to learn and practice coding!

HackerRank是学习和练习编码的最佳场所!

3 个解决方案

#1


1  

The problem is not with the concatenation. It's the line c=sc.nextLine();. When you use c=sc.nextLine(); JVM assigns the value in the b=sc.nextDouble(); line but after the double value.

问题不在于连接。它是c = sc.nextLine();行。当你使用c = sc.nextLine(); JVM在b = sc.nextDouble()中分配值;行但是在双倍值之后。

Example: According to your input,

示例:根据您的输入,

12

4.0 [c=sc.nextLine(); line reads this part. Just after the Double input]

4.0 [c = sc.nextLine();线读这部分。在双输入之后]

is the best place to learn and practice coding!

是学习和练习编码的最佳场所!

So try this code. It skips the line which mentioned above.

所以试试这个代码。它跳过了上面提到的那条线。

public static void main(String[] args) {
       int a;
       double b;
       String c;

       Scanner sc=new Scanner(System.in);
       a=sc.nextInt();
       b=sc.nextDouble();

       sc.nextLine(); // This line skips the part, after the double value.

       c=sc.nextLine();
       System.out.println(a+4);
       System.out.println(b+4.0);
       System.out.println("Hackerrank"+" "+c);


    } 

#2


0  

Scanner moves the scanner to the next line when you call nextLine() method. But that returns the line it skips. If you provide the input like in a single line
"12 4.0 is the best place to learn and practice coding! " or "12 " in first line and press enter "4.0 is the best place to learn and practice coding!" you will get the desired result.

当您调用nextLine()方法时,扫描仪将扫描仪移动到下一行。但是它会返回它跳过的线。如果您提供单行输入“12 4.0是学习和练习编码的最佳位置!”或第一行中的“12”并按下输入“4.0是学习和练习编码的最佳位置!”你会得到理想的结果。

From JavaDOC

/** * Advances this scanner past the current line and returns the input * that was skipped. * * This method returns the rest of the current line, excluding any line * separator at the end. The position is set to the beginning of the next * line. * *

/ ** *使此扫描器前进超过当前行并返回跳过的输入*。 * *此方法返回当前行的其余部分,不包括末尾的任何行*分隔符。该位置设置为下一个*行的开头。 * *

Since this method continues to search through the input looking * for a line separator, it may buffer all of the input searching for * the line to skip if no line separators are present. * * @return the line that was skipped * @throws NoSuchElementException if no line was found * @throws IllegalStateException if this scanner is closed */

由于此方法继续搜索行查找*以查找行分隔符,因此如果不存在行分隔符,它可以缓冲搜索*要跳过的行的所有输入。 * * @return跳过的行* @throws NoSuchElementException如果没有找到行* @throws IllegalStateException如果此扫描器关闭* /

#3


0  

Print only c first, it gives blank value. It is not assigning values to c. System.out.println(c);

首先打印c,它给出空白值。它没有为c赋值。的System.out.println(C);

Problem is not with + operator if you use

如果使用,问题不在于+运算符

System.out.println("Hackerrank"+" "+sc.nextLine());

Then also you will get expected output.

然后你也会获得预期的输出。

Means you need to write following lines: c=sc.nextLine(); c=sc.nextLine(); Then it will consider expected line in variable c.

意味着您需要编写以下行:c = sc.nextLine(); C = sc.nextLine();然后它会考虑变量c中的预期行。

#1


1  

The problem is not with the concatenation. It's the line c=sc.nextLine();. When you use c=sc.nextLine(); JVM assigns the value in the b=sc.nextDouble(); line but after the double value.

问题不在于连接。它是c = sc.nextLine();行。当你使用c = sc.nextLine(); JVM在b = sc.nextDouble()中分配值;行但是在双倍值之后。

Example: According to your input,

示例:根据您的输入,

12

4.0 [c=sc.nextLine(); line reads this part. Just after the Double input]

4.0 [c = sc.nextLine();线读这部分。在双输入之后]

is the best place to learn and practice coding!

是学习和练习编码的最佳场所!

So try this code. It skips the line which mentioned above.

所以试试这个代码。它跳过了上面提到的那条线。

public static void main(String[] args) {
       int a;
       double b;
       String c;

       Scanner sc=new Scanner(System.in);
       a=sc.nextInt();
       b=sc.nextDouble();

       sc.nextLine(); // This line skips the part, after the double value.

       c=sc.nextLine();
       System.out.println(a+4);
       System.out.println(b+4.0);
       System.out.println("Hackerrank"+" "+c);


    } 

#2


0  

Scanner moves the scanner to the next line when you call nextLine() method. But that returns the line it skips. If you provide the input like in a single line
"12 4.0 is the best place to learn and practice coding! " or "12 " in first line and press enter "4.0 is the best place to learn and practice coding!" you will get the desired result.

当您调用nextLine()方法时,扫描仪将扫描仪移动到下一行。但是它会返回它跳过的线。如果您提供单行输入“12 4.0是学习和练习编码的最佳位置!”或第一行中的“12”并按下输入“4.0是学习和练习编码的最佳位置!”你会得到理想的结果。

From JavaDOC

/** * Advances this scanner past the current line and returns the input * that was skipped. * * This method returns the rest of the current line, excluding any line * separator at the end. The position is set to the beginning of the next * line. * *

/ ** *使此扫描器前进超过当前行并返回跳过的输入*。 * *此方法返回当前行的其余部分,不包括末尾的任何行*分隔符。该位置设置为下一个*行的开头。 * *

Since this method continues to search through the input looking * for a line separator, it may buffer all of the input searching for * the line to skip if no line separators are present. * * @return the line that was skipped * @throws NoSuchElementException if no line was found * @throws IllegalStateException if this scanner is closed */

由于此方法继续搜索行查找*以查找行分隔符,因此如果不存在行分隔符,它可以缓冲搜索*要跳过的行的所有输入。 * * @return跳过的行* @throws NoSuchElementException如果没有找到行* @throws IllegalStateException如果此扫描器关闭* /

#3


0  

Print only c first, it gives blank value. It is not assigning values to c. System.out.println(c);

首先打印c,它给出空白值。它没有为c赋值。的System.out.println(C);

Problem is not with + operator if you use

如果使用,问题不在于+运算符

System.out.println("Hackerrank"+" "+sc.nextLine());

Then also you will get expected output.

然后你也会获得预期的输出。

Means you need to write following lines: c=sc.nextLine(); c=sc.nextLine(); Then it will consider expected line in variable c.

意味着您需要编写以下行:c = sc.nextLine(); C = sc.nextLine();然后它会考虑变量c中的预期行。