int[] piDigits={3, 1, 4};
return piDigits;
public int[] makePi() {
return null;
}
int[] piDigits={3, 1, 4};
return piDigits;
public int[] makePi() {
int[] piDigits={3, 1, 4};
return piDigits;
}
above passed all the tests. Not sure if something needs to be done with 22/7 which is pi calculation value right?Not sure if something needs to be done with 22/7Why do you think that? It are exercises to let you create and work with arrays. Not to let you calculate pi!
public int[] makePi() {
int[] result = new int[3];
int value = 2200/7;
int divisor = 100;
for (int i=0; i<3; i++) {
result[i] = value / divisor;
value = value % divisor;
divisor /= 10;
}
return result;
}
public int[] makePi() {
int[] result = new int[3];
int value = (int)(Math.PI * 100);
int divisor = 100;
for (int i=0; i<3; i++) {
result[i] = value / divisor;
value = value % divisor;
divisor /= 10;
}
return result;
}
i wonder why we need to multiply by 100 and then devide by hunded and for loop?If you wonder, why don't you just try without doing that? I encourage you to learn by experimenting with the code.
>>>
for (int i=0; i<3; i++) {
result[i] = value / divisor;
value = value % divisor;
divisor /= 10;
}
in this for loop we are getting 3 when i=0
then 1 when i=1
then 4 when i=2
then loop exists right.
I see how first element of array being pouplated by 3 using [quote]result[0] = value / divisor;[/quote]
but not clear on how 1 and 4 being populated into result[1] and result[2]
>>>value = value % divisor;
this results 14 right which is the reminder due to this modulo operation
[b]divisor /= 10;[/b]
what above line means and does?
is it like
divison =divison/10; which is 1?
please advise
result[i] = value / divisor;
public class Test50 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("pi aray us"+makePi().toString());
}
public static int[] makePi() {
int[] result = new int[3];
int value = 2200/7;
int divisor = 100;
for (int i=0; i<3; i++) {
result[i] = value / divisor;
value = value % divisor;
divisor /= 10;
}
return result;
}
}
pass one (i=0)
divisor = 100
314 / 100 = 3
314 % 100 = 14
pass two (i=1)
divisor = 10
14 / 10 = 1
14 % 10 = 4
pass three (i=2)
divisor = 1
4 / 1 = 4
4 % 1 = 0
System.out.println("pi aray us"+makePi().toString());
http://docs.oracle.com/javi was going through above link. Not able to find yet where it says how int rounding happens?ase/specs/ jls/se7/ht ml/jls- 5.h tml#jls-5. 1.3
public class Test50 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("pi aray us"+makePi().toString());
System.out.println("rounding is"+(int)0.6);
}
public static int[] makePi() {
int[] result = new int[3];
int value = 2200/7;
int divisor = 100;
for (int i=0; i<3; i++) {
result[i] = value / divisor;
value = value % divisor;
divisor /= 10;
}
return result;
}
}
/*pass one (i=0)
divisor = 100
314 / 100 = 3
314 % 100 = 14
pass two (i=1)
divisor = 10
14 / 10 = 1
14 % 10 = 4
pass three (i=2)
divisor = 1
4 / 1 = 4
4 % 1 = 0*/
pi aray us[I@74a174a1
rounding is0
Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (ยง4.2.3). Then there are two cases:
Title | # Comments | Views | Activity |
---|---|---|---|
strCopies challenge | 17 | 73 | |
mapBully challenge | 6 | 89 | |
Setting strict transport security header in header not working | 1 | 48 | |
Is there a canned solution for a custom genealogy website | 2 | 37 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
16 Experts available now in Live!