2017-11-18 3 views
1

나는 아래의 코드를 작성했습니다 지금은 더 이상 3000은 ieit 표시해야한다 촬영 한 체크 아웃 번호를 표시하도록 편집해야합니다배열의 요소 번호를 반환하려면 어떻게해야합니까?

다음 체크 아웃 촬영 한 개 이상의 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); 

} }

+0

당신은 당신의 코드가 무엇을 설명 할 수 있습니까? 당신이 접근 한 것은 무엇입니까? – rushi

+0

당신은 이미 for 루프를 작성하는 방법을 알고 있으며'if' 문에 대해서도 알고있을 것입니다. 따라서 둘을 결합하여 원하는 것을 구현할 수 있어야합니다. 체크 아웃을 반복하고 * 3000 *이 3000 이상인 경우 색인을 인쇄하십시오. –

답변

1

이미 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); 
    } 
    } 
관련 문제