【问题标题】:How to read from one line separated with only whitespace?如何从仅用空格分隔的一行中读取?
【发布时间】:2014-09-04 08:08:18
【问题描述】:
    int a; 
    int b;                           
    int c;

    Scanner input = new Scanner (System.in);

    //how to read three integers with white space delimiter 
    System.out.print("Enter the 3 edges of the triangle to be calculated: "); 
    int numbers = input.nextInt();

    //then turn 3 integers into boolean form
    //this is only the algorithm
    Boolean isTriangle = ((a+b>c)  && (b+c > a)  && (c+a > b));

           System.out.print(isTriangle);

    else

            System.out.print(isTriangle);

所以不是在单独的行或换行符中输入 3 个整数,我希望它们都在同一行中,只是用空格分隔。我是否需要将标准输入更改为字符串,然后将其解析为布尔部分?我很困惑,因为输入整数后,我不知道将它们存储在哪里以用于布尔部分。

编辑部分:

    public static void main(String[] args){
    int x;
    int y;
    int z;

    Scanner input = new Scanner(System.in);
    System.out.println("Enter three edges: ");

    x = input.nextInt();
    y = input.nextInt();
    z = input.nextInt();

    boolean isTriangle = ((x+y>z)  && (y+z > x)  && (z+x > y));

    if (isTriangle){
        System.out.print("Can edges " + x + ", " + y + ", " + z + " form a triangle"+ isTriangle);
    }
    else {
        System.out.print("Can edges " + x + ", " + y + ", " + z + " form a triangle"+ isTriangle);
    }
    }

为什么当我在 system.out 中调用 x y 和 z 时,它们不显示输入的输入,但是当我只将 isTriangle 放在 system.out 上时,它会给我输出

【问题讨论】:

    标签: java integer whitespace java.util.scanner boolean-expression


    【解决方案1】:

    最好在下面使用。

        String values=scanner.next();//if your input 5 5 5
       String numinString[]=values.split(" ");
       int a=Integer.parseInt(numinString[0]);//a=5
       int b=Integer.parseInt(numinString[1]);//b=5
       int c=Integer.parseInt(numinString[2]);//c=5
    

    【讨论】:

    • 你的例子行不通。 Scanner 类的 next() 方法一直读取到第一个空格。所以如果输入是5 5 5scanner.next() 将只读取第一个5
    • 使用scanner.nextLine();
    • 我知道,我只是指出你的解决方案是错误的,你应该更新你的答案
    【解决方案2】:

    只需拨打nextInt 3 次即可。

    System.out.print("Enter the 3 edges of the triangle to be calculated: ");
    a = input.nextInt();
    b = input.nextInt();
    c = input.nextInt();
    

    有输入

    5 5 5
    

    它打印true

    您必须检查是否有三个可用号码(使用input.hasNextInt() 之前 input.nextInt()):

    System.out.print("Enter the 3 edges of the triangle to be calculated: ");
    if(input.hasNextInt()) {
        a = input.nextInt();
    } else {
        //handle it, you don't have ints!
    }
    if(input.hasNextInt()) {
        b = input.nextInt();
    } else {
        //handle it, you have just one int!
    }
    if(input.hasNextInt()) {
        c = input.nextInt();
    } else {
        //handle it, you have just two ints!
    }
    

    DEMO

    【讨论】:

    • 我首先想到了这一点,但我也认为也许还有另一种读取输入的方式。谢谢!
    • 如何使用 hasNextInt?
    • 可以这样做吗?第 1 行右:输入三个边:边 5、1 和 1 可以形成三角形吗? false 就像 system.out.print 都在同一行?
    【解决方案3】:

    你可以这样做:

        int a;
        int b;
        int c;
    
        Scanner input = new Scanner(System.in);
    
        // how to read three integers with white space delimiter
        System.out.print("Enter the 3 edges of the triangle to be calculated: ");
        a = input.nextInt();
        b = input.nextInt();
        c = input.nextInt();
        // then turn 3 integers into boolean form
        // this is only the algorithm
        Boolean isTriangle = ((a + b > c) && (b + c > a) && (c + a > b));
        if (isTriangle)
            System.out.print(isTriangle);
    
        else
    
            System.out.print(isTriangle);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 2013-08-04
      • 1970-01-01
      相关资源
      最近更新 更多