再次问好StackOverflow。我在代码中遇到了一个新问题。是的,这是针对一所学校(高中)。我的问题是这个。到目前为止,这是我的代码。
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner moneyTime = new Scanner(System.in);
System.out.println("Hello world!");
int dep;
int with;
int balance = 1200;
int choice;
while(1 == 1)
{
System.out.println(" ");
System.out.printf("%s%d", "Current Balance: $", balance);
System.out.println(" ");
System.out.println("What would you like to do:");
System.out.println("Type 1 for [DEPOSIT]");
System.out.println("Type 2 for [WITHDRAWAL]");
System.out.println("Type 3 for [EXIT/FINAL]");
choice = moneyTime.nextInt();
if(choice == 1)
{
System.out.print("Amount: ");
dep = moneyTime.nextInt();
deposit(balance, dep);
System.out.println("Testing: " + balance);
}
else if(choice == 2)
{
System.out.print("Amount: ");
with = moneyTime.nextInt();
withdrawl(balance, with);
System.out.println("Testing2: " + balance);
}
else
{
exitORfinal();
}
}
static int deposit(int balance, int dep)
{
int balance1 = balance + dep;
return balance1;
}
static int withdrawal(int balance, int with)
{
int balance1 = balance - with;
return balance1;
}
static int exitORfinal
{
System.out.println("Testing Text to make sure thiworks");
}
}
}
我的代码有一个问题,就是底部的静态代码无法正常工作,并且正在打印出这些错误。
exit status 1
Main.java:46: error: illegal start of expression
static int deposit(int balance, int dep)
^
Main.java:46: error: ';' expected
static int deposit(int balance, int dep)
^
Main.java:46: error: <identifier> expected
static int deposit(int balance, int dep)
^
Main.java:46: error: not a statement
static int deposit(int balance, int dep)
^
Main.java:46: error: ';' expected
static int deposit(int balance, int dep)
^
Main.java:52: error: illegal start of expression
static int withdrawal(int balance, int with)
^
Main.java:52: error: ';' expected
static int withdrawal(int balance, int with)
^
Main.java:52: error: <identifier> expected
static int withdrawal(int balance, int with)
^
Main.java:52: error: not a statement
static int withdrawal(int balance, int with)
^
Main.java:52: error: ';' expected
static int withdrawal(int balance, int with)
^
Main.java:58: error: illegal start of expression
static int exitORfinal
^
Main.java:58: error: ';' expected
static int exitORfinal
^
12 errors
exit status 1
Main.java:46: error: illegal start of expression
static int deposit(int balance, int dep)
^
Main.java:46: error: ';' expected
static int deposit(int balance, int dep)
^
Main.java:46: error: <identifier> expected
static int deposit(int balance, int dep)
^
Main.java:46: error: not a statement
static int deposit(int balance, int dep)
^
Main.java:46: error: ';' expected
static int deposit(int balance, int dep)
^
Main.java:52: error: illegal start of expression
static int withdrawal(int balance, int with)
^
Main.java:52: error: ';' expected
static int withdrawal(int balance, int with)
^
Main.java:52: error: <identifier> expected
static int withdrawal(int balance, int with)
^
Main.java:52: error: not a statement
static int withdrawal(int balance, int with)
^
Main.java:52: error: ';' expected
static int withdrawal(int balance, int with)
^
Main.java:58: error: illegal start of expression
static int exitORfinal
^
Main.java:58: error: ';' expected
static int exitORfinal
^
12 errors
我很困惑。我了解到我可能丢失了一些东西,或者我完全不知道明显的错误。我不知道该怎么办,我尝试了我所知道的一切。我会问老师,但是她很合法,是驴子,即使我们问过其他学生,她也不会帮助任何人。
再次感谢您的帮助(如果有帮助的话)。
答案 0 :(得分:0)
首先,您需要将在main方法中创建的所有方法移出main方法, 第二,您需要向exitORfinal静态方法添加一些参数,例如:exitORfinal(){..} 第三,因为您已声明为moneyTime,所以必须将moneyBoi变量替换为moneyTime。 第四,您必须将提款方法重命名为提款。
您的代码如下:
import java.util.Scanner;
class Main {
static int exitORfinal() {
System.out.println("Testing Text to make sure thiworks");
}
public static void main(String[] args) {
Scanner moneyTime = new Scanner(System.in);
System.out.println("Hello world!");
int dep;
int with;
int balance = 1200;
int choice;
while (1 == 1) {
System.out.println(" ");
System.out.printf("%s%d", "Current Balance: $", balance);
System.out.println(" ");
System.out.println("What would you like to do:");
System.out.println("Type 1 for [DEPOSIT]");
System.out.println("Type 2 for [WITHDRAWAL]");
System.out.println("Type 3 for [EXIT/FINAL]");
choice = moneyTime.nextInt();
if (choice == 1) {
System.out.print("Amount: ");
dep = moneyTime.nextInt();
deposit(balance, dep);
System.out.println("Testing: " + balance);
} else if (choice == 2) {
System.out.print("Amount: ");
with = moneyTime.nextInt();
withdrawl(balance, with);
System.out.println("Testing2: " + balance);
} else {
exitORfinal();
}
}
}
static int deposit(int balance, int dep) {
int balance1 = balance + dep;
return balance1;
}
static int withdrawl(int balance, int with) {
int balance1 = balance - with;
return balance1;
}
}
答案 1 :(得分:0)
该代码如何:
import java.util.Scanner;
class Main {
static int deposit(int balance, int dep) {
return balance + dep;
}
static int withdrawl(int balance, int with) {
return balance - with;
}
static void exitORfinal() {
System.out.println("Testing Text to make sure thiworks");
}
public static void main(String[] args) {
Scanner moneyTime = new Scanner(System.in);
System.out.println("Hello world!");
int dep;
int with;
int balance = 1200;
int choice;
while (true) {
System.out.println(" ");
System.out.printf("%s%d", "Current Balance: $", balance);
System.out.println(" ");
System.out.println("What would you like to do:");
System.out.println("Type 1 for [DEPOSIT]");
System.out.println("Type 2 for [WITHDRAWAL]");
System.out.println("Type 3 for [EXIT/FINAL]");
choice = moneyTime.nextInt();
if (choice == 1) {
System.out.print("Amount: ");
dep = moneyTime.nextInt();
deposit(balance, dep);
System.out.println("Testing: " + balance);
} else if (choice == 2) {
System.out.print("Amount: ");
with = moneyTime.nextInt();
withdrawl(balance, with);
System.out.println("Testing2: " + balance);
} else {
exitORfinal();
}
}
}
}
我认为将这些静态方法放在main
方法之前会更好。
答案 2 :(得分:0)
仅用简单的英语陈述问题: 您的各种方法(exitORfinal,提款,入金)的声明都在主方法的声明内。在Java中,无法在另一个方法主体内定义方法,您必须将其移至类的主体。
此外,我是否建议您使用高效的IDE(Eclipse,Netbeans或带有Java插件(如VSCode或SublimeText)的任何不错的编辑器)?它将很好地强调该问题,甚至提出修复建议。
答案 3 :(得分:0)
在Java中,静态表示它是类的变量/方法,它属于整个类,但不属于其某些对象之一。
这意味着static关键字只能在“类范围”中使用,即在方法内部没有任何意义。
现在,如果在方法内部有一个静态变量,则该变量将在方法的作用域之内,而不是在类的作用域之内,并且JVM无法为其分配内存,因为方法是在类的对象的帮助下调用的,并且那是在运行时,而不是在类加载时。您可以执行以下操作:
public Class YourClass {
static int someNumber = 5;
public static void main(String[] args) {
System.out.println(someNumber);
}
}