Java for循环!会打印数组内容 [英] Java for loop ! does prints array contents

查看:87
本文介绍了Java for循环!会打印数组内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应打印数组内容的代码,打印数组字内容为secuence但不打印卫星内容为数字,打印cs后四次迭代打印卫星然后经过四次迭代导航然后Honmgyan,应打印

CS

软件

卫星

Java

导航

Android

红岩

代码



Code that should print array contents , prints the array word contents as secuence but does not prints the satellite contents as numbers, prints cs and after four iterations prints satellite then after four iterations Navigation and then Honmgyan, should print
CS
Software
Satellite
Java
Navigation
Android
Hongyan
Code

public class ReadAndWriteFromArrays
  {     
    public static void main(String[] args)
    {
    BufferedWriter bw = null;
    FileWriter fw = null;                                 
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

            String word[];
            word = new String[4];
            word[0] = "Software";
            word[1] = "Java";
            word[2] = "Android";
            word[3] = "Code";

            String Satellite[];
            Satellite = new String[4];
            Satellite[0] = "CS";
            Satellite[1] = "Satellite";
            Satellite[2] = "Navigation";
            Satellite[3] = "Hongyan";

                for(String Ad : Satellite)
                for(String Ac : word)
                {
                    String FILENAME = "F:\\"+ Ad +".html";
                    try
                    {
                        System.out.print("Word: " + Ad);
                        String French = reader.readLine();

                        System.out.print("Words:" + Ac);
                        String Android = reader.readLine();

                        System.out.print("a: ");
                        String a = reader.readLine();   

                        System.out.print("b: ");
                        String b = reader.readLine();
                    }

                    catch(IOException d)
                    {                                             
                        d.printStackTrace();
                    }
                    finally{
                        try {
                            if (bw != null)
                                bw.close();

                            if (fw != null)
                                fw.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            try {
                if (bw != null)
                    bw.close();

                if (fw != null)
                    fw.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
}              
}





我尝试了什么:



为循环更改,还有另一种方法可以写几个for循环,以便将不同的数组内容打印为编号



What I have tried:

changed for loops , there is another way to write several for loops so that different array contents are printed as numbered

推荐答案

你有一个嵌套循环。因此内部代码将执行4 * 4 = 16次。



如果您确定两个数组在相同索引处具有相同的大小和相应的项目使用一个循环打印项目:

You have a nested loop. So the inner code will be executed 4 * 4 = 16 times.

If you know for sure that both arrays have the same size and corresponding items at the same index just use one loop and print the items:
for (int i = 0; i < word.length; i++)
{
    System.out.println(Satellite[i]);
    System.out.println(word[i]);
}

以上内容会根据您提问中的要求打印出来。

The above would print it out as requested in your question.


for(int i = 0; i < 4; ++i)
{
//    String FILENAME = "F:\\"+ Ad +".html";
    System.out.println("Words:" + Satellite[i]);
    System.out.println("Word: " + word[i]);
}


这篇关于Java for循环!会打印数组内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆