Java中的do-while循环有什么问题
我正在编写这个程序,不明白为什么循环不会退出并输出Java中的do-while循环有什么问题,java,do-while,Java,Do While,我正在编写这个程序,不明白为什么循环不会退出并输出System.out.print()。有人能看一下并告诉我发生了什么事吗 public class SalesTax { public static void main(String[] args) { // Input items for shopping cart HashMap<String, Double> cart = new HashMap<String,
System.out.print()
。有人能看一下并告诉我发生了什么事吗
public class SalesTax
{
public static void main(String[] args)
{
// Input items for shopping cart
HashMap<String, Double> cart = new HashMap<String, Double>();
// Create a Scanner
Scanner input = new Scanner(System.in).useLocale(Locale.US);
//variables
String item;
double price;
boolean done = false;
String ans;
do
{
System.out.print("Enter the item then select enter followed by the price and enter.");
item = input.nextLine();
price = input.nextDouble();
cart.put(item, price);
System.out.print("If done type done if not continue with adding to cart.");
ans = input.nextLine();
if(ans.equals("Done"))
done = true;
else
item = input.nextLine();
price = input.nextDouble();
cart.put(item, price);
System.out.print("If done type done if not continue with adding to cart.");
} while( !done);
System.out.print("Yo");
}
}
公共类销售税
{
公共静态void main(字符串[]args)
{
//为购物车输入项目
HashMap购物车=新建HashMap();
//创建扫描仪
扫描仪输入=新扫描仪(System.in).useLocale(Locale.US);
//变数
字符串项;
双倍价格;
布尔完成=假;
字符串ans;
做
{
System.out.print(“输入项目,然后选择输入,然后选择价格和输入”);
item=input.nextLine();
price=input.nextDouble();
购物车(商品、价格);
System.out.print(“如果完成,则键入完成,如果不继续添加到购物车。”);
ans=input.nextLine();
如果(ans.equals(“Done”))
完成=正确;
其他的
item=input.nextLine();
price=input.nextDouble();
购物车(商品、价格);
System.out.print(“如果完成,则键入完成,如果不继续添加到购物车。”);
}而(!完成);
系统输出打印(“Yo”);
}
}
问题在于else
块周围缺少大括号:虽然块缩进正确,但缩进并不重要:只有第一行
item = input.nextLine();
被视为其他
的一部分;其余的都是无条件执行的
请注意,只有一个地方可以将done
设置为true
,您可以将do
/while
循环替换为“永久”循环,并使用break
从中间退出循环:
while (true) {
...
if (ans.equals("Done")) {
break;
}
...
}
这使得声明
done
变量变得不必要。问题在于else
块周围缺少大括号:尽管块已正确缩进,但缩进并不重要:只有第一行
item = input.nextLine();
public static void main(String[] args)
{
//Input items for shopping cart
HashMap<String, Double> cart = new HashMap<String, Double>();
//Create a Scanner
Scanner input = new Scanner(System.in).useLocale(Locale.US);
//variables
String item = "";
double price;
boolean done = false;
String ans = ""; //Initialize to empty, just my common practice.
do
{
System.out.print("Enter the item then select enter followed by the price and enter.");
item = input.nextLine();
price = input.nextDouble();
cart.put(item, price);
System.out.print("If done type done if not continue with adding to cart.");
ans = input.nextLine();
if(ans.toLowerCase.equals("done")) //to handle upper and lower case
{
done = true;
}
else
{ //Add curly braces*************
item = input.nextLine();
price = input.nextDouble();
cart.put(item, price);
System.out.print("If done type done if not continue with adding to cart.");
}
}while(!done);
System.out.print("Yo");
被视为其他
的一部分;其余的都是无条件执行的
请注意,只有一个地方可以将done
设置为true
,您可以将do
/while
循环替换为“永久”循环,并使用break
从中间退出循环:
while (true) {
...
if (ans.equals("Done")) {
break;
}
...
}
这使得声明done
变量变得不必要。公共静态void main(字符串[]args)
public static void main(String[] args)
{
//Input items for shopping cart
HashMap<String, Double> cart = new HashMap<String, Double>();
//Create a Scanner
Scanner input = new Scanner(System.in).useLocale(Locale.US);
//variables
String item = "";
double price;
boolean done = false;
String ans = ""; //Initialize to empty, just my common practice.
do
{
System.out.print("Enter the item then select enter followed by the price and enter.");
item = input.nextLine();
price = input.nextDouble();
cart.put(item, price);
System.out.print("If done type done if not continue with adding to cart.");
ans = input.nextLine();
if(ans.toLowerCase.equals("done")) //to handle upper and lower case
{
done = true;
}
else
{ //Add curly braces*************
item = input.nextLine();
price = input.nextDouble();
cart.put(item, price);
System.out.print("If done type done if not continue with adding to cart.");
}
}while(!done);
System.out.print("Yo");
{
//为购物车输入项目
HashMap购物车=新建HashMap();
//创建扫描仪
扫描仪输入=新扫描仪(System.in).useLocale(Locale.US);
//变数
字符串项=”;
双倍价格;
布尔完成=假;
字符串ans=“”;//初始化为空,这是我的常见做法。
做
{
System.out.print(“输入项目,然后选择输入,然后选择价格和输入”);
item=input.nextLine();
price=input.nextDouble();
购物车(商品、价格);
System.out.print(“如果完成,则键入完成,如果不继续添加到购物车。”);
ans=input.nextLine();
if(ans.toLowerCase.equals(“done”)//处理大小写
{
完成=正确;
}
其他的
{//添加大括号*************
item=input.nextLine();
price=input.nextDouble();
购物车(商品、价格);
System.out.print(“如果完成,则键入完成,如果不继续添加到购物车。”);
}
}而(!完成);
系统输出打印(“Yo”);
}
基本上,你所要做的就是合上牙套。另外,您可以通过将第一组.next-->()移到do语句上方,然后让else语句处理.next-->()公共静态void main(String[]args),使其更加模块化
{
//为购物车输入项目
HashMap购物车=新建HashMap();
//创建扫描仪
扫描仪输入=新扫描仪(System.in).useLocale(Locale.US);
//变数
字符串项=”;
双倍价格;
布尔完成=假;
字符串ans=“”;//初始化为空,这是我的常见做法。
做
{
System.out.print(“输入项目,然后选择输入,然后选择价格和输入”);
item=input.nextLine();
price=input.nextDouble();
购物车(商品、价格);
System.out.print(“如果完成,则键入完成,如果不继续添加到购物车。”);
ans=input.nextLine();
if(ans.toLowerCase.equals(“done”)//处理大小写
{
完成=正确;
}
其他的
{//添加大括号*************
item=input.nextLine();
price=input.nextDouble();
购物车(商品、价格);
System.out.print(“如果完成,则键入完成,如果不继续添加到购物车。”);
}
}而(!完成);
系统输出打印(“Yo”);
}
基本上,你所要做的就是合上牙套。此外,您可以通过将第一组.next-->()移到do语句上方,然后让else语句处理.next-->()使其更加模块化。除了缺少大括号外,程序还存在一个问题,即显示键入
done
的指令,但该程序实际上检查是否已完成
。equals
方法要求字母大小写完全相同。因此,如果您按照说明进行操作,那么您的循环可能永远不会退出
这是解决该问题的一种方法:
if(ans.equalsIgnoreCase("Done"))
除了缺少大括号外,程序显示键入
done
的指令也是一个问题,但程序实际上会检查done
。equals
方法要求字母大小写完全相同。因此,如果您按照说明进行操作,那么您的循环可能永远不会退出
这是解决该问题的一种方法:
if(ans.equalsIgnoreCase("Done"))
你用的是什么IDE?查找逻辑错误的最简单方法是使用调试器并单步执行程序。在“else”后面的位周围需要大括号。正如我