在获得输出方面我做错了什么?

时间:2018-04-26 15:47:05

标签: java arrays

import java.util.Scanner;

public class Qa2QeArray2 {

    public static void main(String[] args) {
        int numOfAircrafts;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter num Of Aircrafts : ");
        numOfAircrafts = sc.nextInt();
        //To Determine the length of the Array
        String[] stringArray = new String[numOfAircrafts];
        System.out.println("Please enter the names of the Aircrafts : ");
        for(int i = 0; i < numOfAircrafts; i++)
        {
            stringArray[i] = sc.next();
        }
        //To insert the string values to the Array
        double[] doubleArray = new double[numOfAircrafts];
        System.out.println("Please enter the shipment rate through each aircraft carrier : ");
        for(int i = 0; i < numOfAircrafts; i++)
        {
            doubleArray[i] = sc.nextDouble();
        }
        //To insert the double values to the Array
        String[] dubArr = new String [stringArray.length * doubleArray.length];

        for (int x = 0; x < stringArray.length ; x++) 
            for (int y = 0; y < doubleArray.length ; y++) {

        System.out.println(stringArray[x] + " \t " + doubleArray[y]); 
            }
    }

}

我正在寻找o / p,如下所示

Emirates     20.89
Indigo       10.34

但我得到的是o / p

Emirates     20.89
Emirates     10.34
Indigo       20.89
Indigo       10.34

1 个答案:

答案 0 :(得分:0)

您有两个正在运行的嵌套循环。 x循环和y循环。 x=0打印值时x=1 它打印值。 这就是你输出两次的原因。

删除一个循环,你就可以了。