2015-02-09 57 views
0

大家好,我正在试图获取它,因此我的程序能够接收多个用户的数据,然后在while循环完成后打印所有数据完成(我的计数器设置为1,因此可以在最后输入一组新信息,但如果它达到两个人的限制,它将停止并打印信息)。我已经能够使用用户输入数据正确地打印出信息,但是我无法让多个人输入数据,即使能够打印出数据也无法打印出多个数据集接收不止一个用户的数据。我会如何做这两件事?这里是我的代码,我到目前为止有:如何输入多个单位的数据,并利用Java中的计数器

public class credLimit { 
public static void main(String[]args){ 
    Scanner input = new Scanner(System.in); 

    int newBalance = 0; 
    int credCounter = 1; 


    while(credCounter <= 2){ 
     System.out.print("Enter account number: "); 
     int accNum = input.nextInt(); 
     System.out.print("Enter your beginning balance: "); 
     int beginningBalance = input.nextInt(); 
     System.out.print("Enter your total charges this month: "); 
     int charges = input.nextInt(); 
     System.out.print("Enter your total credit applied this month: "); 
     int credit = input.nextInt(); 
     System.out.print("Enter your allowed credit limit: "); 
     int maxcredLimit = input.nextInt(); 
     newBalance = beginningBalance + charges - credit; 
     credCounter = credCounter + 1; 

     System.out.printf("%nAccount number: %d%n", accNum); 
     System.out.printf("Your new balance: %d%n", newBalance); 
     if(newBalance <= maxcredLimit) 
      System.out.printf("You have not exceeded your credit limit. %d%n"); 
     else if (newBalance > maxcredLimit) 
      System.out.printf("Credit limit exceeded. %d%n"); 
    } 
} 

我能够采取多组的信息,但现在我只能获得用户数据的一个进行拍摄,计算(以确定其信用额度已超过),并打印出来。由于某种原因,它会停止一个用户的信息,而不是让两个用户输入他们的信息,为什么?

回答

1

我猜你的代码崩溃,因为你没有任何参数传递给那些过去2 print语句:

if(newBalance <= maxcredLimit) 
    System.out.printf("You have not exceeded your credit limit. %d%n"); 
else if (newBalance > maxcredLimit) 
    System.out.printf("Credit limit exceeded. %d%n"); 

您是不是要找:

if(newBalance <= maxcredLimit) 
    System.out.printf("You have not exceeded your credit limit. %d%n", newBalance); 
else if (newBalance > maxcredLimit) 
    System.out.printf("Credit limit exceeded. %d%n", newBalance); 
+0

这是怎么回事,它仍然允许一组数据正确处理,为什么不是两个? – Fyree 2015-02-09 00:58:58

+0

你在IDE中工作吗?我在想你的程序崩溃了,你没有看到它提供的控制台中的错误。 即使对于一组数据,您的代码是否打印出这些打印语句之一? 当我运行你的代码时,你的程序在为第一组数据输入后就崩溃了,这是因为这些打印语句。 – Kacy 2015-02-09 01:03:20

+0

我正在使用一个IDE,并获得第一组数据的工作,(Eclipse),但是我确实得到了这个确切的错误代码后的数据:线程“主”java.util.MissingFormatArgumentException异常:格式说明符'd ' \t在java.util.Formatter.format(未知来源) \t在java.io.PrintStream.format(未知来源) \t在java.io.PrintStream.printf(未知来源) \t在MyName_Project.credLimit。 main(credLimit.java:30) – Fyree 2015-02-09 01:16:09

相关问题