繁体   English   中英

无法使用扫描仪打印字符串

[英]Not able to print a string using scanner

在这里,字符串的输入是“嗨,你好吗”,但我的输出却是单独的“嗨”。 让我知道错误在哪里。 我也尝试过nextLine()。

import java.util.Scanner;


public class Stringintalter {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        System.out.println("enter the num");
        int a = sc.nextInt();
        System.out.println("Enter the Double");
        double b = sc.nextDouble();
        System.out.println("Enter the string");
        String c = sc.next();  //tried out with nextLine() also.

        System.out.println(c);
        System.out.println(b);
        System.out.println(a);
    }

}

这仅是基本错误。 我仍然浏览了很多东西,但仍无法解决错误。

每次你做

    int a = sc.nextInt();

    double b = sc.nextDouble();

您的扫描仪没有读取newLine令牌,因此您应该执行以下操作:

     System.out.println("enter the num");
    int a = sc.nextInt();
    sc.next();
    System.out.println("Enter the Double");
    double b = sc.nextDouble();
    sc.next();
    System.out.println("Enter the string");
    String c = sc.next();  //tried out with nextLine() also.

这段代码对我有用。

import java.util.Scanner;


public class Stringintalter {

     public static void main(String[] args) {

         Scanner sc=new Scanner(System.in);
         System.out.println("enter the num");
         int a = sc.nextInt();
         System.out.println("Enter the Double");
         double b = sc.nextDouble();
         System.out.println("Enter the string");
         sc.nextLine();
         String c = sc.nextLine();
         System.out.println(c);
         System.out.println(b);
         System.out.println(a);
    }

}

也许这段代码会为您提供帮助。

Scanner u = new Scanner(System.in);

     Scanner sc=new Scanner(System.in);
    System.out.println("enter the num");
    int a = sc.nextInt();
    System.out.println("Enter the Double");
    double b = sc.nextDouble();

    System.out.println(b);
    System.out.println(a);


    String c;
    c = u.nextLine();
    System.out.println(c);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM