0

我已经搜索了大约一个小时,但还没有找到解决我的问题的方法。我正在尝试使用我的扫描仪变量输入稍后在我的 switch 参数中使用它,但我目前被困在第二行,因为正确的语法是为了让我的问题正常工作。这是我的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");

    }
4

1 回答 1

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
    }
}
于 2013-10-28T03:22:15.997 回答