Results 1 to 2 of 2
  1. #1
    cliffh is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default Simple java program, need help

    I'm trying to write a Java program that asks the user for how many numbers they want to enter, and will then ask them for that many numbers. For each number (after the first one), then will print whether it is bigger than, smaller than, or the same as the number that came before. It keeps comparing the new number to the original number 1 instead of the previous number. Any suggestions on what is wrong?

    import java.util.*;
    import java.text.*;

    public class proj3
    {
    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);

    System.out.print("How many numbers? ");
    int num = Integer.parseInt(s.nextLine());
    System.out.println();
    int count = 1;
    System.out.print("Enter number 1: ");
    int prevnum = Integer.parseInt(s.nextLine());
    System.out.println();
    while (count < num) {
    System.out.print("Enter number " + (count +1) + ": ");
    int currnum = Integer.parseInt(s.nextLine());
    if (currnum > prevnum){
    System.out.println(currnum + " is bigger than " + prevnum);
    count = count + 1;
    currnum = prevnum;
    }
    else if (currnum< prevnum){
    System.out.println(currnum + " is smaller than " + prevnum);
    count = count + 1;
    currnum = prevnum;
    System.out.println();
    }
    else {
    System.out.println(currnum + " is the same as " + prevnum);
    count = count + 1;
    currnum = prevnum;
    System.out.println();
    }

    }


    }
    }

  2. #2
    cliffh is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    fixed it. needed to set prevnum = currnum not currnum = prevnum

Similar Threads

  1. Simple Java program
    By Rolle in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 04:05 PM
  2. [SOLVED] Need help with simple Java program
    By hungdukie in forum New To Java
    Replies: 5
    Last Post: 11-26-2008, 03:17 AM
  3. Simple Java probablitity program?
    By nothing4me in forum New To Java
    Replies: 6
    Last Post: 11-12-2008, 06:16 AM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •