1

以下のコードに問題があります。ユーザーが最後に「Y」を入力した場合にプログラムを再起動し、他の何かが押された場合に終了することを除いて、すべて正常に動作しています。

ただし、「電卓の再起動」プロンプトで何かを入力すると、「Y」または「N」のどちらを入力しても、実行が停止します。ここでは、Y/N による検証はそれほど重要ではありません。Y が入力された場合は再開し、それ以外のものが入力された場合は終了するようにしたいだけです。

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

4 に答える 4

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

Javaでオブジェクト比較を行う場合は、equals()

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

または、前の回答で指摘したように、文字を==演算子と比較できます

于 2012-08-23T16:27:29.023 に答える
1

文字列やその他のオブジェクトを比較する場合は、.equals(Object other)メソッドを使用する必要があります。==はプリミティブ(boolean、int、double、...)でのみ使用できます。

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

ユーザーが「y」または「Y」を入力できるように文字列を大文字にする方法もあります

scan.next().toUpperCase().equals("Y");
于 2012-08-23T16:30:04.623 に答える
1

これを試して:

scan.nextLine().charAt(0) == 'Y'
于 2012-08-23T16:24:11.400 に答える
0

文字列にはEqualsメソッドを使用する必要があります。

while ("Y".equals(scan.next()));
于 2012-08-23T16:28:12.810 に答える