d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Fg For Halp
Add Reply New Topic New Poll
Member
Posts: 35,445
Joined: Apr 28 2006
Gold: Locked
Warn: 10%
Apr 28 2013 02:59pm
i'm taking a javascript class and need some relatively simple help. here's the assignment:
Write, compile, and execute a Java application program that counts down by 2’s
beginning with a user-input number. Your program should follow these steps:
1. Have the user input a positive integer starting value. Validate that they have input a positive
number, giving them as many chances as necessary to do so.
2. Use a while-loop to count down from the starting value by 2’s, outputting one value at a time,
and stopping when the value becomes negative (see example below).
3. Output “DONE”.

what i've done so far is:
public class while
{
public static void main (String [] args)
{

int numTimes, num;

num = Input.readInt("Enter a number: ");
while (num < 0)
numTimes = Input.readInt("Please enter a positive number: ");

while (num > 0)
{
Output.showValue("Number:", tipValues);
count = count - 2;
while (numTimes < 0);

}

}
}

for some reason i got this error 7 times... "while.java:10: illegal start of expression public static void main (String [] args)"

let me know if anyone has ideas
thanks
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Apr 28 2013 03:24pm
ok first off Javascript and Java are two COMPLETELY differents things.
second your code is very messed up, was it supposed to be a draft or something? :rofl:

Here's a possible corrected code, put it in a CountsDownBy2.java file:

Code

import java.util.Scanner;

public class CountsDownBy2 {
public static void main(String[] args) {
 int num = -1;
 Scanner s = new Scanner(System.in);
 while (num < 0) {
  System.out.print("Enter a number: ");
  num = s.nextInt();
 }
 while (num >= 0) {
  System.out.println("Number: " + num);
  num -= 2;
 }
 System.out.println("DONE");
}
}


This post was edited by m0hawk on Apr 28 2013 03:31pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 28 2013 03:28pm
Among other problems, you're using a keyword for your class name.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Apr 29 2013 07:15am
Code
import java.util.Scanner;

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

   /*
    * 1. Have the user input a positive integer starting value. Validate that
    * they have input a positive number, giving them as many chances as
    * necessary to do so.
    */

   int input;

   while ( true ) {
     System.out.println( "Enter a positive integer: " );
     input = new Scanner( System.in ).nextInt( );
     if ( input < 0 ) {
       System.out.println( "Invalid input" );
       continue;
     } else {
       break;
     }
   }

   /*
    * 2. Use a while-loop to count down from the starting value by 2’s,
    * outputting one value at a time, and stopping when the value becomes
    * negative (see example below).
    */

   while ( input >= 0 ) {
     System.out.println( input -= 2 );
   }

   /* 3. Output “DONE”. */
   System.out.println( "DONE" );
 }
}


This post was edited by labatymo on Apr 29 2013 07:17am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll