Carmenia Bona

Greenhorn
+ Follow
since Aug 12, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Carmenia Bona

Winston Gutkowski wrote:

Carmenia Bona wrote:This is what I'm getting:...


Carmenia,

A really good idea when you run into problems like this is to StopCoding (←click).

Then, you have two choices (both of which require a pencil and LOTS of paper):
1. Go through your code and write down, for each iteration of the loop, the values of each variable. An alternative is to print them all out with System.out.println() statements.
2. Forget the code altogether and write down in English (or your native language) the exact steps required to get the result you want; and then check those steps against what you've actually coded.

Personally, I prefer number 2; but the best time to do it is before you write any Java code at all.

HIH

Winston




Thanks, Winston. Yes, I do want to learn on my own. I will try the second method you are suggesting.
11 years ago

K. Tsang wrote:What are you getting?



This is what I'm getting:


The product is 20
The count is 4
11 years ago
Hi. I'm trying to write a code that will do this: 1*2*3*4...

But, I can't seem to get it. My code keeps giving me the wrong answer so I know there's something wrong with it. Can someone help me?

Here's what I have:

public class prod1 {
public static void main(String[] args) {
//int sum = 0;
int product = 0;
int count = 0;
int lowerbound = 1;
int upperbound = 4;

int number = lowerbound;

do {
int a = number;
//int b = number + 1;
product = a * (a + 1);
++number;
++count;
}

while (number <= upperbound);

System.out.println("The product is " + product);
System.out.println("The count is " + count);

}
}

11 years ago
Hi, I am trying to teach myself how to code in Java. Found a few sites with exercises but won't harm to have a few more. Does anyone know where I can find more exercises? Thanks!
11 years ago

Ramesh Kumar Muthukumar wrote:average = (double) sum / upperbound;

this also works.




thanks. i will try this next time. :)
11 years ago

Ramesh Kumar Muthukumar wrote:

average = sum / upperbound;



both the 'sum' and 'upperbound' are int values and the result also will be int and then it is converted into double.

that is why you get 50.0 instead of 50.5.

There are many ways to get the expected output. one of them is changing the upperbound and lowerbound datatype to double.



Thanks you! It worked. May I know what are the other ways?
11 years ago
Hi, I am really new in Java programming and I am doing some exercises in Java.
The program is supposed to display the sum and average. Like this:

The sum is 5050
The average is 50.5

But my code won't display the decimal.

Can somebody help me?

Here's my code:


public class sumandaverage {
public static void main(String[] args) {
int sum = 0;
double average = 00.0;
int lowerbound = 1;
int upperbound = 10;

for (int number = lowerbound; number <= upperbound; ++number) {
sum += number;
}

average = sum / upperbound;

System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
}
}



Thanks so much!!!
11 years ago