我无法让我的if语句正确循环

时间:2016-03-01 02:27:57

标签: java if-statement

我试图提示用户他们输入的信息是否正确,程序是否正在进行而不是重新提示用户。我想我需要改变循环方式做某种类型的do-while循环,但我不知道该怎么做。

{

    //Here we will set up our method of data entry and decimal format
    Scanner keyboard = new Scanner(System.in);
    DecimalFormat eNotation1 = new DecimalFormat("00.00");

    //Let's introduce our program to the user
    System.out.println("Hi, This is a program to calculate quarterly interest growth");

    //Here we will prompt the user to enter the amount of quarters
    System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
    int quarterNumber = keyboard.nextInt();
    if (quarterNumber > 10 || quarterNumber < 0)
    {
    System.out.println("You entered a number outside of the boundrys. Please run the program again and choose a number between 0 and 10 (number can include 10)");
    System.exit(0);
    }

    //Here we will prompt the user for the starting balance.
    System.out.println("What is the starting balance of this account?");
    double startingBalance = keyboard.nextDouble();

    //Here we will prompt the user for the interest rate
    System.out.println("What is the interest rate (in percent) in your area? Please pick a number from 1 - 20.");
    double interestRate = keyboard.nextDouble();
    if (interestRate < 1 || interestRate > 20)
    {
    System.out.println("You entered a number outside of the boundrys. Please run the program again and choose a number between 1 and 20 (can include 1 and 20)");
    System.exit(0);
    }

    //Here we will double check with the user that the info is correct.
    System.out.println("The amount of quarters you would like displayed is " + quarterNumber);
    System.out.println("The starting balance you would like to work with is " + startingBalance);
    System.out.println("The interest rate in your area is " + interestRate);
    System.out.println("Is this information correct?");
    keyboard.nextLine();
    String answer = keyboard.nextLine();
    System.out.println("");

    //We will have them enter the information again if the answer is no or No
    if (answer == "no" || answer == "No")
    {
    //Here we will prompt the user to enter the amount of quarters
    System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
    keyboard.nextInt();
    quarterNumber = keyboard.nextInt();

    //Here we will prompt the user for the starting balance.
    System.out.println("What is the starting balance of this account?");
    keyboard.nextDouble();
    startingBalance = keyboard.nextDouble();

    //Here we will prompt the user for the interest rate
    System.out.println("What is the interest rate (in percent) in your area? Please pick a number from 1 - 20.");
    keyboard.nextDouble();
    interestRate = keyboard.nextDouble();
    }

    //Next we will proceed with the calculation if the information is indeed correct
    double endingBalance = startingBalance + (startingBalance * (interestRate / 100 * .25));
    double interestEarned = endingBalance - startingBalance;

    //Now we will output the information
    for (int qn = 1; qn < (quarterNumber + 1); qn++ , startingBalance = startingBalance + interestEarned , endingBalance =startingBalance + (startingBalance * (interestRate / 100 * .25)), interestEarned = endingBalance - startingBalance )
    {
    System.out.println("Quarter Number                      Starting Balance                      Interest Earned                           Ending Balance   ");
    System.out.println(qn + "                                   " + eNotation1.format(startingBalance) + "                                " + eNotation1.format(interestEarned) + "                                      " + eNotation1.format(endingBalance) + "   ");                
    }   


 }

}

3 个答案:

答案 0 :(得分:0)

我认为您应该将您的密码附在Else声明中,以避免无意中进行。请记住,else会限制代码的运行时间并将其放在更具指定的路径上。

答案 1 :(得分:0)

我可能知道你的问题是什么。

示例代码段:(注意我没有实际的java编辑器,所以只需处理任何缺少的语法

 //Here we will prompt the user to enter the amount of quarters
int quarters = 0;
System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
do
{
quarters = keyboard.nextInt();
if (quarters <= 0 || quarters >= 10)
{
System.out.println("You entered a number outside of the boundrys. Please Type a number greater than 0 and less than equal to 10 again)");
}
}
while (quarters <= 0 || quarters >= 10)

这个想法是一个递归提示。应用程序首先提示用户键入结果,然后处理数量,因此您必须在do-while循环中处理提示。假设你不想重复相同的旧文本,你可以做我做的事情(我认为这更加清晰)。

编辑:假设您想要处理应用程序的整个重复。将变量声明的所有内容放入一个方法中(或更好的各种方法,以便更清洁地处理,并处理类似的事情。

int quarters;
double balance;
//TODO : Add all other required variables
string verification;

do
{
quarters = getNumberOfQuartersFromUser();
balance = getStartingBalance();
... //TODO : all the other methods
//verification
System.out.println("Is this accepted?");
verification = keyboard.nextString();
//Handle verfication if it is no or No
if (verification == null || verification == "No" || verification == "no")
{
System.out.println("Starting from the beginning again");
}
}
while (verification == null || verification == "No" || verification == "no")
//out of loop, so handle calculation.
...

和方法片段

private int getNumberOfQuartersFromUser()
{
int quarters = 0;
System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
do
{
quarters = keyboard.nextInt();
if (quarters <= 0 || quarters >= 10)
{
System.out.println("You entered a number outside of the boundrys. Please Type a number greater than 0 and less than equal to 10 again)");
}
}
while (quarters <= 0 || quarters >= 10)
return quarters;
}

答案 2 :(得分:0)

{

//Here we will set up our method of data entry and decimal format
Scanner keyboard = new Scanner(System.in);
DecimalFormat eNotation1 = new DecimalFormat("00.00");

//Let's introduce our program to the user
System.out.println("Hi, This is a program to calculate quarterly interest growth");

//Here we will prompt the user to enter the amount of quarters
System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
int quarterNumber = keyboard.nextInt();

//Here you had started your condition I'll try to make it nested if statement
if (quarterNumber > 10 || quarterNumber < 0)
//if the condition is true perform this
{
System.out.println("You entered a number outside of the boundrys. Please run the program again and choose a number between 0 and 10 (number can include 10)");
System.exit(0);
}
else{
//if false conditions inside else statement should be performed

//Here we will prompt the user for the starting balance.
System.out.println("What is the starting balance of this account?");
double startingBalance = keyboard.nextDouble();

//Here we will prompt the user for the interest rate
System.out.println("What is the interest rate (in percent) in your area? Please pick a number from 1 - 20.");
double interestRate = keyboard.nextDouble();

//you could put another if statement inside if statement which is known as nested if statement.
if (interestRate < 1 || interestRate > 20)
{
System.out.println("You entered a number outside of the boundrys. Please run the program again and choose a number between 1 and 20 (can include 1 and 20)");
System.exit(0);
}
else{
//like before you should enclose this to else to avoid confusion

//Here we will double check with the user that the info is correct.
System.out.println("The amount of quarters you would like displayed is " + quarterNumber);
System.out.println("The starting balance you would like to work with is " + startingBalance);
System.out.println("The interest rate in your area is " + interestRate);
System.out.println("Is this information correct?");
keyboard.nextLine();
String answer = keyboard.nextLine();
System.out.println("");

//We will have them enter the information again if the answer is no or No
if (answer == "no" || answer == "No")
{
//Here we will prompt the user to enter the amount of quarters
System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
keyboard.nextInt();
quarterNumber = keyboard.nextInt();

//Here we will prompt the user for the starting balance.
System.out.println("What is the starting balance of this account?");
keyboard.nextDouble();
startingBalance = keyboard.nextDouble();

//Here we will prompt the user for the interest rate
System.out.println("What is the interest rate (in percent) in your area? Please pick a number from 1 - 20.");
keyboard.nextDouble();
interestRate = keyboard.nextDouble();
}

//Next we will proceed with the calculation if the information is indeed correct
double endingBalance = startingBalance + (startingBalance * (interestRate / 100 * .25));
double interestEarned = endingBalance - startingBalance;

//Now we will output the information
for (int qn = 1; qn < (quarterNumber + 1); qn++ , startingBalance = startingBalance + interestEarned , endingBalance =startingBalance + (startingBalance * (interestRate / 100 * .25)), interestEarned = endingBalance - startingBalance )
{
System.out.println("Quarter Number                      Starting Balance                      Interest Earned                           Ending Balance   ");
System.out.println(qn + "                                   " + eNotation1.format(startingBalance) + "                                " + eNotation1.format(interestEarned) + "                                      " + eNotation1.format(endingBalance) + "   ");                
}   
}
}
}

请记住,学习嵌套if语句对程序员来说是必不可少的。没有这个,你的所有条件都将继续下去。

相关问题