使用While循环代替Java中的For循环来询问用户输入 [英] Using While Loop instead of a For Loop in Java to ask User Input

查看:126
本文介绍了使用While循环代替Java中的For循环来询问用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Scanner askPrice = new Scanner(System.in); 

for(double i = 0; i <3; i ++);
{
double totalInitial = 0.00;
System.out.println(为您的物品输入价格。
+每次输入后按回车键,不要输入'$'符号:);
double price1 = askPrice.nextDouble(); //输入价格one
double price2 = askPrice.nextDouble(); //输入价格二
double price3 = askPrice.nextDouble(); //输入价格三

double total =((totalInitial)+(price1)+(price2)+(price3));

我想将for循环更改为while循环来询问用户物品的价格输入一个双精度值),直到定点值。我怎样才能做到这一点?我知道我已经设置了三个迭代,但是我想修改没有预设迭代次数的代码。任何帮助,将不胜感激。

解决方案

您可以试试这个:

  Scanner askPrice = new Scanner(System.in); 
//我们初始化一个拳头BigDecimal在0
BigDecimal totalPrice = new BigDecimal(0);
//无限循环...
while(true){
// ...其中我们查询用户
System.out.println(为您输入价格item。
+每次输入后按回车键,不要输入'$'符号:);
// ...尝试获取用户输入的下一个double
//并将其添加到总计
try {
double price = askPrice.nextDouble();
//这里是添加if语句来检查
//用户输入的值(如果需要,中断)
// - 不正确的输入在 catch语句通过
totalPrice = totalPrice.add(new BigDecimal(String.valueOf(price)));
//这是一个很好的地方添加一个if语句来检查
//总数和中断如有必要
}
// ...直到被意外破坏输入或任何其他异常
catch(Throwable t){
//您应该根据
作出不同的反应//异常抛出
System.out.println(finished - TODO处理单个例外);
//这打破了无限循环
break;


打印出最终结果
System.out.println(totalPrice.toString());

请注意 BigDecimal 的货币。

I wrote this piece of code:

Scanner askPrice = new Scanner(System.in);

for(double i = 0 ; i < 3; i++);
{
double totalInitial = 0.00;
System.out.println("Enter the price for your item. "
+ "Press enter after each entry. Do not type the '$' sign: ") ;
double price1 = askPrice.nextDouble(); //enter price one
double price2 = askPrice.nextDouble(); //enter price two
double price3 = askPrice.nextDouble(); //enter price three

double total = ((totalInitial) + (price1) + (price2) + (price3));

I want to change the for loop to a while loop to ask the user a price for an item (input of a double) until a sentinel value. How can I do this? I know I have three iterations already set, but I want to modify the code where there is not a preset number of iterations. Any help would be appreciated.

解决方案

You could try this:

Scanner askPrice = new Scanner(System.in);
// we initialize a fist BigDecimal at 0
BigDecimal totalPrice = new BigDecimal("0");
// infinite loop...
while (true) {
    // ...wherein we query the user
    System.out.println("Enter the price for your item. "
        + "Press enter after each entry. Do not type the '$' sign: ") ;
    // ... attempt to get the next double typed by user 
    // and add it to the total
    try {
        double price = askPrice.nextDouble();
            // here's a good place to add an "if" statement to check 
            // the value of user's input (and break if necessary) 
            // - incorrect inputs are handled in the "catch" statement though
        totalPrice = totalPrice.add(new BigDecimal(String.valueOf(price)));
            // here's a good place to add an "if" statement to check
            // the total and break if necessary
    }
    // ... until broken by an unexpected input, or any other exception
    catch (Throwable t) {
            // you should probably react differently according to the 
            // Exception thrown
        System.out.println("finished - TODO handle single exceptions");
            // this breaks the infinite loop
        break;
    }
}
// printing out final result
System.out.println(totalPrice.toString());

Note the BigDecimal here to better handle sums of currency.

这篇关于使用While循环代替Java中的For循环来询问用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆