【问题标题】:How to use For Loop in Java to ask user for Input如何在 Java 中使用 For 循环来询问用户输入
【发布时间】:2013-05-23 13:59:21
【问题描述】:

这是我的任务:

编写一个模拟收银机的程序。提示用户输入三件商品的价格。将它们加在一起以获得小计。确定小计的税 (6%)。找到销售小计加税的总金额。显示每件商品的价格、小计金额、税额和最终金额。

我是这样做的:

package cashregisteremulator;

import java.util.Scanner;

public class CashRegisterEmulator {

  public static void main(String[] args) {

    Scanner price = new Scanner(System.in);


    System.out.print("Please enter a price for item number one $");
    double price1 = price.nextDouble();

    System.out.print("Please enter a price for item number two $" );
    double price2 = price.nextDouble();

    System.out.print("Please enter a price for item number three $");
    double price3 = price.nextDouble();

    double total = ((price1) + (price2) + (price3));
    System.out.println("The subtotal is $" + total);

    double tax = .06;

    double totalnotax = (total * tax );
    System.out.println("The tax for the subtotal is $" + totalnotax);
    double totalplustax = (total + totalnotax);
    System.out.println("The total for your bill with tax is $" + totalplustax);

  }
}

但是,我需要使用循环来执行此分配。由于提示只要求 3 次迭代,我想到了使用 for 循环。到目前为止,我有这个:

package CashRegister;

import java.util.Scanner;

 public class CashRegister {

public static void main(String[] args) {
    Scanner askPrice = new Scanner(System.in);  

   for(double i = 0 ; i < 3; i++);  
 {
   System.out.println("Enter a value") ;
   double price = askPrice.nextDouble();



 }
}    
}

我要怎么做才能向用户要价三倍?

【问题讨论】:

  • 为什么要求用户输入一个double,然后将其分配给counter,然后将counter设置为0?
  • 我不确定你在问什么。

标签: java for-loop iteration


【解决方案1】:

我不会为你编写所有代码,但这可以使用 for 循环轻松实现:

int numberOfIterations = 3;

// The format of the for loop:
// for (initialize counter; continuation condition; incrementer)
for(int i = 0; i < numberOfIterations; i++){
    // do something
}

【讨论】:

    【解决方案2】:
    double sum = 0;
    for(int i = ; i < n; i++)
    {
        //ask for value;
        sum += value;
    }
    

    更新

    在您的更新中,这必须进入 for 循环:

    double[] amount = new int[3];
    for(int i = 0; i < amount.len; i++)
    {
        // ask user for value
        amount[i] = value;
    }
    
    for(int i = 0; i < amount.len; i++)
    {
         // do your calculations and print it.
    }
    
    // print the total sums.
    

    对于总计,您需要额外的变量。

    【讨论】:

    • 没有理由变得过于复杂。如果 OP 不理解 for 循环,他们就不会理解接口和集合。坚持使用数组(特别是因为有预定义的固定迭代次数)。
    • 是的,我刚刚意识到它并将其重写为一个简单的版本,因为甚至不需要数组。 :)
    • 它将进入 for 循环。您是否真的需要一个数组取决于最终列表的外观。为用户输入的内容创建一个数组绝对是个好主意,然后您可以遍历它,并进行单独的计算和打印。
    【解决方案3】:

    您需要进行 3 次迭代,对于这种情况,最好使用 for 循环。为了存储用户输入,您需要一个 数组,在其中存储具有 for 循环中当前位置的 index 的值.

    看看我在你的代码上构建的这个例子:

    double[] price= new double[3];
    for (int i=0; i<3; i++); {
         System.out.println("Enter another ");
         double price[i] = price.nextDouble();
    } 
    

    稍后,您可以遍历价格数组,并添加所有值:

    double total = 0.0;
    for (int i=0; i<price.length; i++) {
        total += price[i];
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-05
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 2021-12-08
      相关资源
      最近更新 更多