2013-10-28 76 views
0

我已經搜索了大約一個小時,並且還沒有找到解決我的問題。我試圖使用我的掃描器變量輸入來稍後將它用於我的開關參數,但是我現在被困在第二行上,正確的語法是爲了讓我的問題正常工作。 這裏是我的Java問題:在java中的掃描儀將不匹配我的雙變量

//創建KW輸入掃描儀

Scanner kw = new Scanner(System.in); 
    double kwUsed = kw; 

    //Verify if KW is valid 
    try 
    { 
     //KW used data entry 
     System.out.println("Please enter KW used: "); 
     kwUsed = kw.nextDouble(); 
     System.out.println("KW entry valid"); 
    } 
    catch (Exception error) 
    { 
     System.out.println("Invalid entry"); 
     System.out.println(error); 
    } 

    //Date entry will be calculated by how much KW user enters 
    switch (input) 
    { 
     case "2/15/13": 
      double quarter1 = kwUsed * 0.10; 
      break; 
     case "4/15/13": 
      double quarter2 = kwUsed * 0.12; 
      break; 
     case "8/15/13": 
      double quarter3 = kwUsed * 0.15; 
      break; 
     case "11/15/13": 
      double quarter4 = kwUsed * 0.15; 
      break; 
     default: 
      System.out.println("Invalid date"); 

    } 
+2

這條線的重點是什麼'double kwUsed = kw;'? – Tdorno

+0

只是'雙kwUsed;'? – nhgrif

+0

@Tdorno我想把kw用於我的開關,但是kwUsed沒有啓動,但是如果我嘗試啓動kwUsed,我的語法是雙kwUsed = kw.nextDouble();但我不想只在我的try/catch中有第二行的輸入來驗證它。我不知道正確的語法是否與我的try/catch和switch切換相對應。 – user2926620

回答

0

只想常說我不會只是修復某人的代碼,然後將其粘貼,但在這種情況下,我覺得它的更好地讓OP看到一些製造的小錯誤,而不是漫不經心。

import java.util.Scanner; 
import java.io.IOException; 
import java.io.FileReader; 
import java.io.File; 
import java.io.BufferedReader; 

public class MainTest { 
    public static void main(String[] args) { 

     Scanner kw = new Scanner(System.in); 
     double kwUsed = 0; 

     // Verify if KW is valid 
     try { 

      // KW used data entry 
      System.out.println("Please enter KW used: "); 
      kwUsed = kw.nextDouble(); 
      System.out.println("KW entry valid"); 
     } catch (Exception error) { 
      System.out.println("Invalid entry"); 
      System.out.println(error); 
     } 

     // Date entry will be calculated by how much KW user enters 
     String input; 
     System.out.println("Enter input case"); 
     input = kw.next(); 

     switch (input) { 
     case "2/15/13": 
      double quarter1 = kwUsed * 0.10; 
      break; 
     case "4/15/13": 
      double quarter2 = kwUsed * 0.12; 
      break; 
     case "8/15/13": 
      double quarter3 = kwUsed * 0.15; 
      break; 
     case "11/15/13": 
      double quarter4 = kwUsed * 0.15; 
      break; 
     default: 
      System.out.println("Invalid date"); 

     } 
      //handle your quarter1-4 here 
    } 
} 
+0

非常感謝!你明確地爲我清理了一些東西。 – user2926620

+0

@ user2926620很高興能幫到你。 – Tdorno