java while循环卡住了

时间:2016-09-28 03:09:19

标签: java while-loop

所以我有一个学校的课程项目,我必须创建一个银行帐户程序,一切顺利运行,直到我点击while循环,然后它陷入无限循环,没有执行任何东西,请帮助,代码是以下

import java.util.Scanner;


public class Blueprint_ET
{
  //initialize all variables in the object
  private double balance;
  private double interestRate;
  private double years;
  private String name;
  private int age;
  private int validAmount;
  private double initialBalance;

  //construct the objects with the following variables
  public Blueprint_ET(double balance, String name, int age){
    years=0;
    interestRate=.02;
    this.balance=balance;
    this.name = name;
    this.age= age;
    initialBalance = balance;
  }

  //deposit method
  public void deposit(double depositAmt){
    balance+=depositAmt;
  }

  //withdraw method
  public void withdraw(double withdrawAmt){
    balance-=withdrawAmt;
  }

  //method used to set the account years and add the interest accordingly
  public void setYearsAndAddInterest(double years){
    this.years=years;
    double interestEarned = (interestRate * years) * balance;
    balance += interestEarned;
  }

  //method used to get the name so that it can be printed out later on
  public String getName(){
    return name;
  }

  //method used to get the initial balance so that it can be printed out
  public double getStarting(){
    return initialBalance;
  }

  //method used to get the years so that it can be printed out
  public double getYears(){
    return years;
  }

  //method used to get the final balance once the account it closed
  public double getFinal(){
    return balance;
  }

  //method used to get the age of the person
  public int getAge(){
    return age;
  }
  public static void main(String[] args){

    //create "kboard" scanner
    Scanner kboard = new Scanner(System.in);

    //ask the user to enter the persons first name
    System.out.print("Enter the account owners first name here: ");
    String firstName = kboard.nextLine();

    //ask the user to enter the persons last name
    System.out.print("Enter the account owners last name here: ");
    String lastName = kboard.nextLine();

    //puts together the first and last name into one variable
    String fullName = firstName + " " + lastName;

    //asks the user for the starting balance of the account
    System.out.print("Enter the starting balance of the account here: ");
    double balance = kboard.nextDouble();

    //asks the user for the age of the person
    System.out.print("Enter the age of the person here: ");
    int age = kboard.nextInt();

    //initialize variables that will be used in the while loop
    String option;
    int exitNum=0;
    double years=0;
    double deposit=0;
    double withdraw=0;

    //Create account object
    Blueprint_ET account1 = new Blueprint_ET(balance, fullName, age);

    //start while loop
    while (exitNum < 20 ){

      //prompts the user to enter what option they want to do with according codes
      System.out.print("To widthdraw, type wd, to deposit, type d, to change the number of years that the account has been open and add the according interest, type y, to close the account, type c.");
      option = kboard.nextLine();

      //if statement for entering the amount of money withdrawn if the option selected is the code for wd
      if (option == "wd") {
        System.out.print("Enter the amount you want to withdraw from the account: ");
        withdraw = kboard.nextDouble();
        account1.withdraw(withdraw);
      }

      //if statement for entering the years the account has been open and sets the years and adds the interest rate into the balance
      else if (option == "y") {
        System.out.print("Enter the years the person has had the account open: ");
        years = kboard.nextDouble();
        account1.setYearsAndAddInterest(years);
      }

      //if statement for entering the amount of money to deposit, adds it to balance
     else if (option == "d") {
        System.out.print("Enter the amount you want to deposit: ");
        deposit = kboard.nextDouble();
        account1.deposit(deposit);
      }

      //sets the exitNum to 21 so that it can exit the while loop
      else if (option == "e") {
        exitNum = 21;
      }

    } 
    //prints out all data once the account has been closed
    System.out.println(account1.getName()+"'s is "+account1.getAge()+" had an account that had an intial balance of "+account1.getStarting()+". This account was open for "+account1.getYears()+" years. Also, when the account was closed, they had a balance of "+account1.getFinal());
  }
}

4 个答案:

答案 0 :(得分:3)

考虑一下你的代码:

//sets the exitNum to 21 so that it can exit the while loop
else if (option == "e") {
    exitNum = 21;
}

请注意,要比较 2种方式

  1. 使用等于 ==运算符。
  2. 使用str.equals(str2)方法。
    1. 使用等于 ==运算符。

      ==测试引用相等性。我们假设你有一个String str1 = "Hello World"。现在这个str1 String对象占用了一些 记忆中的空间。现在让我们假设您创建另一个完全相同的 String str2 = "Hello World"现在还不知道是否这些 两个不同的Hello World String对象将返回true或 与false 相比时str1 == str2。他们可能会返回true if 这两个对象都指向内存中的相同Object。 但是,如果它们引用不同的内存位置,也可能返回false

      底线是==运算符测试参考。它没有 对于这个操作员来说,关于物体里面有什么东西。

    2. 使用str.equals(str2)方法。

      str.equals(str2)实际上会检查内部的内容 字符串对象。这种方法是否兼而有之 对象指的是相同或不同的内存位置。它只是 考虑字符串对象的内部材料。

    3. 因此,在比较字符串对象时,应始终使用.equals()方法。所以你的方法就变成了:

      //sets the exitNum to 21 so that it can exit the while loop
      else if (option.equals("e")) {
          exitNum = 21;
      }
      

答案 1 :(得分:1)

您必须使用a.equals("abc")将一个字符串与另一个字符串进行比较。您必须知道equals()==之间的区别。我已经运行了你的代码,这是问题,我想。

答案 2 :(得分:0)

您使用了==比较器,它对两个字符串执行参考检查。由于引用检查永远不会是true,因此它将以无限循环结束,跳过所有if else语句

由于您要比较字符串中的内容,请改用equals()

您可以查看此answer以获取详细说明。

答案 3 :(得分:0)

您应始终使用equals()方法比较两个字符串。它实际上比较了String对象存储的表达式。对于引用类型,运算符==比较内存中两个对象的地址

相关问题