Java新手,需要帮助重新启动while循环吗
在上周左右的时间里,我刚刚开始学习Java,我正在创建一个程序,作为一个计算佣金的销售计算器。 我的代码如下:Java新手,需要帮助重新启动while循环吗,java,if-statement,while-loop,calculator,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 doubl
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循环时,它不会重新开始选择另一个销售人员。我可能做了一些非常糟糕的事情,我的代码格式可能非常糟糕。任何帮助都将不胜感激。去掉=false和分号。所以不是:
while (startLoop = false);
{
System.out.println("foo");
}
这相当于
while (startLoop = false) {
// do nothing
}
{
System.out.println("foo");
}
相反
while (!startLoop) {
// do something here
}
我已经删除了你的javascript标签,因为这个问题不是关于这个完全不同的语言,你不想误导javascript专家,让错误的人来审查你的问题。去掉=false和分号<代码>while(startoop=false)代码>将其更改为
,而(!StartOOP)
投票结束,因为这实际上只是一个印刷错误。为您的4个项目和价格使用数组,这样您就不必反复重复。@D.Leigh不要使用==来比较字符串,而是使用equals()。或者更好,equalsIgnoreCase()。