do-while循环不继续

时间:2012-08-23 16:18:59

标签: java loops do-while

我遇到以下代码的问题。这一切都运行正常,除非我希望程序重新启动,如果用户在结尾输入“Y”,并在按下任何其他内容时结束。

但是,每当我在“重新启动计算器”提示符下键入任何内容时,无论我输入“Y”还是“N”,它都将停止运行。使用Y / N进行验证在这里并不太重要,我只是希望它在键入Y时重新启动,如果输入任何其他内容则结束。

为noob代码道歉,这里是Java初学者。

import java.util.Scanner;
import java.text.*;

public class Savings {

public static void main(String[] args)
{

//Imports scanner, to read user's input
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);

do {
    //Asks for and receives user's initial deposit
    int initial_Deposit;
    do {
        System.out.print("Enter initial deposit in dollars (Between $1 - $50000: ");
        while (!scan.hasNextInt()) {
            System.out.println("Please enter a valid number between '1-50000'");
            scan.next();
        }
        initial_Deposit = scan.nextInt();
    } while (initial_Deposit <= 0 || initial_Deposit >= 50001);

    //Asks for and receives user's interest rate
    double interest_Rate;
    do {
        System.out.print("Enter interest rate as a percentage between '0.1-100.0' (e.g. 4.0):");
        while (!scan.hasNextDouble()) {
            System.out.println("Enter interest rate as a percentage between '0.1-100.0' (e.g. 4.0):");
            scan.next();
        }
        interest_Rate = scan.nextDouble();
    } while (interest_Rate <= 0.0 || interest_Rate >= 100.1);

    //Asks for and receives user's monthly deposit
    int monthly_Deposit;
    do {
        System.out.print("Enter monthly deposit in dollars between '$1 - $5000: ");
        while (!scan.hasNextDouble()) {
            System.out.println("Enter monthly deposit in dollars between '$1 - $5000: ");
            scan.next();
        }
        monthly_Deposit = scan.nextInt();
    } while (monthly_Deposit <= 0 || monthly_Deposit >= 5001);

    //Asks for and receives user's investment duration  
    int monthly_Duration;
    do {
        System.out.print("Enter investment duration (Between 1 and 12): ");
        while (!scan.hasNextDouble()) {
            System.out.println("Enter investment duration (Between 1 and 12): ");
            scan.next();
        }
        monthly_Duration = scan.nextInt();
    } while (monthly_Duration <= 0 || monthly_Duration >= 13);

    //Asks for and receives user's first name
    String first_Name;
    System.out.print("Enter first name: ");
    first_Name = input.next();

    //Asks for and receives user's surname
    String last_Name;
    System.out.print("Enter surname: ");
    last_Name = input.next();

    //Formats first name to only first letter
    char firstLetter = first_Name.charAt(0);

    //Changes name to correct format
    String formatted_Name;
    formatted_Name = "Savings growth over the next six months for " + last_Name + ", " + firstLetter;
    System.out.println(formatted_Name);

    //Calculates the first balance
    double balanceCurrent;
    balanceCurrent = initial_Deposit + monthly_Deposit;

    //Prepares to format currency
    DecimalFormat df = new DecimalFormat("#.##");

    //Defining variables
    double balanceNew;
    double interestEarned;

    //Defining counter for while loop
    int counter;
    counter = monthly_Duration;
    int month_Counter;
    month_Counter = 1;

    //While loop to calculate savings
    while (counter > 0) {
        balanceNew = balanceCurrent + (balanceCurrent *((interest_Rate /12)/100));
        interestEarned = balanceCurrent *((interest_Rate /12)/100);
        balanceCurrent = balanceNew + monthly_Deposit;
        System.out.println("Balance after month " + month_Counter + ": $" + df.format((balanceNew)));
        System.out.println("Interest earned for this month: $" + df.format(interestEarned));
        counter = counter - 1;
        month_Counter = month_Counter + 1;

    }
    //Formats data into a table
    balanceCurrent = initial_Deposit + monthly_Deposit; 
    counter = monthly_Duration;
    int month;
    month = 0;
    String dollarSign = "$";

    String stringHeadingOne = "Month";
    String stringHeadingTwo = "New Balance";
    String stringHeadingThree = "Interest Earned";
    String dividerOne = "-----     -----------     ---------------";
    System.out.println("");
    System.out.printf("%-9s %s %19s \n", stringHeadingOne, stringHeadingTwo, stringHeadingThree);
    System.out.println(dividerOne);

    while (counter > 0) {
        balanceNew = balanceCurrent + (balanceCurrent *((interest_Rate /12)/100));
        interestEarned = balanceCurrent *((interest_Rate /12)/100);
        balanceCurrent = balanceNew + monthly_Deposit;
        month = month + 1;
        System.out.printf("%-11s %s %s %13s %s \n", month, dollarSign, df.format((balanceNew)), dollarSign, df.format(interestEarned));
        counter = counter - 1;
    }
System.out.print("Restart Calculator? Y/N);");
} while (scan.next() == "Y");

} }

4 个答案:

答案 0 :(得分:4)

while (scan.next() == "Y");      // Is checking for reference equality

在Java中进行对象比较时,请使用equals()

while (scan.next().equals("Y"));

或者,正如之前的回答所指出的,您可以将字符与==运算符进行比较

答案 1 :(得分:1)

试试这个:

scan.nextLine().charAt(0) == 'Y'

答案 2 :(得分:1)

在比较字符串或任何其他对象时,您需要使用.equals(Object other)方法。你只能使用== with primatives(boolean,int,double,...)

scan.nextLine().equals("Y");
//or
scan.next().equals("Y");

还有一种将字符串转换为大写字母的方法,允许用户输入“y”或“Y”

scan.next().toUpperCase().equals("Y");

答案 3 :(得分:0)

您应该对字符串使用Equals方法:

while ("Y".equals(scan.next()));