gpt4 book ai didi

java - 如何在Java中使用For循环询问用户输入

转载 作者:行者123 更新时间:2023-11-30 04:22:24 25 4
gpt4 key购买 nike

这是我的作业:

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



}
}
}

我需要做什么才能向用户询问三次价格?

最佳答案

我不会为您编写所有代码,但这可以使用 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
}

关于java - 如何在Java中使用For循环询问用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16715933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com