Question:
Java Program that prints the number 0 through 10, along with their valuse multiplied by 10 and by 100.?
2013-10-12 04:14:11 UTC
If you could help me do this that would be great!

Here is what I have so far

// Multiply.java - This program prints the numbers 0 through 10 along
// with these values multiplied by 10 and by 100.
// Input: None.
// Output: Prints the numbers 0 through 10 along with their values multiplied by 10 and by 100.


public class NewMultiple
{
public static void main(String args[])
{

String head1 = "Number: ";
String head2 = "Multiplied by 10: ";
String head3 = "Multiplied by 100: ";
int numberCounter = 0; // Numbers 0 through 10.
int byTen = numberCounter * 10; // Stores the number multiplied by 10.
int byHundred = numberCounter * 100; // Stores the number multiplied by 100.
final int NUM_LOOPS = 10; // Constant used to control loop.

// This is the work done in the housekeeping() method
System.out.println("0 through 10 multiplied by 10 and by 100" + "\n");

// This is the work done in the detailLoop() method
// Initialize loop control variable.
int count = 0;
// Write your counter controlled while loop here
for (numberCounter = 0; numberCounter < 10; numberCounter++)
{
System.out.println(byTen);
System.out.println(byHundred);
}
System.out.println(head1 + numberCounter);
System.out.println(head2 + byTen);
System.out.println(head3 + byHundred);
// Next number.
// This is the work done in the endOfJob() method
System.exit(0);
} // End of main() method.

} // End of Multiply class.


and the if you run it all it says for the print out is
Process finished with exit code 1

Thanks for the help :)
Three answers:
James Bond
2013-10-12 05:45:05 UTC
System.out.println("10");

System.out.println("100");



for (numberCounter = 0; numberCounter < 10; numberCounter++)

{

byTen=numberCounter*10;

byHundread=numberCounter*100;

System.out.println(head1 + numberCounter);

System.out.println(head2 + byTen);

System.out.println(head3 + byHundred);

}
2013-10-12 15:43:48 UTC
So for example you want to print 1, then 1*10, then 1*100, then print 2, 2*10 and 2*100 etc.?



/* -- This code will loop ten times, with values of i from 1 to 10 -- */

for (int i = 1; i <= 10; i++) {



/* -- This prints the current value of i, then that value x10 and x100 -- */

System.out.println(i.toString() + ", " + (i*10).toString() + ", " + (i*100).toString());

}



This should print an output of:

1, 10, 100

2, 20, 200

3, 30, 300

...

10, 100, 1000





Note: while the .toString() after each i is not strictly necessary, because Java will understand that you're trying to parse the integer as a string when you concatenate it, it's good practice to throw it in.
Sadsongs
2013-10-12 12:03:03 UTC
Any exit value > 0 indicates an error so yes, there's a problem. How to find it: if you don't want to get into symbolic debuggers, try the crude but effective method of commenting out most of the code and seeing what happens. Gradually uncomment bits - that makes it easy to spot problems.



As to the code above - you need calculations to be inside the for loop.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...