Java 加油站项目编码烦恼
Java Gas Station project coding troubles
我是 Java 编码的初学者。不到 4 周前刚开始在线 class。我目前正在做一个我需要
的项目
traverse a logical decision to determine the need of a customer. Simulate a gas station that has 4 stations...
在拼凑我目前所知道的之后,我想出了下面的代码。它确实有一些黄色错误,当我在 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");
}
您似乎缺少有关 class 结构的知识,并且需要更多练习缩进/注意括号,如果我用占位符删除部分代码并正确缩进(这可以被 "folding" 您 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 将这些解释为 class 初始值设定项。
(参见 https://www.dummies.com/programming/java/what-is-an-initializer-in-java/)
这些不是 运行 您的主要方法,实际上根本不是 运行ning,因为它们不是 static class初始值设定项。
私有扫描仪键盘;
私人扫描仪键盘1;
//Marker 2
下面的其他 fields 正在 class 实例范围内定义。
你的主要方法,像所有主要入口点一样,是静态的,没有 classes 尚未初始化,所以 class 实例范围内的任何东西都不是 运行ning,并且没有 class 个初始值设定项 运行。
要解决此问题,只需删除 //End Main Method 括号,在 class 的最后创建另一个括号,所有内容将再次包含在 main 方法中。我建议此时自动格式化您的代码。
Eclipse 会抱怨这些字段,这些字段现在将变成局部变量,因为它们将在 main 方法的范围内定义,因此您可以通过删除前面的访问修饰符 'private' 来解决这个问题键盘,keyboard1。事实上,您可以在每次使用时使用相同的键盘变量作为局部变量,即使不将其作为 class.
中的字段
希望这对您有所帮助。
编辑: 看来您可能一直在尝试将其拆分为多个方法,并使它们与字段混淆。如果是这样,您需要阅读如何声明方法,仅在字段后指定 {} 是不够的。
在这种情况下,Snip 1
将标记您尝试创建新方法的位置。
您需要将它们指定为
private static void keyboard1()
{
Scanner keyboard1 = new Scanner(System.in);
//Snip1
};
在这种情况下,private void keyboard1()
与 Scanner keyboard1
没有任何关系,可以方便地命名。然后,您需要继续在主方法中调用此方法。因为它是静态的,所以你这样做是安全的,但如果你需要多个加油站,你需要让它们成为非静态的,并初始化一个实例。
如果您无法在方法之间传递变量,您可以 1. 在 class 中将它们声明为字段(注意它们必须是静态的,因为您是在静态 main 方法中访问它们,而不实例化 class。)或者将它们作为参数传递给方法。
我是 Java 编码的初学者。不到 4 周前刚开始在线 class。我目前正在做一个我需要
的项目traverse a logical decision to determine the need of a customer. Simulate a gas station that has 4 stations...
在拼凑我目前所知道的之后,我想出了下面的代码。它确实有一些黄色错误,当我在 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");
}
您似乎缺少有关 class 结构的知识,并且需要更多练习缩进/注意括号,如果我用占位符删除部分代码并正确缩进(这可以被 "folding" 您 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 将这些解释为 class 初始值设定项。
(参见 https://www.dummies.com/programming/java/what-is-an-initializer-in-java/)
这些不是 运行 您的主要方法,实际上根本不是 运行ning,因为它们不是 static class初始值设定项。
私有扫描仪键盘;
私人扫描仪键盘1;
//Marker 2
下面的其他 fields 正在 class 实例范围内定义。
你的主要方法,像所有主要入口点一样,是静态的,没有 classes 尚未初始化,所以 class 实例范围内的任何东西都不是 运行ning,并且没有 class 个初始值设定项 运行。
要解决此问题,只需删除 //End Main Method 括号,在 class 的最后创建另一个括号,所有内容将再次包含在 main 方法中。我建议此时自动格式化您的代码。
Eclipse 会抱怨这些字段,这些字段现在将变成局部变量,因为它们将在 main 方法的范围内定义,因此您可以通过删除前面的访问修饰符 'private' 来解决这个问题键盘,keyboard1。事实上,您可以在每次使用时使用相同的键盘变量作为局部变量,即使不将其作为 class.
中的字段希望这对您有所帮助。
编辑: 看来您可能一直在尝试将其拆分为多个方法,并使它们与字段混淆。如果是这样,您需要阅读如何声明方法,仅在字段后指定 {} 是不够的。
在这种情况下,Snip 1
将标记您尝试创建新方法的位置。
您需要将它们指定为
private static void keyboard1()
{
Scanner keyboard1 = new Scanner(System.in);
//Snip1
};
在这种情况下,private void keyboard1()
与 Scanner keyboard1
没有任何关系,可以方便地命名。然后,您需要继续在主方法中调用此方法。因为它是静态的,所以你这样做是安全的,但如果你需要多个加油站,你需要让它们成为非静态的,并初始化一个实例。
如果您无法在方法之间传递变量,您可以 1. 在 class 中将它们声明为字段(注意它们必须是静态的,因为您是在静态 main 方法中访问它们,而不实例化 class。)或者将它们作为参数传递给方法。