首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >do-while循环不再继续

do-while循环不再继续
EN

Stack Overflow用户
提问于 2012-08-24 00:18:59
回答 4查看 995关注 0票数 1

我对下面的代码有一个问题。这一切都很好,除了我希望程序重新启动时,如果用户在结尾键入"Y“,并结束任何其他按下。

但是,无论我输入"Y“还是"N”,只要我在"Restart Calculator“提示符下输入任何内容,它就会停止运行。在这里,Y/N的验证并不太重要,我只想在键入Y时重新启动,如果键入其他内容则结束。

对于新手代码,Java初学者,请在这里道歉。

代码语言:javascript
复制
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");

}}

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-08-24 00:27:29

代码语言:javascript
复制
while (scan.next() == "Y");      // Is checking for reference equality

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

代码语言:javascript
复制
while (scan.next().equals("Y"));

或者,正如前面的答案所指出的,您可以使用==运算符比较字符

票数 4
EN

Stack Overflow用户

发布于 2012-08-24 00:24:11

试试这个:

代码语言:javascript
复制
scan.nextLine().charAt(0) == 'Y'
票数 1
EN

Stack Overflow用户

发布于 2012-08-24 00:30:05

在比较String或任何其他对象时,您需要使用.equals(Object other)方法。您只能将==与主语(布尔值、整型、双精度、...)一起使用

代码语言:javascript
复制
scan.nextLine().equals("Y");
//or
scan.next().equals("Y");

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

代码语言:javascript
复制
scan.next().toUpperCase().equals("Y");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12096033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档