Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于存储总计和查找平均值的初学者Java问题_Java_Loops_If Statement_Average_Subtotal - Fatal编程技术网

关于存储总计和查找平均值的初学者Java问题

关于存储总计和查找平均值的初学者Java问题,java,loops,if-statement,average,subtotal,Java,Loops,If Statement,Average,Subtotal,我正在学习我的第一个编码课程,我遇到了一个循环问题。我应该写一个程序,将用户输入的特定数量和特定价格相乘,然后找到我购买的平均值。我遇到的问题是,你不能拥有超过10个项目的数量,我不知道如何确保在我乘以平均值时数量不会超过10,我尝试了if-then循环,最终跳到下一行,或者在询问他们是否愿意购买时删除初始值我将在下面发布我的代码以及我们最终应该得到的示例输出,感谢对初学者的任何帮助或建议 import java.util.Scanner; public class Main { pub

我正在学习我的第一个编码课程,我遇到了一个循环问题。我应该写一个程序,将用户输入的特定数量和特定价格相乘,然后找到我购买的平均值。我遇到的问题是,你不能拥有超过10个项目的数量,我不知道如何确保在我乘以平均值时数量不会超过10,我尝试了if-then循环,最终跳到下一行,或者在询问他们是否愿意购买时删除初始值我将在下面发布我的代码以及我们最终应该得到的示例输出,感谢对初学者的任何帮助或建议

import java.util.Scanner;

public class Main {

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

System.out.println("What is the quantity of your purchase?");

double a = cs.nextDouble();


  }


System.out.println("How much does your purchase cost?");




double b = cs.nextDouble();

  System.out.println("Would you like to purchase anything else? Please type yes or no.");


double total = a*b;

avg = (Float) ((a+b/2);

System.out.println("Your total is: "+total"\nThe average of your purchase is: " + avg);

  }
样本输出:

输入数量:3 输入价格:8.50 这将花费25.50美元

输入数量:12 输入错误!一次最多只能购买10件物品

输入数量:2 输入价格:5.50 这将花费11美元

输入数量:4 输入价格:3.00 这将花费12美元

您订购了9件物品,价格为48.50美元,平均每件5.39美元


它不必看起来完全像这样,但总体思路是订购各种未描述的产品,这些产品的数量和价格总计达到平均值。唯一的规则是数量必须保持在10以下,我使用的是浮动变量或双精度变量。再次感谢

您可以
typecast
将双精度值转换为int,然后在一些
if
语句中使用该值,查看该值是否为
<10
,具体取决于该值,然后执行进一步的操作

示例代码

 double total = 0.0;
 double avg = 0.0;
 double b = 0.0;
 Scanner cs = new Scanner(System.in);

 System.out.println("What is the quantity of your purchase?");

 double a = cs.nextDouble();
 int value = (int) a;
 int i = 0;
 if (value < 10) {
  //loop through items
  for (i = 0; i < value; i++) {

   System.out.println("How much does your purchase cost?" + i);

   b = cs.nextDouble();
   //add to total
   total += b;

  }
  //get avg with total no and total quantity
  avg = (double)((total / value));
  System.out.println("Your total is: " + total + "\nThe average of your purchase is: " + avg);
 } else {

  System.out.println("Incorrect Entry! You can only buy up to 10 items at once!");


 }
What is the quantity of your purchase?                                                                                        
13                                                                                                                            
Incorrect Entry! You can only buy up to 10 items at once!

您可以
typecast
将双精度值转换为int,然后在一些
if
语句中使用该值,以查看该值是否为
<10
,具体取决于该值,并执行进一步的操作

示例代码

 double total = 0.0;
 double avg = 0.0;
 double b = 0.0;
 Scanner cs = new Scanner(System.in);

 System.out.println("What is the quantity of your purchase?");

 double a = cs.nextDouble();
 int value = (int) a;
 int i = 0;
 if (value < 10) {
  //loop through items
  for (i = 0; i < value; i++) {

   System.out.println("How much does your purchase cost?" + i);

   b = cs.nextDouble();
   //add to total
   total += b;

  }
  //get avg with total no and total quantity
  avg = (double)((total / value));
  System.out.println("Your total is: " + total + "\nThe average of your purchase is: " + avg);
 } else {

  System.out.println("Incorrect Entry! You can only buy up to 10 items at once!");


 }
What is the quantity of your purchase?                                                                                        
13                                                                                                                            
Incorrect Entry! You can only buy up to 10 items at once!

您只需将quantity变量设为整数,因为quantity始终是十进制数,并插入if语句

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    Scanner cs=new Scanner(System.in);
    System.out.println("What is the quantity of your purchase?");
    int qunatity = cs.nextInt();
    if(a > 10) {
        System.out.println("Incorrect Entry! You can only buy up to 10 items at once!");
    }
    System.out.println("How much does your purchase cost?");
    double cost = cs.nextDouble();
    // System.out.println("Would you like to purchase anything else? Please type yes or no.");
    double total = a*b;
    avg = total / quantity;
    System.out.println("Your total is: "+total"\nThe average of your purchase is: " + avg);

  }


您只需将quantity变量设为整数,因为quantity始终是十进制数,并插入if语句

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    Scanner cs=new Scanner(System.in);
    System.out.println("What is the quantity of your purchase?");
    int qunatity = cs.nextInt();
    if(a > 10) {
        System.out.println("Incorrect Entry! You can only buy up to 10 items at once!");
    }
    System.out.println("How much does your purchase cost?");
    double cost = cs.nextDouble();
    // System.out.println("Would you like to purchase anything else? Please type yes or no.");
    double total = a*b;
    avg = total / quantity;
    System.out.println("Your total is: "+total"\nThe average of your purchase is: " + avg);

  }


你的循环在哪里,放置准确的代码并写下关于问题的明确信息一些代码格式将有助于回答问题。你的循环在哪里,放置准确的代码并写下关于问题的明确信息一些代码格式将有助于回答问题。这就是我缺少的,每次循环成功,我都会失去价值。谢谢现在我要弄清楚如何将整个过程循环四次,并将四次相加。只需在if语句中添加一个循环,即:
for(i=0;i您可以将此答案设置为..如果这解决了您的问题,这就是我所缺少的,每次我得到一个成功的循环,我都会失去我的价值。谢谢!现在我只想知道如何将整个循环四次,并将所有四次加在一起。只需在if语句中添加一个循环,即:
for(i=0;i您可以将此答案设置为..如果这解决了您的问题