找不到我的逻辑错误



我制作了一个简单的程序:使用文件(读写)结束类扩展但是程序不起作用。Netbeans没有显示错误,但当我运行它时。。。。。。出现了一些错误。。。。。我不明白我的bug在哪里(我认为这是一个合乎逻辑的bug)。

这是一个简单的程序:

package detyre_kursi;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Detyre_kursi {
  public static void main(String[] args) {
    LlogariBankare llogaria1 = new LlogariBankare("aaa", 1000); 
    llogaria1.Balanca();    
  }
}

class LlogariBankare {
  //variablat e instances
  private String id;
  private int nrLlogarise;
  private int vitiHapjes;
  private double balanca;
  static int nrTranasksioneve = 0;
  public LlogariBankare() {
    System.out.println("Ju keni harruar te vendosi id dhe nrLlogarise");
  }
  public LlogariBankare(String id, int nrLlogarise) {
    this.id = id;
    this.nrLlogarise = nrLlogarise;
    vitiHapjes = 0;
    balanca = 0; 
    Lexim(this.id, this.nrLlogarise); 
  }
  public double getBalanca() {
    return balanca;
  }
  public int getVitiHapjes() {
    return vitiHapjes;
  }
  private void Lexim(String s, int llog) {
    try {
      File file = new File("c:\java\balanca.txt");
      Scanner scanner = new Scanner(file);
      while (scanner.hasNextLine()) {
        if (scanner.next().equals(s) && scanner.nextInt() == llog) {
          vitiHapjes = scanner.nextInt();
          balanca = scanner.nextDouble();
        }
      }
    } catch (IOException e) {
      e.getMessage();
    }   
  }

  void Balanca() { 
    try{
      File file = new File("c:\java\test.txt");
      PrintWriter out = new PrintWriter(file);
      out.println(this.balanca);  
    } catch (IOException e) {
      e.getMessage();
    }   
    System.out.println(this.id + " , ju keni " + this.balanca + 
        " lek ne llogarine tuaj te krijuar ne vitin " + vitiHapjes + 
        " dhe keni kryer " + nrTranasksioneve + " transaksione gjithsej");
  }

  void Terheqe(double terheqe) {
    this.balanca -= terheqe;
    System.out.println("Ju sapo keni terhequr " + terheqe + " nga llogaria juaj");
    nrTranasksioneve++;
  }
  void Depozitim(double depozitim) {
    this.balanca += depozitim;
    System.out.println("Ju sapo keni depozituar " + depozitim + " nga llogaria juaj");
    nrTranasksioneve++;
  }

}
class Interesi extends LlogariBankare {
  int vitiTanishem = 2012;
  double interesi = 0;
  int diferencaViteve = vitiTanishem - getVitiHapjes();
  Interesi(String id, int nrLlogarise) {
    super(id,nrLlogarise); 
  }
  void gjejInteresisn() {
    interesi = getBalanca() + getBalanca() * diferencaViteve * 0.01;
  }
}

文件balanca中有以下行:aaa 1000 1990 34000

用糟糕的话来说,这是一家银行的简单版本。

您从文件中读取余额,然后您使用Terheqe()和Depozitim()来获得-和+的平衡。您可以使用Balance()来查看您有多少美元。当我运行它时,会出现以下错误:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at detyre_kursi.LlogariBankare.Lexim(Detyre_kursi.java:57)
at detyre_kursi.LlogariBankare.<init>(Detyre_kursi.java:40)
at detyre_kursi.Detyre_kursi.main(Detyre_kursi.java:11)

Java结果:1

此行导致问题。scanner.nextInt()可能不是int,我觉得除非有具体原因,否则执行两个next()调用是不好的。

if(scanner.next().equals(s)&&scanner.nextInt()==llog){

这只是一个猜测,但请尝试替换:

scanner.next().equals(s)

带有:

s.equals(scanner.next())

我认为您的逻辑问题来自的构造函数

LlogariBankare llogaria1 = new LlogariBankare("aaa", 1000); 

再看看。

相关内容

  • 没有找到相关文章

最新更新