2016-09-16 56 views
-1

我無法在此代碼中劃分兩個字符。我需要找到cansNeeded的價值,但我不斷收到一個錯誤無法劃分2個整數

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) 

添加雙似乎解決了我原來的「/」符號的問題引起錯誤。但現在我有下面的錯誤。任何幫助?

+1

你說的是整數和字符,但實際上是使用雙打。你能否試着讓你的問題更清楚(正確)?它會更容易理解 – Stultuske

+0

哪一行是21行? –

+1

看起來像缺少輸入。你輸入了什麼?另外,您的提示在提供輸入後打印。應該可以逆轉。 – Thilo

回答

0

您的程序有幾個問題。

首先,您沒有爲wallWidth分配值,但是爲wallHeight做了兩次,結果給出了所有零值。

其次,您在打印出提示消息之前等待用戶輸入。

最後,不需要return語句:)

這裏是一個工作版本,還可以進一步的過程中調整了,只給一個想法。

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