2013-12-14 2 views
1

내 코드를 살펴볼 때 Eclipse를 사용하고 있는데 가장 일반적인 오류는 "토큰의 구문 오류, 잘못 배치 된 구문"입니다. 내가 무엇을하고 있는지 잘 모르겠습니다. 잘못되었지만 Java에 상당히 익숙합니다.자바 프로그래밍 은행 계좌 코드

내 코드는 표시된 (사용자 입력) 금액을 은행 계좌에서 인출하기로하고, $ 10,000부터 시작하여 인출 금액이 0보다 작거나 10,000 달러보다 클 경우 트리거합니다 주장 오류.

class ThreadsUnitProject2 { 
public static void main(Sting args []) 
// Field member 
private int balance; 

public void BankAccount() 
{ 
balance = 10000; 
} 

public int withdraw(int amount) 
{ 
// Subtract requested amount from balance 
balance-=amount; 

// Return requested amount 
return amount; 
} 


public int getBalance() 
{ 
return balance; 
} 


import java.util.Scanner; 

class BankAccountTester extends BankAccount 
{ 
public static void main(String[] args) 
{ 
    Scanner scan = new Scanner(System.in); 

    BankAccount myAccount = new BankAccount(); 

    System.out.println("Balance = " + myAccount.getBalance()); 

    System.out.print("Enter amount to be withdrawn: "); 
int amount = scan.nextInt(); 

    assert (amount >= 0 && amount <= myAccount.getBalance()):"You can't withdraw that amount!"; 

    myAccount.withdraw(amount); 

    System.out.println("Balance = " + myAccount.getBalance()); 
} 

도움 주셔서 감사합니다.

+1

'import java.util.Scanner;'를 파일의 맨 위로 이동하십시오. –

+0

'public static void main (스팅 args [])'이것은 괄호가 필요합니다. 다른 것들 사이에. –

+0

사람들이 쉽게 읽을 수 있도록 제출할 때 들여 쓰기를 수정해야합니다. 또한 두 개의'main' 메소드가있는 것으로 보입니까? – AVP

답변

0

는 BankAccount에 클래스 "ThreadsUnitProject2"을 이름을 변경하고 그에서 main() 메소드를 제거하고 BankAccountTester 클래스를 공개하고 난 그냥 수정해야했다 마지막으로 코드에서 많은 문제를 본 후 BankAccount가 생성자

0

에서 공백을 제거 그것은 당신이 아래 보이는 해결책을 통해 배우려고합니다.

첫 번째 클래스 파일이어야합니다.

public class BankAccount { 

    private int balance; 

    public BankAccount() {  //constructor 
     balance = 10000; 
    } 

    public int withdraw(int amount) { 
     balance -= amount; 
     return amount; 
    } 

    public int getBalance() { 
     return balance; 
    } 
} 

두 번째 클래스 파일이어야합니다. 여기에는 BankAccount 클래스를 테스트하는 main 메소드가 포함됩니다.

import java.util.Scanner; 

public class BankAccountTester { 
    public static void main(String[] args) { 
     Scanner  scan  = new Scanner(System.in); 
     BankAccount myAccount = new BankAccount(); 

     System.out.println("Balance = " + myAccount.getBalance()); 
     System.out.print ("Enter amount to be withdrawn: "); 
     int amount = scan.nextInt(); 

     assert (amount >= 0 && amount <= myAccount.getBalance()) : "You can't withdraw that amount!"; 
     myAccount.withdraw(amount); 

     System.out.println("NewBalance = " + myAccount.getBalance()); 
    } 
} 

this link to proper coding conventions을 읽고 검토하십시오.

+0

또한 배포 할 때 assert를 프로그램 논리에 사용해서는 안되며, 기본적으로 사용하지 않도록 설정되어 있고 -ea 인수로 활성화해야합니다. 여기에 좋은 설명 : http://stackoverflow.com/questions/2758224/assertion-in-java – LINEMAN78