如何在Java中使用For循环请求用户输入

如何在Java中使用For循环请求用户输入,java,for-loop,iteration,Java,For Loop,Iteration,这是我的任务: 编写一个模拟收银机的程序。提示用户输入三个项目的价格。将它们相加,得到小计。确定小计的税费(6%)。查找销售小计加税金的总额。显示每个项目的价格、小计金额、税额和最终金额。 我是这样做的: package cashregisteremulator; import java.util.Scanner; public class CashRegisterEmulator { public static void main(String[] args) { Scan

这是我的任务:

编写一个模拟收银机的程序。提示用户输入三个项目的价格。将它们相加,得到小计。确定小计的税费(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();



 }
}    
}
package收银机;
导入java.util.Scanner;
公营收银机{
公共静态void main(字符串[]args){
扫描仪askPrice=新扫描仪(System.in);
对于(双i=0;i<3;i++);
{
System.out.println(“输入值”);
双倍价格=askPrice.nextDouble();
}
}    
}

为了三次向用户索要价格,我必须做些什么?

您需要进行三次迭代,对于这种情况,最好使用for循环。为了存储用户输入,您需要一个数组,在该数组中,您将当前位置的索引值存储在for循环

看一看我基于您的代码构建的示例:

double[] price= new double[3];
for (int i=0; i<3; i++); {
     System.out.println("Enter another ");
     double price[i] = price.nextDouble();
} 
double[]价格=新的double[3];
对于(int i=0;i
double sum=0;
for(int i=;i
更新

在您的更新中,这必须进入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.
int numberOfIterations = 3;

// The format of the for loop:
// for (initialize counter; continuation condition; incrementer)
for(int i = 0; i < numberOfIterations; i++){
    // do something
}
double[]金额=新整数[3];
对于(int i=0;i

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

我不打算为您编写所有代码,但这可以通过使用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.
int numberOfIterations = 3;

// The format of the for loop:
// for (initialize counter; continuation condition; incrementer)
for(int i = 0; i < numberOfIterations; i++){
    // do something
}
int numberOfIterations=3;
//for循环的格式:
//for(初始化计数器;连续条件;递增)
对于(int i=0;i
为什么要求用户输入一个double,然后将其分配给
计数器
,然后将
计数器
设置为0?我不太清楚你在问什么。没有理由过于复杂。如果OP不理解for循环,他们就不会理解接口和集合。坚持使用数组(特别是因为有一个预定义的固定迭代次数)。是的,我刚刚意识到了这一点,并将其改写为一个简单的版本,因为甚至不需要数组它将进入for循环。您是否真的需要数组取决于最终列表的外观。为用户输入的内容创建一个数组肯定是个好主意,然后您可以循环它,并进行单独的计算和打印。