除了使用nextLine()和我需要用空格打印整个语句外,如何在java中输入字符串

除了使用nextLine()和我需要用空格打印整个语句外,如何在java中输入字符串,java,Java,因此,在下面的代码中,我想输入一个字符串,但不使用函数nextLine(),我需要用语句打印整个语句。我已经尝试了另一个关键字next()它只打印第一个单词,因为有一个空格,它停止了。但我需要打印整个报表 那么,在这种情况下,解决方案应该是什么 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner scan = new Sca

因此,在下面的代码中,我想输入一个字符串,但不使用函数
nextLine()
,我需要用语句打印整个语句。我已经尝试了另一个关键字
next()
它只打印第一个单词,因为有一个空格,它停止了。但我需要打印整个报表

那么,在这种情况下,解决方案应该是什么

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int i = scan.nextInt();
        double d = scan.nextDouble();
        String s = scan.next();

        // Write your code here.

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

您可以使用数组存储来自的输入,也可以使用列表(更可取),然后只显示列表的内容

Scanner scan = new Scanner(System.in);
List<String> tempList = new ArrayList<>();

int i = scan.nextInt();
double d = scan.nextDouble();
String s = scan.next();

while(!s.equalsIgnoreCase("exit")){
       tempList.add(s);
       s = scan.next();
}

System.out.println("String: " + tempList.toString().replace("[","")
        .replace("]","").replace(",",""));
System.out.println("Double: " + d);
System.out.println("Int: " + i);
Scanner scan=新的扫描仪(System.in);
List templast=new ArrayList();
int i=scan.nextInt();
double d=scan.nextDouble();
字符串s=scan.next();
而(!s.equalsIgnoreCase(“退出”)){
圣殿骑士;
s=scan.next();
}
System.out.println(“字符串:”+templast.toString().replace(“[”,”)
.replace(“],”).replace(“,”,”);
System.out.println(“双精度:+d”);
System.out.println(“Int:+i”);
Scanner.next()
方法查找并返回下一个完整的令牌

没有
nextLine()
和数组是很难做到的。但这就是我厌倦了不用数组和
nextLine()。以某种方式必须输入一个值(在本例中,我使用了
-1
)来结束循环

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    int i = scan.nextInt();
    double d = scan.nextDouble();

    boolean flag = true;
    String s="";

    System.out.println("enter -1 to exit");
    while(flag){
        s += scan.next() + " ";
        if(s.contains("-1")){
            flag = false;
        }
    }

    System.out.println("String: " + s.replace("-1", ""));
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
}
您需要在字符串输入扫描仪之前放置一个额外的scan.nextLine来使用额外的\n

int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.next();

很简单,只需使用Scanner变量的另一个参考变量, 在
.nextLine()
方法读取
.nextLine()的nextLine标记之前
因此,通过创建新对象,它会清除临时内存

import java.util.Scanner;

class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double d=scan.nextDouble();
        int i = scan.nextInt();
        Scanner newscan=new Scanner(System.in);
        String s=newscan.nextLine();
        // Write your code here.

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

如果我们使用字符串s=scan.next();它将只初始化第一个字。要初始化整个语句,我们必须使用字符串s=scan.nextLine();虽然这个代码片段可以解决这个问题,但它确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。
import java.io.*;
import java.util.Scanner;

class Solution {
   public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double d=scan.nextDouble();
        int i = scan.nextInt();
        scan.nextLine();
        String s=scan.nextLine();
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
 }