Shinzenryu
Eternal Poster
- Joined
- Jun 4, 2015
- Posts
- 1,395
- Reaction
- 236
- Points
- 335
So, yesterday natapos ko yung Zodiac sign project na ginagawa ko using If... else statements. Ngayon i decide to make another Zodiac Sign project but this time it's Chinese zodiacs. So i have already figured out kung paano yun gawin using string arrays and modulos like this:
import java.util.Scanner;
public class ChineseZodiacs {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] yearSign = {"monkey", "rooster", "dog", "pig", "rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse", "sheep"};
System.out.print("Enter a year: ");
int year = input.nextInt();
System.out.println(zodiacs[year % 12]);
}
}
Pero gusto kong gawin itong Chinese Zodiacs without using modulus and gamitin ang Loops. And i tried googling for examples and i found one pero di ko maintindihan ito yung code:
public static void main(String[] args) {
int year;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a year");
year=sc.nextInt();
String[] yearSign = {"monkey", "rooster", "dog", "pig", "rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse", "sheep"};
int j = 0;
for ( int i = 1000; i <=year; i++) {
if (j == yearSign.length) {
j=0;
}
j++;
}
System.out.println("Your Chinese Zodiac is "+yearSign[j-1]);
Bakit po kailangan mag intialize ng another iteration na j? and bakit kailanga sya i initialize twice? what does the variable j do? Pwede pa ba tong ma simplify but still using String Arrays and loops? Thank you po.
import java.util.Scanner;
public class ChineseZodiacs {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] yearSign = {"monkey", "rooster", "dog", "pig", "rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse", "sheep"};
System.out.print("Enter a year: ");
int year = input.nextInt();
System.out.println(zodiacs[year % 12]);
}
}
Pero gusto kong gawin itong Chinese Zodiacs without using modulus and gamitin ang Loops. And i tried googling for examples and i found one pero di ko maintindihan ito yung code:
public static void main(String[] args) {
int year;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a year");
year=sc.nextInt();
String[] yearSign = {"monkey", "rooster", "dog", "pig", "rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse", "sheep"};
int j = 0;
for ( int i = 1000; i <=year; i++) {
if (j == yearSign.length) {
j=0;
}
j++;
}
System.out.println("Your Chinese Zodiac is "+yearSign[j-1]);
Bakit po kailangan mag intialize ng another iteration na j? and bakit kailanga sya i initialize twice? what does the variable j do? Pwede pa ba tong ma simplify but still using String Arrays and loops? Thank you po.
Last edited: