我是Java编码的初学者。不到4周前刚开始上网络课。我现在正在做一个项目,我需要
通过合理的决策来确定客户的需求。模拟一个有四个加油站的加油站。
在拼凑了我目前所知道的信息之后,我想出了下面的代码。它确实有一些黄色错误,当我在Eclipse中运行时,我只得到打印出来:
welcome to gas station, choose station number 1, 2 or 3. 我能从这里得到一些帮助吗?
public static void main(String[] args) {
int stations;
keyboard2 = new Scanner(System.in);
System.out.print("Welcome to Gas Station \n");
System.out.println("Choose Station Number: \n");
System.out.println("1, 2, or 3 \n");
stations = keyboard2.nextInt(2);
switch (stations) {
case 1:
System.out.println("You entered Station 1.");
break;
case 2:
System.out.println("You entered Station 2.");
break;
case 3:
System.out.println("You entered Station 3.");
break;
default:
System.out.println("Error: Invalid Number");
}
}
private Scanner keyboard;
{
System.out.print("Choose your fuel type:\n");
System.out.println("Press 1 for Unleaded\n");
System.out.println("Press 2 for Unleaded Plus\n");
System.out.println("Press 3 for Unleaded Premium\n");
int gastype;
gastype = keyboard.nextInt(2);
switch (gastype) {
case 1:
System.out.println("You Chose Unleaded.");
break;
case 2:
System.out.println("You Chose Unleaded Plus.");
break;
case 3:
System.out.println("You Chose Unleaded Premium.");
break;
default:
System.out.println("Error: Invalid Number");
}
}
private Scanner keyboard1;
{
System.out.print("Enter gallon amount"
+ "Max amount 30 gallons)");
int numberGallons;
numberGallons = keyboard.nextInt(9);
}
double totalPayment = 0.0;
double numberGallons = 0;
double Unleaded = 0;
double UnleadedPlus = 0;
double UnleadedPremium = 0;
double tax = 0;
private static Scanner keyboard2;
{
Unleaded = 2.50;
UnleadedPlus = 3.00;
UnleadedPremium = 3.50;
tax = 3.5;
totalPayment = numberGallons + Unleaded * tax;
System.out.println("total gas amount: " + numberGallons
+ "\ntotal payment:" + Unleaded * tax + "\nthank you");
}发布于 2018-09-28 04:53:29
如果我用占位符删除部分代码并正确缩进(可以通过IDE中的代码“折叠”和自动格式化来模拟),那么您似乎缺少有关类结构的知识,并且需要更多地练习缩进/注意括号。
public static void main(String[] args)
{//Start main method
int stations;
keyboard2 = new Scanner(System.in);
System.out.print("Welcome to Gas Station \n");
System.out.println("Choose Station Number: \n");
System.out.println("1, 2, or 3 \n");
stations = keyboard2.nextInt(2);
switch (stations)
{
case 1:
System.out.println("You entered Station 1.");
break;
case 2:
System.out.println("You entered Station 2.");
break;
case 3:
System.out.println("You entered Station 3.");
break;
default:
System.out.println("Error: Invalid Number");
}//End Switch
}//End Main Method
private Scanner keyboard;
{
//Snip 1
}
private Scanner keyboard1;
{
//Snip 1
}
//Marker 2
double totalPayment = 0.0;
double numberGallons = 0;
double Unleaded = 0;
double UnleadedPlus = 0;
double UnleadedPremium = 0;
double tax = 0;
private static Scanner keyboard2;
{
//Snip 1
}
}//Class End我在//Snip 1中评论的部分是之外的--您的主要方法,java是将它们解释为类初始化器。
(见https://www.dummies.com/programming/java/what-is-an-initializer-in-java/)
它们不是在主方法中运行的,也根本没有实际运行,因为它们没有静态类初始化器。
私有扫描键盘;私有扫描程序keyboard1;以及//Marker 2下面的另一个字段正在类实例作用域中定义。
与所有主要入口点一样,main方法是静态的,还没有初始化的类,因此类实例范围中的任何内容都没有运行,也没有运行类初始化器。
要解决这个问题,只需删除//End主方法括号,在类的最末端创建另一个方法,那么所有内容都将再次包含在Main方法中。现在我建议自动格式化您的代码。
Eclipse将抱怨字段,这些字段现在将被转换为局部变量,因为它们将在main方法的作用域中定义,因此您可以通过在键盘keyboard1前面删除访问修饰符‘私有’来修复这个问题。实际上,每次使用它时,您都可以使用相同的键盘变量和局部变量,即使没有使它成为类中的字段。
希望这能有所帮助。
编辑:--您可能试图将其分解为多个方法,并将它们与字段混淆。如果是这样的话,您需要了解如何声明方法,仅在字段之后指定{}是不够的。
在这种情况下,Snip 1将标记您试图创建新方法的位置。您需要将它们指定为
private static void keyboard1()
{
Scanner keyboard1 = new Scanner(System.in);
//Snip1
};在这种情况下,private void keyboard1()与Scanner keyboard1没有关系,它可以被命名为任何方便的名称。然后,您需要继续在主方法中调用此方法。因为它是静态的,所以这样做是安全的,但是如果您需要多个加油站,则需要使它们非静态,并初始化一个实例。
如果在方法之间传递变量,则可以将它们声明为类中的字段(注意,它们必须是静态的,因为您在静态主方法中访问它们,而不实例化类)。或者将它们作为参数传递给方法。
https://stackoverflow.com/questions/52548411
复制相似问题