2013-04-16 104 views
-1

我在java中的while語句遇到問題。這裏是我的代碼(我對此很新)...做...雖然不循環

import java.text.DecimalFormat; 
import java.util.Scanner; 

public class ElecticBillLab6 { 
    public static void main(String[] args) { 

    int accountNumber; 
    int customerName; 
    int oldMeterReading; 
    int newMeterReading; 
    int usage; 
    double charge = 0; 
    String name; 
    String lastName; 
    String response; 
    final double baseCharge = 5.00; 
    int yes = 1; 
    int no = 2; 
    boolean YesResponse; 

    Scanner keyBoard = new Scanner(System.in); 

    do { 

     System.out.print("Customer's first Name? "); 
     name = keyBoard.next(); 
     System.out.print("Customer last name?"); 
     lastName = keyBoard.next(); 
     System.out.print("Enter customer account number "); 
     accountNumber = keyBoard.nextInt(); 
     System.out.print("Enter old meter reading "); 
     oldMeterReading = keyBoard.nextInt(); 
     System.out.print("Enter new meter reading "); 
     newMeterReading = keyBoard.nextInt(); 

     usage = (newMeterReading - oldMeterReading); 

     if (usage < 301) { 
      charge = 5.00; 

     } else if (usage < 1001) { 
      charge = (5.00 + (.03 * (usage - 300))); 
     } else if (usage > 1000) { 
      charge = (35.00 + ((usage - 1000) * .02)); 
     } 

     DecimalFormat df = new DecimalFormat("$,###.00"); 
     System.out.println("PECO ENERGY"); 
     System.out.println("Customer Account Number " + accountNumber); 
     System.out.println("Name on Account " + name + lastName); 
     System.out.println("Last Month Meter Reading: " + oldMeterReading); 
     System.out.println("This Month Meter Reading: " + newMeterReading); 
     System.out.println("Current Usage : " + usage); 
     System.out.println("Your current month charges are " 
       + (df.format(charge))); 

     System.out 
       .println("Do you want to enter another customer's meter reading?"); 
     System.out.print("Enter 1 for yes or 2 for no."); 
     response = keyBoard.next(); 

    } while (response == "1"); 

} 
} 

該程序執行一次並且不能正確循環。哪裏不對?

+4

不應該'response ==「1」'是'response.equals(「1」)'? – Blender

+0

非常感謝!你們都搖滾! – user2284562

回答

3

嘗試:

response.equals("1") 

==應用於字符串只返回true如果是同一個對象。

5

您無法將字符串與==比較。

使用

while(response.equals("1")); 
3

response == "1"不是如何在Java中比較String秒。

你實際上想用response.equals("1")代替,它會比較兩個String之間的內容,它們彼此之間並沒有內存位置。

+0

謝謝sooooo了!它終於有效! – user2284562