Java数组仅显示来自两个数组的特定值。

huangapple go评论73阅读模式
英文:

Java Arrays to show only an exact value from 2 arrays

问题

class Main {
    public static void main(String[] args) {
        int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        String months[] = {"Ianuarie ", "Februarie ", "Martie ", "Aprilie ", "Mai ", "Iunie ", "Iulie ", "August ", "Septembrie ", "Octombrie ", "Noiembrie  ", "Decembrie "};

        {
            int i;
            for (i = 0; i < 12; i++) {
                System.out.println(months[i] + month_days[i] + " zile.");
            }
        }
        {
            int i;
            for (i = 0; i < 12; i++) {
                System.out.println((month_days[i]));
            }
        }
    }
}
英文:

Hello i'm a student and i've just started studying java, and i have a problem with an exercise, the first thing i had to make was two arrays to show how many days each month has, but now i have to make another exercise in which i only have to display the months that has 31 days. can you explain me how to display that?

class Main {
    public static void main(String[] args) {
        int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        String months[] = {&quot;Ianuarie &quot;, &quot;Februarie &quot;, &quot;Martie &quot;, &quot;Aprilie &quot;, &quot;Mai &quot;, &quot;Iunie &quot;, &quot;Iulie &quot;, &quot;August &quot;, &quot;Septembrie &quot;, &quot;Octombrie &quot;, &quot;Noiembrie  &quot;, &quot;Decembrie &quot;};
        {
            int i;
            for (i = 0; i &lt; 12; i++) {
                System.out.println(months[i] + month_days[i] + &quot; zile.&quot;);
            }
        }
        {
            int i;
            for (i = 0; i &lt; 12; i++) {
                System.out.println((month_days[i]));
            }
        }
    }
}

答案1

得分: 0

你应该使用 if 条件来检查哪个月有31天,并相应地打印出来。

代码

    public class Main {
        public static void main(String[] args) {
            int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            String months[] = {"Ianuarie ", "Februarie ", "Martie ", "Aprilie ", "Mai ", "Iunie ", "Iulie ", "August ", "Septembrie ", "Octombrie ", "Noiembrie  ", "Decembrie "};
        
            System.out.println("打印每个月的天数.....");
            int i;
            for (i = 0; i < 12; i++) {
                System.out.println(months[i] + month_days[i] + " zile.");
            }
    
            System.out.println("打印只有31天的月份.....");
            for (i = 0; i < 12; i++) {
                if(month_days[i] == 31) {
                    System.out.println(months[i] + " 有 " + month_days[i] + " 天");
                }
            }
        }
    }

输出:

打印每个月的天数.....
Ianuarie 31 zile.
Februarie 28 zile.
Martie 31 zile.
Aprilie 30 zile.
Mai 31 zile.
Iunie 30 zile.
Iulie 31 zile.
August 31 zile.
Septembrie 30 zile.
Octombrie 31 zile.
Noiembrie  30 zile.
Decembrie 31 zile.

打印只有31天的月份.....
Mai  有 31 天
Iulie  有 31 天
August  有 31 天
Octombrie  有 31 天
Decembrie  有 31 天
英文:

You should use an if condition to check which month has 31 days and print it accordingly.

Code

public class Main {
	public static void main(String[] args) {
         int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
         String months[] = {&quot;Ianuarie &quot;, &quot;Februarie &quot;, &quot;Martie &quot;, &quot;Aprilie &quot;, &quot;Mai &quot;, &quot;Iunie &quot;, &quot;Iulie &quot;, &quot;August &quot;, &quot;Septembrie &quot;, &quot;Octombrie &quot;, &quot;Noiembrie  &quot;, &quot;Decembrie &quot;};
    
         System.out.println(&quot;Printing all the Months and days for each months.....&quot;);
         int i;
         for (i = 0; i &lt; 12; i++) {
                 System.out.println(months[i] + month_days[i] + &quot; zile.&quot;);
         }

         System.out.println(&quot;printing Months having 31 days only .....&quot;);
         for (i = 0; i &lt; 12; i++) {
             if(month_days[i] == 31) {
                 System.out.println(months[i] + &quot; has &quot; + month_days[i] + &quot; days&quot;);
             }
         }
     }
}

Output :

Printing all the Months and days for each months.....  
                                                                                                      
Ianuarie 31 zile.                                                                                                                                           
Februarie 28 zile.                                                                                                                                          
Martie 31 zile.                                                                                                                                             
Aprilie 30 zile.                                                                                                                                            
Mai 31 zile.                                                                                                                                                
Iunie 30 zile.                                                                                                                                              
Iulie 31 zile.
August 31 zile.                                                                                                                                             
Septembrie 30 zile.                                                                                                                                         
Octombrie 31 zile.                                                                                                                                          
Noiembrie  30 zile. 
Decembrie 31 zile.  

                                                                                                                                        
printing Months having 31 days only .....

Mai  has 31 days                                                                                                                                              
Iulie  has 31 days                                                                                                                                            
August  has 31 days                                                                                                                                           
Octombrie  has 31 days                                                                                                                                        
Decembrie  has 31 days 

答案2

得分: 0

我强烈建议你从 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html 学习Java语言,然后你才能理解我所写的内容。

以下是其中关键概念的链接:
控制流语句
表达式、语句和块

public class Main {
public static void main(String argv[]) {
		int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		String months[] = { "Ianuarie ",
							"Februarie ",
							"Martie ",
							"Aprilie ",
							"Mai ",
							"Iunie ",
							"Iulie ",
							"August ",
							"Septembrie ",
							"Octombrie ",
							"Noiembrie  ",
							"Decembrie " };
		int i;
		System.out.println("Months with 31 zile.");
		for (i = 0; i < 12; i++) {
			if (month_days[i] == 31) {
				System.out.println(months[i] + month_days[i] + " zile.");				
			}
		}
		System.out.println("Months with less than 31 zile.");
		for (i = 0; i < 12; i++) {
			if (month_days[i] != 31) {
				System.out.println(months[i] + month_days[i] + " zile.");				
			}
		}
	}
}
英文:

I strongly recommend you learn java language from https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html before you can understand what I have written.

These are having key concepts:
Control Flow Statements, Expressions, Statements, and Blocks

public class Main {
public static void main(String argv[]) {
		int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		String months[] = { &quot;Ianuarie &quot;,
							&quot;Februarie &quot;,
							&quot;Martie &quot;,
							&quot;Aprilie &quot;,
							&quot;Mai &quot;,
							&quot;Iunie &quot;,
							&quot;Iulie &quot;,
							&quot;August &quot;,
							&quot;Septembrie &quot;,
							&quot;Octombrie &quot;,
							&quot;Noiembrie  &quot;,
							&quot;Decembrie &quot; };
		int i;
		System.out.println(&quot;Months with 31 zile.&quot;);
		for (i = 0; i &lt; 12; i++) {
			if (month_days[i] == 31) {
				System.out.println(months[i] + month_days[i] + &quot; zile.&quot;);				
			}
		}
		System.out.println(&quot;Months with less than 31 zile.&quot;);
		for (i = 0; i &lt; 12; i++) {
			if (month_days[i] != 31) {
				System.out.println(months[i] + month_days[i] + &quot; zile.&quot;);				
			}
		}
	}
}

huangapple
  • 本文由 发表于 2020年10月16日 18:46:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64387690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定