无法使用扫描仪

时间:2016-07-04 09:49:45

标签: java

这里字符串的输入是"嗨你好吗"但我得到输出" hi"单独。请告诉我错误在哪里。我也试过了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);
    }

}

这只是基本错误。我浏览了很多,但我无法解决错误。

3 个答案:

答案 0 :(得分:1)

每次你做

    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.

答案 1 :(得分:1)

此代码适用于我。

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);
    }

}

答案 2 :(得分:0)

也许这段代码可以帮到你。

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);