英文:
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[] = {"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]));
}
}
}
}
答案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[] = {"Ianuarie ", "Februarie ", "Martie ", "Aprilie ", "Mai ", "Iunie ", "Iulie ", "August ", "Septembrie ", "Octombrie ", "Noiembrie ", "Decembrie "};
System.out.println("Printing all the Months and days for each months.....");
int i;
for (i = 0; i < 12; i++) {
System.out.println(months[i] + month_days[i] + " zile.");
}
System.out.println("printing Months having 31 days only .....");
for (i = 0; i < 12; i++) {
if(month_days[i] == 31) {
System.out.println(months[i] + " has " + month_days[i] + " days");
}
}
}
}
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[] = { "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.");
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论