无法划分2个整数

时间:2016-09-16 06:29:30

标签: java

我无法在此代码中划分两个字符。我需要找到cansNeeded的值,但我一直收到错误

    import java.util.Scanner;
    import java.lang.Math;     // Note: Needed for math functions

    public class PaintEstimator {
    public static void main(String[] args) {
      //Create a new scanner
      Scanner scnr = new Scanner(System.in);


      //Set the values
      double wallHeight = 0.0;
      double wallWidth = 0.0;
      double wallArea = 0.0;
      double gallonsPaintNeeded = 0.0;
      final double squareFeetPerGallons = 350.0;
      final double gallonsPerCan = 1.0;



      //Scan for user input
      wallHeight = scnr.nextDouble();
      wallHeight = scnr.nextDouble();
      wallArea = (wallHeight * wallWidth);
      gallonsPaintNeeded = (wallArea/squareFeetPerGallons);

      //Print out the values
      System.out.println("Enter wall height (feet): ");
      System.out.println("Enter wall width (feet): ");
      System.out.println("Wall area:  square feet");
      System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");




      double cansNeeded = (gallonsPaintNeeded / gallonsPerCan);  
      System.out.println("Cans needed: " + cansNeeded + " can(s)");  

      return;
   }
}

我一直收到此错误

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at PaintEstimator.main(PaintEstimator.java:21)

添加double似乎解决了我的原始问题“/”符号导致错误。但现在我有以下错误。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

您的计划存在一些问题。

首先,您没有为wallWidth分配值,但为wallHeight分配了两次,结果为所有零值。

其次,在打印提示信息之前等待用户输入。

最后,不需要返回语句:)

这是一个工作版本,当然可以进一步调整,只是为了给出一个想法。

import java.util.Scanner;

public class PaintEstimator {

    public static void main(String[] args) {
        // Create a new scanner
        Scanner scnr = new Scanner(System.in);

        // Set the values
        double wallHeight = 0.0;
        double wallWidth = 0.0;
        double wallArea = 0.0;
        double gallonsPaintNeeded = 0.0;
        final double squareFeetPerGallons = 350.0;
        final double gallonsPerCan = 1.0;

        // Print out the values
        System.out.print("Enter wall height (feet): ");
        wallHeight = scnr.nextDouble();

        System.out.print("Enter wall width (feet): ");
        wallWidth = scnr.nextDouble();

        wallArea = (wallHeight * wallWidth);
        System.out.println(String.format("Wall area: %f square feet", wallArea));

        gallonsPaintNeeded = (wallArea / squareFeetPerGallons);
        System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");

        double cansNeeded = (gallonsPaintNeeded / gallonsPerCan);
        System.out.println("Cans needed: " + cansNeeded + " can(s)");
    }
}