2015-02-09 61 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

相關問題