java - 如何返回数组的元素编号?

标签 java arrays element

我已经编写了下面的代码,现在我需要编辑它,以便它显示已超过 3000 的结账号码。即它必须显示:

以下结账金额已超过 3000.00 英镑: 结帐编号“元素编号” 结帐编号“元素编号”等

我一直在尝试一些不同的方法,但还没有成功。感谢您的帮助 !

import java.util.Scanner;
public class Checkouts
{
   static final int NUMBER_OF_CHECKOUTS = 6, MAX_TAKING = 1000000;

   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      double[] checkoutList = new double[NUMBER_OF_CHECKOUTS];

  System.out.println();
  for (int index = 0; index < checkoutList.length; index++)
  {
     System.out.print("\tEnter takings for checkout number ");
     System.out.print((index + 1) + ": ");
     checkoutList[index] = scan.nextDouble();
     while (checkoutList[index] < 0 || checkoutList[index] > MAX_TAKING)
     {
        System.out.print("\tImpossible! - enter takings for checkout number ");
        System.out.print((index + 1) + " again: ");
        checkoutList[index] = scan.nextDouble();
     } 
  }
  System.out.println();
  double totalTakings = 0;
  for (int index = 0; index < checkoutList.length; index++)
  {
     totalTakings += checkoutList[index];
  }

  System.out.println("The total takings for the supermarket is " + totalTakings );

} }

最佳答案

由于您已经有一个遍历每个索引的 for 循环,我们可以在那里进行检查:

  System.out.println("The following checkouts have taken more than 3000.00 pounds:");
  for (int index = 0; index < checkoutList.length; index++)
  {
     totalTakings += checkoutList[index];

     if(checkoutList[index] > 3000.00){
         //now we can be sure the current index is the location of
         //a checkout that was > 3000.00 pounds
         System.out.println("Checkout Number: " + index);
     }
  }

关于java - 如何返回数组的元素编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47368978/

相关文章:

java - 如何传递数组维度?

java - 给定字符串为空或 null

java - 如何在 Java 8 中使用过滤器忽略 int 数组中的值并收集

java - ArrayIndexOutOfBoundsException : -1

C++ 带有记录数组的交换函数

javascript - 从数组中删除一个元素,然后在不使用全局变量的情况下保留其状态以备将来使用

java - Artifactory java.lang.ClassNotFoundException : org. glassfish.jersey.servlet.internal.ServletContainerProviderFactory

java - 设置java中主类中一个类中数组的大小

c++ - 在 C++ 中迭代列表并搜索另一个列表中的每个元素

javascript - jQuery:如果 $(this) 或另一个元素模糊,则