如何在我的生肖星座 Java 项目中使用循环。

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

How do i use Loop for my Chinese Zodiac Sign Java Project

问题

我开始学习Java,我是一个完全的新手。我决定在我的项目中制作生肖配对,但要求是:
我们应该使用数组和循环。

我已经弄清楚了如何在这个项目中使用数组,但我不知道如何插入循环。我尝试在网上搜索示例,但都是关于switch和if else语句的。希望你们能帮我解决这个问题。谢谢。

英文:

I started learning java and im a total newbie. I decided to make Chinese Zodiac signs for my project but the requirements are:
we should use arrays and loops

i already figured out how to use arrays for this project but i dont know how to insert the loops. i tried googling for examples but all are switch and if else statements. hope you guys could help me with this. thanks.

答案1

得分: 1

你创建一个数组。填充它。然后使用从0开始的for循环,因为数组从0开始,并一直进行到数组的最后一个元素。在我的示例中,我有一个包含3个元素的数组,所以我的元素将位于0、1、2位置。zodiacs.length会给我3,所以我从0数到2,包括2。

使用for循环

String zodiacs[] = new String[3]; 
zodiacs[0] = "Aries";
zodiacs[1] = "Whatever";
zodiacs[2] = "something";

for (int i = 0; i < zodiacs.length; i++) {
    System.out.println(zodiacs[i]);
}

使用while循环

String zodiacs[] = new String[3]; 
zodiacs[0] = "Aries";
zodiacs[1] = "Whatever";
zodiacs[2] = "something";

int i = 0;

while (i < zodiacs.length) {
    System.out.println(zodiacs[i]);
    i++;
}

由于你不允许使用HashMap,想法是创建两个数组:一个用于年份,一个用于生肖符号。每个生肖符号应该与其代表年份的索引相同。例如,“Whatever”在1992年,所以zodiacs[0]应该是“Whatever”,在years数组中1992应该在years[0]位置。

String zodiacs[] = new String[3];
int years[] = new int[3];

zodiacs[0] = "Aries";
zodiacs[1] = "Whatever";
zodiacs[2] = "something";

years[0] = 1991;
years[1] = 1992;
years[2] = 1993;

int yearUserWasBorn = 1992;

int i = 0;
int zodiacIndexForUserYear = -1;

while (i < years.length) {
    if (years[i] == yearUserWasBorn) {
        zodiacIndexForUserYear = i;
        break;
    }
    i++;
}

if (zodiacIndexForUserYear == -1) {
    System.out.println("Sorry we couldn't find your zodiac based on your year");
} else {
    System.out.println("Zodiac is : " + zodiacs[zodiacIndexForUserYear]);
}
英文:

You create array. Populate it. Then use for loop starting at 0 because arrays start at 0 and go until you are at last element of array. In my example I have array with 3 elements so my elements will be at 0,1,2 positions. zodiacs.length will give me 3 so I start counting from 0 to 2 including 2

Using for loop

String zodiacs[]  =new String[3]; 
    zodiacs[0] = &quot;Aries&quot;;
    zodiacs[1] = &quot;Whatever&quot;;
    zodiacs[2] = &quot;something&quot;;


    for(int i = 0 ; i &lt; zodiacs.length ; i++) {
        System.out.println(zodiacs[i]);
    }

Using while loop

String zodiacs[]  =new String[3]; 
zodiacs[0] = &quot;Aries&quot;;
zodiacs[1] = &quot;Whatever&quot;;
zodiacs[2] = &quot;something&quot;;


int i = 0;

while(i &lt; zodiacs.length){
    System.out.println(zodiacs[i]);
    i++;
}

Since you are not allowed to use HashMap, idea is to create 2 arrays: one for years and one for zodiac signs. Each zodiac sign should be at same index as it's representative year. example "Whatever" is in 1992 so at zodiacs[0] should be "Whatever" and in years 1992 shoud be at years[0]

String zodiacs[]  =new String[3];
    int years[]  =new int[3];
    
    zodiacs[0] = &quot;Aries&quot;;
    zodiacs[1] = &quot;Whatever&quot;;
    zodiacs[2] = &quot;something&quot;;
    
    years[0] = 1991;
    years[1] = 1992;
    years[2] = 1993;
    
    int yearUserWasBorn = 1992;

    int i = 0;
    int zodiacIndexForUserYear = -1;
    
    while(i &lt; years.length){
      
      if(years[i] == yearUserWasBorn){
          zodiacIndexForUserYear = i;
          break;
      }
      
      i++;
    }
    
    if(zodiacIndexForUserYear == -1){
        System.out.println(&quot;Sorry we couldn&#39;t find you zodiac based on you year&quot;);
    }else{
        System.out.println(&quot;Zodiac is : &quot; + zodiacs[i]);
    }

huangapple
  • 本文由 发表于 2020年4月5日 19:14:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61041664.html
匿名

发表评论

匿名网友

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

确定