2 Replies - 35599 Views - Last Post: 11 February 2007 - 10:13 PM Rate Topic: -----

#1 elielobt  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 02-February 07

Display Odd Numbers

Posted 02 February 2007 - 03:13 PM

trying to write a for loop to print odd numbers from 1-100

4 errors
oddNumbers.java:10: 'class' or 'interface' expected
cpublic class oddNumbers
^
oddNumbers.java:14: not a statement
for (int i = 0; i++; i < 100){
^
oddNumbers.java:24: 'else' without 'if'
else if
^
oddNumbers.java:31: '}' expected
^
4 errors



public class oddNumbers
{
public static void main (String args[])
{
for (int i = 0; i++; i < 100){
{
if (i%2 != 0){ //If number is odd
System.out.print (i);
if (i != 99){
System.out.print (", "); }
}
else
System.out.print ("."); //Add period after all numbers are written

}
}

This post has been edited by elielobt: 02 February 2007 - 03:38 PM


Is This A Good Question/Topic? 0
  • +

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Display Odd Numbers

Posted 02 February 2007 - 03:17 PM

Please post the errors you are receiving. It makes it easier for our members to debug. Thanks! :)

You have some misplaced braces, I'm afraid. You've also mixed up the syntax for the for loop control.

Your errors:

Quote

oddNumbers.java:10: 'class' or 'interface' expected
cpublic class oddNumbers
^

public does not begin with a c

Quote

oddNumbers.java:14: not a statement
for (int i = 0; i++; i < 100){

This is the for loop syntx I mentioned...see the corrected version below.

Quote

oddNumbers.java:24: 'else' without 'if'
else if
^
oddNumbers.java:31: '}' expected
^

These two errors are the result of extra and misplaced braces, as noted above. See the code below for corrections.

public class oddNumbers
{
public static void main (String args[])
{
for (int i = 0; i < 100; i++)
{
if (i%2 != 0){ //If number is odd
  System.out.print (i);
  if (i != 99){
	 System.out.print (", "); }
  }
}
System.out.print ("."); //Add period after all numbers are written

}
}


Was This Post Helpful? 2

#6 corliss  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 189
  • Joined: 25-October 06

Re: Display Odd Numbers

Posted 11 February 2007 - 10:13 PM

I would do this in a modular way....eg. have a class file and run file
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1