Java 编写循环/while循环?
我在高中的一个编程课上,我被分配了一个任务,做一个基本的小计和顶级计算器,但我在一家餐馆工作,所以做一个只允许你读一种食物的计算器似乎有点毫无意义。所以我试着让它能够接受多种食物,并将它们添加到一个价格变量中。很抱歉,如果此代码中的某些代码看起来效率低下或冗余。当然,这只是高中 问题是,当我运行它时,它会询问用户是否还想添加其他食物,当我输入“是”或“否”时,程序不会执行任何操作。继续跑,但不走得更远。有什么解释吗Java 编写循环/while循环?,java,loops,for-loop,while-loop,Java,Loops,For Loop,While Loop,我在高中的一个编程课上,我被分配了一个任务,做一个基本的小计和顶级计算器,但我在一家餐馆工作,所以做一个只允许你读一种食物的计算器似乎有点毫无意义。所以我试着让它能够接受多种食物,并将它们添加到一个价格变量中。很抱歉,如果此代码中的某些代码看起来效率低下或冗余。当然,这只是高中 问题是,当我运行它时,它会询问用户是否还想添加其他食物,当我输入“是”或“否”时,程序不会执行任何操作。继续跑,但不走得更远。有什么解释吗 import java.text.NumberFormat; import ja
import java.text.NumberFormat;
import java.util.Scanner;
public class Price {
/**
* @param args
*/
public static void main(String[] args) {
final double taxRate = .0887; //8.87% Tax Rate
double tipRate;
int quantity1;
Scanner kb = new Scanner(System.in);
double subtotal, tax, tip, totalCost1, unitPrice1 = 0;
String done;
System.out.println ("How many of the first item did you get?: ");
quantity1 = kb.nextInt();
for (int i = 0; i < quantity1; i++)
{
System.out.println ("What was the price of that single item "+(i+1) + ": ");
unitPrice1 = kb.nextDouble();
System.out.println ("Was there another food item you'd like to add?: ");
done=kb.next();
while (done.equalsIgnoreCase("Yes"));
}
System.out.println ("What percent would you like to tip? (Formatted like 0.10 for 10%, 0.20 for 20%, etc.): ");
tipRate = kb.nextDouble();
subtotal= quantity1 * unitPrice1;
tax = subtotal * taxRate;
totalCost1 = subtotal + tax;
tip = totalCost1 * tipRate;
totalCost1 = totalCost1 + tip;
//Formatting
NumberFormat money = NumberFormat.getCurrencyInstance();
NumberFormat tipMoney = NumberFormat.getCurrencyInstance();
NumberFormat taxPercent = NumberFormat.getPercentInstance();
NumberFormat tipPercent = NumberFormat.getPercentInstance();
System.out.println ("Your total before tax is: " + money.format(subtotal));
System.out.println ("The tax is " + money.format(tax) + " at " + tipPercent.format(taxRate));
System.out.println ("The tip at " + tipPercent.format(tipRate) + " is " + tipMoney.format(tip));
}
}
导入java.text.NumberFormat;
导入java.util.Scanner;
公共类价格{
/**
*@param args
*/
公共静态void main(字符串[]args){
最终双重税率=.0887;//8.87%税率
双蒂普拉特;
int数量1;
扫描仪kb=新扫描仪(System.in);
双倍小计、税金、小费、总成本1、单价1=0;
字符串完成;
System.out.println(“您得到的第一个项目有多少?:”;
quantity1=kb.nextInt();
对于(int i=0;i
这里有一个无限循环:
while (done.equalsIgnoreCase("Yes"));
一旦输入Yes
,它将一直坐在那里,什么也不做,因为done
的值是Yes
,并且永远不会更改
你的循环结构也有点奇怪。您的外部for
循环的运行次数与第一项的数量相同。但你不应该只把这个数字乘以成本吗?因为您运行循环的时间与用户输入的项目数相同(通过预先询问),或者您不询问他们项目总数,只要让他们输入Yes
,如果他们想添加更多项目;你不可能两个都做
您的循环可能如下所示:
String input = "Yes";
while(input.equalsIgnoreCase("Yes")) {
System.out.println ("How many of the first item did you get? ");
quantity1 = kb.nextInt();
System.out.println ("What was the price of that single item? ");
unitPrice1 = kb.nextDouble();
//total += unitPrice1 * quantity1 - you don't have this in your code, but this is where you would be calculating the running total
System.out.println("Was there another food item you'd like to add? ");
input = kb.next();
}
这里有一个无限循环:
while (done.equalsIgnoreCase("Yes"));
一旦输入Yes
,它将一直坐在那里,什么也不做,因为done
的值是Yes
,并且永远不会更改
你的循环结构也有点奇怪。您的外部for
循环的运行次数与第一项的数量相同。但你不应该只把这个数字乘以成本吗?因为您运行循环的时间与用户输入的项目数相同(通过预先询问),或者您不询问他们项目总数,只要让他们输入Yes
,如果他们想添加更多项目;你不可能两个都做
您的循环可能如下所示:
String input = "Yes";
while(input.equalsIgnoreCase("Yes")) {
System.out.println ("How many of the first item did you get? ");
quantity1 = kb.nextInt();
System.out.println ("What was the price of that single item? ");
unitPrice1 = kb.nextDouble();
//total += unitPrice1 * quantity1 - you don't have this in your code, but this is where you would be calculating the running total
System.out.println("Was there another food item you'd like to add? ");
input = kb.next();
}
这里有一个无限循环:
while (done.equalsIgnoreCase("Yes"));
一旦输入Yes
,它将一直坐在那里,什么也不做,因为done
的值是Yes
,并且永远不会更改
你的循环结构也有点奇怪。您的外部for
循环的运行次数与第一项的数量相同。但你不应该只把这个数字乘以成本吗?因为您运行循环的时间与用户输入的项目数相同(通过预先询问),或者您不询问他们项目总数,只要让他们输入Yes
,如果他们想添加更多项目;你不可能两个都做
您的循环可能如下所示:
String input = "Yes";
while(input.equalsIgnoreCase("Yes")) {
System.out.println ("How many of the first item did you get? ");
quantity1 = kb.nextInt();
System.out.println ("What was the price of that single item? ");
unitPrice1 = kb.nextDouble();
//total += unitPrice1 * quantity1 - you don't have this in your code, but this is where you would be calculating the running total
System.out.println("Was there another food item you'd like to add? ");
input = kb.next();
}
这里有一个无限循环:
while (done.equalsIgnoreCase("Yes"));
一旦输入Yes
,它将一直坐在那里,什么也不做,因为done
的值是Yes
,并且永远不会更改
你的循环结构也有点奇怪。您的外部for
循环的运行次数与第一项的数量相同。但你不应该只把这个数字乘以成本吗?因为您运行循环的时间与用户输入的项目数相同(通过预先询问),或者您不询问他们项目总数,只要让他们输入Yes
,如果他们想添加更多项目;你不可能两个都做
您的循环可能如下所示:
String input = "Yes";
while(input.equalsIgnoreCase("Yes")) {
System.out.println ("How many of the first item did you get? ");
quantity1 = kb.nextInt();
System.out.println ("What was the price of that single item? ");
unitPrice1 = kb.nextDouble();
//total += unitPrice1 * quantity1 - you don't have this in your code, but this is where you would be calculating the running total
System.out.println("Was there another food item you'd like to add? ");
input = kb.next();
}
当用户输入yes时,您需要退出for循环,所以您可以在此处使用标签,如下所示:
outerloop:
for (int i = 0; i < quantity1; i++)
{
System.out.println ("What was the price of that single item "+(i+1) + ": ");
unitPrice1 = kb.nextDouble();
System.out.println ("Was there another food item you'd like to add?: ");
done=kb.next();
while (done.equalsIgnoreCase("Yes")){
break outerloop;
}
}
outerloop:
对于(int i=0;i
您当前的代码在while中不起任何作用