使用 java if-else

这是我第一次使用堆栈溢出来提问。我是 Java 编程的初学者。我被我的任务困住了,有人可以帮我解决吗?

所以,问题是使用java if-else将图像上的内容写入java代码。但我没有得到这个问题。谁能解释一下?是否可以使用 if-else 进行编码?谢谢你。

http://img1.mukewang.com/61a7251d0001f03d05520290.jpg

import java.util.Scanner;

public class MailOrderHouse{


    public static void main(String[] args){


       Scanner sc = new Scanner(System.in);

       double product1;

       double product2;

       double product3;

       double product4;

       double product5;


       System.out.println("Product price: ");

       double product_price = sc.nextDouble();


       System.out.println("Enter quantity sold: ");

       int quantity = sc.nextInt();


    }

}

我完全不明白这个问题。


呼如林
浏览 143回答 2
2回答

MM们

首先,向用户说明可用的产品及其各自的价格:int productChoice = 0;int quantity = 0;double totalSum = 0.0;System.out.println("Welcome To The Mail_Order House.");System.out.println("Please select Product Number (1 to 5) you want to buy:\n");System.out.println("1) Product Name 1: RM2.98");System.out.println("2) Product Name 2: RM4.50");System.out.println("3) Product Name 3: RM9.98");System.out.println("4) Product Name 4: RM4.49");System.out.println("5) Product Name 5: RM6.87");这使用户可以轻松查看可以购买的商品,从而做出有效的选择。现在要求用户输入产品编号:productChoice = sc.nextInt();用户提供的值与他/她想要的产品名称相关。现在只需询问用户该特定产品的所需数量即可:System.out.println("What quantity of Product #" + productChoice + " do you want?");quantity = sc.nextInt();既然我们有了产品数量,就可以使用IF/ELSE IF来收集所选产品的价格并将其乘以用户提供的数量以获得该产品的总欠款:if (productChoice == 1) {    // ......TO DO........}else if (productChoice == 2) {    totalSum += 4.50 * quantity;    // This is the same as: totalSum = totalSum + (4.50 * quantity);}else if (productChoice == 3) {    // ......TO DO........}else if (productChoice == 4) {    // ......TO DO........}else if (productChoice == 5) {    // ......TO DO........}else {    System.out.println("Invalid product number supplied!");}如您所见,您现在拥有向控制台显示所需输出字符串所需的所有数据:System.out.println("Mail-Order House sold " + quantity +                    " of Product #" + productChoice + " for: RM" +                    String.format("%.2f", totalSum));在String.format("%.2f", totalSum)上述线可确保在总和2个精确到小数点后显示给控制台。21.422000522340在这种特殊情况下,您不希望像这样的数字显示为货币值(阅读String.format()方法)。

弑天下

您必须为已售产品数量输入 5 个输入值,为产品价格输入 5 个输入值。您必须计算这些产品的总价。您可以使用如下循环,而不是为 10 个输入取 10 个变量:import java.util.Scanner;public class MailOrderHouse{&nbsp; &nbsp; public static void main(String[] args){&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; double total = 0;&nbsp; &nbsp; &nbsp; &nbsp; int totalProduct = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 5; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int productQuantity = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double productPrice = sc.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total += productPrice;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; totalProduct += productQuantity;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Mail-order house sell " + totalProduct + " product " + totalProduct + " for RM" + productPrice);&nbsp;&nbsp; &nbsp; }}虽然无法理解您的输入格式。希望能帮助到你。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java