我已经搜索了大约一个小时,还没有找到解决我问题的方法。我试图使用我的扫描仪变量输入稍后使用它到我的开关参数但我目前卡在第二行正确的语法是为了我的问题正常工作。 这是我的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");
}
答案 0 :(得分: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
}
}