Java 如何返回数组的元素号?
我已经写了下面的代码,现在我需要编辑它,以便它显示超过3000次的结账号码。i、 e.它必须显示: 以下结账花费超过3000.00英镑: 签出编号“元素编号” 结帐编号“元素编号”等 我一直在尝试几种不同的方法,但还没有成功。谢谢你的帮助Java 如何返回数组的元素号?,java,arrays,elements,Java,Arrays,Elements,我已经写了下面的代码,现在我需要编辑它,以便它显示超过3000次的结账号码。i、 e.它必须显示: 以下结账花费超过3000.00英镑: 签出编号“元素编号” 结帐编号“元素编号”等 我一直在尝试几种不同的方法,但还没有成功。谢谢你的帮助 import java.util.Scanner; public class Checkouts { static final int NUMBER_OF_CHECKOUTS = 6, MAX_TAKING = 1000000; public s
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 );
import java.util.Scanner;
公共类结帐
{
静态最终整数校验次数=6,最大取数=1000000;
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
double[]checkoutList=新的double[签出的数量];
System.out.println();
for(int index=0;indexMAX|u take)
{
System.out.print(“\t可能!-输入结帐编号的收入”);
系统输出打印((索引+1)+“再次:”);
checkoutList[索引]=scan.nextDouble();
}
}
System.out.println();
双倍总收入=0;
for(int index=0;index
}
}既然您已经有了一个遍历每个索引的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);
}
}
System.out.println(“以下结账花费超过3000.00英镑:”);
for(int index=0;index3000.00){
//现在我们可以确定当前索引是
//超过3000.00英镑的结帐
System.out.println(“签出编号:+索引”);
}
}
您能解释一下代码的作用吗?你采用了什么方法?你已经知道如何编写for循环,我想你也知道if
语句。因此,您应该能够将两者结合起来以实现您想要的。循环检查签出,如果其中一个高于3000,则打印其索引。