第二个字符串 sq 可以使用 JAVA 打印相同的变量吗?
Can the second string sq be printed with same variable using JAVA?
我想打印一个由两个字符串连接而成的字符串:第一个是 declares;第二个是通过nextLine()
输入的。当我以一个 space 作为输入输入两个字符串时,此代码有效,但当我尝试输入一个句子时,它不起作用。
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
int i = 4;
double d = 4.0;
String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
/* Declare second integer, double, and String variables. */
int iq;
double dq;
String sq;
iq=scan.nextInt();
dq=scan.nextDouble();
sq= scan.nextLine();
/* Read and save an integer, double, and String to your variables.*/
// Note: If you have trouble reading the entire String, please go back and review the Tutorial closely.
/* Print the sum of both integer variables on a new line. */
System.out.println(i+iq);
System.out.println(d+ dq);
s= s.concat(sq);
System.out.println(s);
/* Print the sum of the double variables on a new line. */
/* Concatenate and print the String variables on a new line;
the 's' variable above should be printed first. */
scan.close();
}
}
首先,上面提到的代码不适用于任何字符串或句子。已知问题是,当您在 Scanner.next() 或任何 Scanner.nextFoo 方法之后使用 Scanner.nextLine 时,扫描仪将无法正常工作。
解决方法:
在 scan.nextDouble()
之后添加 Scan.nextLine()
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
int i = 4;
double d = 4.0;
String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
/* Declare second integer, double, and String variables. */
int iq;
double dq;
String sq;
iq=scan.nextInt();
dq=scan.nextDouble();
scan.nextLine();
sq= scan.nextLine();
/* Read and save an integer, double, and String to your variables.*/
// Note: If you have trouble reading the entire String, please go back and review the Tutorial closely.
/* Print the sum of both integer variables on a new line. */
System.out.println(i+iq);
System.out.println(d+ dq);
s= s.concat(sq);
System.out.println(s);
/* Print the sum of the double variables on a new line. */
/* Concatenate and print the String variables on a new line;
the 's' variable above should be printed first. */
scan.close();
}
}
我想打印一个由两个字符串连接而成的字符串:第一个是 declares;第二个是通过nextLine()
输入的。当我以一个 space 作为输入输入两个字符串时,此代码有效,但当我尝试输入一个句子时,它不起作用。
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
int i = 4;
double d = 4.0;
String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
/* Declare second integer, double, and String variables. */
int iq;
double dq;
String sq;
iq=scan.nextInt();
dq=scan.nextDouble();
sq= scan.nextLine();
/* Read and save an integer, double, and String to your variables.*/
// Note: If you have trouble reading the entire String, please go back and review the Tutorial closely.
/* Print the sum of both integer variables on a new line. */
System.out.println(i+iq);
System.out.println(d+ dq);
s= s.concat(sq);
System.out.println(s);
/* Print the sum of the double variables on a new line. */
/* Concatenate and print the String variables on a new line;
the 's' variable above should be printed first. */
scan.close();
}
}
首先,上面提到的代码不适用于任何字符串或句子。已知问题是,当您在 Scanner.next() 或任何 Scanner.nextFoo 方法之后使用 Scanner.nextLine 时,扫描仪将无法正常工作。
解决方法:
在 scan.nextDouble()
之后添加 Scan.nextLine()import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
int i = 4;
double d = 4.0;
String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
/* Declare second integer, double, and String variables. */
int iq;
double dq;
String sq;
iq=scan.nextInt();
dq=scan.nextDouble();
scan.nextLine();
sq= scan.nextLine();
/* Read and save an integer, double, and String to your variables.*/
// Note: If you have trouble reading the entire String, please go back and review the Tutorial closely.
/* Print the sum of both integer variables on a new line. */
System.out.println(i+iq);
System.out.println(d+ dq);
s= s.concat(sq);
System.out.println(s);
/* Print the sum of the double variables on a new line. */
/* Concatenate and print the String variables on a new line;
the 's' variable above should be printed first. */
scan.close();
}
}