全新的Java,需要一些帮助重启while循环

时间:2016-01-26 01:32:16

标签: java if-statement while-loop calculator

我刚开始在上周左右开始学习Java,而且我正在创建一个程序,作为计算佣金的销售计算器。 我的代码如下:

import java.util.Scanner;

public class Application {

int itemOne;
int itemTwo;
int itemThree;
int itemFour;

final double baseCommission = 200;

final double itemOnePrice = 239.99;
final double itemTwoPrice = 129.75;
final double itemThreePrice = 99.95;
final double itemFourPrice = 350.89;


final double commissionPercentage = 0.09;

boolean startLoop = false;

public void start(){

    while (startLoop = false);
    {
        //Welcome message
        System.out.println("Welcome to Devon's Sales Calculator!");

        //Defining new scanner
        Scanner user_input = new Scanner(System.in);

        //Getting user input for salesperson name along with number of items sold as well as assigning them to a variable
        String salesman_name;
        System.out.print("Insert salesperson name:\t");
        salesman_name = user_input.next();

        System.out.print("Enter number of first item sold:\t");
        int first_item = user_input.nextInt();

        System.out.print("Enter number of second item sold:\t");
        int second_item = user_input.nextInt();

        System.out.print("Enter number of third item sold:\t");
        int third_item = user_input.nextInt();

        System.out.print("Enter number of fourth item sold:\t");
        int fourth_item = user_input.nextInt();


        //Printing out the name of the salesmen, followed by the total price of items sold
        System.out.println("Sales Person\t" + salesman_name);

        System.out.println("Total price of first item sold\t" + first_item * itemOnePrice);

        System.out.println("Total price of second item sold\t" + second_item * itemTwoPrice);

        System.out.println("Total price of third item sold\t" + third_item * itemThreePrice);

        System.out.println("Total price of fourth item sold\t" + fourth_item * itemFourPrice);

        //Calculating total of all items sold
        double finalPrice = first_item * itemOnePrice + second_item * itemTwoPrice + third_item * itemThreePrice + fourth_item * itemFourPrice;

        //Calculating commission @ 0,9%
        System.out.println("Total commission earned\t" + finalPrice * commissionPercentage);

        //Decision whether or not to restart the while loop 
        String decision;
        System.out.println("Would you like to check another salesperson?");
        decision = user_input.next();

        if(decision == "yes"){
            startLoop = false;
        }

        else if(decision == "no"){
            startLoop = true;
            }

        }

     }
}

每当我执行while循环时,它都不会重新启动以选择另一个推销员。我可能做了一些可怕的错误,我的代码格式化可能很糟糕。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

摆脱= false和分号。所以不是:

while (startLoop = false);
{
   System.out.println("foo");
}

相当于

while (startLoop = false) {
    // do nothing
}

{
   System.out.println("foo");
}

相反,

while (!startLoop) {
    // do something here
}