Java while循环行为
我遇到了一个为我正在学习的Java课程编写的问题,我无法理解为什么它会以某种方式运行。这是本书A部分和B部分的问题: A) 创建一个名为Purchase的类。每次购买都包含发票号、销售金额和销售税金额。包括发票编号和销售金额的设置方法。在销售金额的set()方法中,将销售税计算为销售金额的5%。还包括显示购买详细信息的显示方法。将文件另存为Purchase.java B) 创建一个应用程序,用于声明购买对象并提示用户输入购买详细信息。提示输入发票号时,在输入1000到8000之间的数字之前,不要让用户继续操作。提示输入销售金额时,在用户输入非负数、销售金额和销售税之前,请勿继续。将文件另存为CreatePurchase.java 以下是问题第一部分的代码:Java while循环行为,java,Java,我遇到了一个为我正在学习的Java课程编写的问题,我无法理解为什么它会以某种方式运行。这是本书A部分和B部分的问题: A) 创建一个名为Purchase的类。每次购买都包含发票号、销售金额和销售税金额。包括发票编号和销售金额的设置方法。在销售金额的set()方法中,将销售税计算为销售金额的5%。还包括显示购买详细信息的显示方法。将文件另存为Purchase.java B) 创建一个应用程序,用于声明购买对象并提示用户输入购买详细信息。提示输入发票号时,在输入1000到8000之间的数字之前,不要
public class Purchase
{
int invoiceNumber = 1234;
double salePrice = 10.00;
double SalesTax;
public void setInvoiceNumber(int invoice)
{
invoiceNumber = invoice;
}
public void setSalePrice(double saleAmount)
{
salePrice = saleAmount;
SalesTax = (saleAmount * .05);
}
public void displaySalePrice()
{
System.out.println("Your invoice number is:" + invoiceNumber + ".");
System.out.println("Your sale amount is: " + salePrice + ".");
System.out.println("Your sales tax is: " + SalesTax + ".");
}
}
import java.util.Scanner;
public class CreatePurchase
{
public static void main(String[] args)
{
int invoice;
double saleAmount;
invoice = 0;
saleAmount = 0.0;
Purchase completedPurchase = new Purchase();
Scanner input = new Scanner(System.in);
System.out.println("Please enter the invoice number: ");
invoice = input.nextInt();
System.out.println("Please enter the sale amount: ");
saleAmount = input.nextDouble();
do
{
System.out.println("You entered an invalid number.");
System.out.println("Please enter a number between 1000 and 8000.");
invoice = input.nextInt();
}
while (invoice < 1000 || invoice > 8000);
do
{
System.out.println("You entered an invalid number.");
System.out.println("Please enter a number greater than 0.");
saleAmount = input.nextDouble();
}
while (saleAmount < 0);
completedPurchase.setInvoiceNumber(invoice);
completedPurchase.setSalePrice(saleAmount);
completedPurchase.displaySalePrice();
}
}
下面是问题第二部分的代码:
public class Purchase
{
int invoiceNumber = 1234;
double salePrice = 10.00;
double SalesTax;
public void setInvoiceNumber(int invoice)
{
invoiceNumber = invoice;
}
public void setSalePrice(double saleAmount)
{
salePrice = saleAmount;
SalesTax = (saleAmount * .05);
}
public void displaySalePrice()
{
System.out.println("Your invoice number is:" + invoiceNumber + ".");
System.out.println("Your sale amount is: " + salePrice + ".");
System.out.println("Your sales tax is: " + SalesTax + ".");
}
}
import java.util.Scanner;
public class CreatePurchase
{
public static void main(String[] args)
{
int invoice;
double saleAmount;
invoice = 0;
saleAmount = 0.0;
Purchase completedPurchase = new Purchase();
Scanner input = new Scanner(System.in);
System.out.println("Please enter the invoice number: ");
invoice = input.nextInt();
System.out.println("Please enter the sale amount: ");
saleAmount = input.nextDouble();
do
{
System.out.println("You entered an invalid number.");
System.out.println("Please enter a number between 1000 and 8000.");
invoice = input.nextInt();
}
while (invoice < 1000 || invoice > 8000);
do
{
System.out.println("You entered an invalid number.");
System.out.println("Please enter a number greater than 0.");
saleAmount = input.nextDouble();
}
while (saleAmount < 0);
completedPurchase.setInvoiceNumber(invoice);
completedPurchase.setSalePrice(saleAmount);
completedPurchase.displaySalePrice();
}
}
它仍然不能正常工作。将循环更改为while循环当然有效,但现在当我为发票号输入一个正确范围内的数字,为saleAmount输入一个错误的数字时,程序完成了,并且没有执行saleAmount的while循环?我似乎缺少一个概念
还有,谁能推荐一个比JGrasp更好的IDE呢。这是我们被告知要使用的,但它很麻烦。我有VisualStudio和Eclipse,但我觉得在这两个IDE中做java作业可能有些过头了。我将学习更多的java课程和C++课程,所以也许值得学习VS或Eclipse中的基础知识。如果您有任何建议,我们将不胜感激。这就是如何…当循环工作时:执行一次主体,然后检查条件。使用正常的while循环。这是因为您使用的是
do{}while(…)
而不是while(…){}
do while保证至少运行一次,然后继续循环
如果初始条件为false,则while将永远不会运行。在
do while
循环中,在测试条件之前执行主体。您可能希望使用while
循环,其中主体在测试条件后执行
我强烈建议您阅读与Sun的不同之处。将循环更改为
,而s
while (invoice < 1000 || invoice > 8000)
{
...
}
while(发票<1000 | |发票>8000)
{
...
}
当我编译CreatPurchase.java并运行它时,它是可以工作的,但在工作之前必须先循环使用循环
这是因为您正在使用do…while
循环。do…while
循环至少执行一次,并在执行循环体后检查条件
在这种情况下,您需要的是一个简单的while
循环,以便在执行循环体之前检查条件。即:
while (invoice < 1000 || invoice > 8000)
{
...
}
while(发票<1000 | |发票>8000)
{
...
}
这是因为do
块总是执行至少一次。您应该使用while
循环:
while (invoice < 1000 || invoice > 8000)
{
System.out.println("You entered an invalid number.");
System.out.println("Please enter a number between 1000 and 8000.");
invoice = input.nextInt();
}
while(发票<1000 | |发票>8000)
{
System.out.println(“您输入的数字无效”);
System.out.println(“请输入一个介于1000和8000之间的数字”);
发票=输入。nextInt();
}
这样,您只需要请求另一个数字,如果发票号不在您定义的范围内。Java中有几个循环函数,一个是do while循环,它总是执行一次,如果while部分为true,它会再次循环
另一个更好的例子是while循环,它的工作原理相同,但在第一个循环之前检查while部分
检查您需要一个while(condition){…}
循环。这样,只有当条件为true时,才会执行循环中的代码。在代码中有一个do…while(condition)
循环,循环的内部将始终至少执行一次 import java.util.Scanner;
import java.util.Scanner;
public class CreatePurchase {
public static void main(String[] args) {
int invoice;
double amount;
Purchase sale1 = new Purchase();
System.out.println("Enter invoice number between 1000 and 8000 >>");
Scanner input = new Scanner(System.in);
invoice = input.nextInt();
while (invoice < 1000 || invoice > 8000) {
System.out.println("You entered a wrong invoice number");
System.out.println("Enter invoice number between 1000 and 8000 >>");
invoice = input.nextInt();
}
System.out.println("Enter sales amount >>");
amount = input.nextDouble();
while (amount < 0) {
System.out.println("Enter number greater than 0 ");
System.out.println("Enter sales amount >>");
amount = input.nextDouble();
}
sale1.setInvoiceNumber(invoice);
sale1.setAmount(amount);
sale1.display();
}
}
公共类购买{
公共静态void main(字符串[]args){
国际发票;
双倍金额;
Purchase sale1=新购买();
System.out.println(“输入介于1000和8000之间的发票号>>”;
扫描仪输入=新扫描仪(System.in);
发票=输入。nextInt();
而(发票<1000 | |发票>8000){
System.out.println(“您输入了错误的发票号”);
System.out.println(“输入介于1000和8000之间的发票号>>”;
发票=输入。nextInt();
}
System.out.println(“输入销售额>>”;
amount=input.nextDouble();
而(金额<0){
System.out.println(“输入大于0的数字”);
System.out.println(“输入销售额>>”;
amount=input.nextDouble();
}
sale1.设置发票编号(发票);
销售1.设定金额(金额);
sale1.display();
}
}
Hint:有两种类型的while循环使用do…while执行至少一次进入do{}的范围(“执行此操作,并在下一个循环中考虑此条件”)。还要确保您知道| |(条件或)运算符在做什么。它在示例输入中按预期工作,但这只是因为第一个表达式的计算结果为false。使用while循环而不是do while循环的所有建议都是准确的。我将循环从do while更改为while循环。这立即纠正了问题。我还纠正了一些小错误,程序运行正常。谢谢大家的洞察力和帮助。没有一点总结/解释的答案是什么?