Can You Please Help Me with this "while loop program"?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aprilmae36
    New Member
    • Nov 2006
    • 2

    #1

    Can You Please Help Me with this "while loop program"?

    Nice day to all... I'm new in java programming and I'm having some problem with this program. The problem is this: Write a program that will read an unspecified number of non-zero integers and determine how many positive and negative values have been read. The program will abort when the user input a zero.

    I've already made a program but I still can't get it right. I deeply appreciate if you will help me.

    Here's the program I made:


    import javax.swing.JOp tionPane;

    public class counter

    {

    public static void main(String [] args)

    {

    String num;

    int count;

    int number;

    count = 0;

    number = 0;

    num = JOptionPane.sho wInputDialog(nu ll, "Enter your first integer:");

    int inputNumber = Integer.parseIn t(num);

    while (inputNumber!=0 )

    {

    if (inputNumber > 0)

    count ++;

    else

    number ++;

    num = JOptionPane.sho wInputDialog(nu ll, "Enter another integer, or enter 0 to end:");

    }

    if (count == 0)

    {

    JOptionPane.sho wMessageDialog( null, "Program aborting.");

    }

    else

    {

    JOptionPane.sho wMessageDialog( null, "There are " + count + " positive integers." );

    JOptionPane.sho wMessageDialog( null, "There are " + number + " negative integers." );

    }

    }

    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by aprilmae36
    Nice day to all... I'm new in java programming and I'm having some problem with this program. The problem is this: Write a program that will read an unspecified number of non-zero integers and determine how many positive and negative values have been read. The program will abort when the user input a zero.

    I've already made a program but I still can't get it right. I deeply appreciate if you will help me.

    Here's the program I made:


    import javax.swing.JOp tionPane;

    public class counter

    {

    public static void main(String [] args)

    {

    String num;

    int count;

    int number;

    count = 0;

    number = 0;

    num = JOptionPane.sho wInputDialog(nu ll, "Enter your first integer:");

    int inputNumber = Integer.parseIn t(num);

    while (inputNumber!=0 )

    {

    if (inputNumber > 0)

    count ++;

    else

    number ++;

    num = JOptionPane.sho wInputDialog(nu ll, "Enter another integer, or enter 0 to end:");

    }

    if (count == 0)

    {

    JOptionPane.sho wMessageDialog( null, "Program aborting.");

    }

    else

    {

    JOptionPane.sho wMessageDialog( null, "There are " + count + " positive integers." );

    JOptionPane.sho wMessageDialog( null, "There are " + number + " negative integers." );

    }

    }

    }

    Code:
     import javax.swing.JOptionPane; 
    public class counter
    {
    public static void main(String [] args)
    {
    
    int negative = 0;
    int positive = 0;
    String num = JOptionPane.showInputDialog(null, "Enter your first integer:");
    int inputNumber = Integer.parseInt(num);
    while (inputNumber != 0)
    {
    if (inputNumber > 0)
    positive ++;
    else
    negative ++;
    num = JOptionPane.showInputDialog(null, "Enter another integer, or enter 0 to end:");
     inputNumber = Integer.parseInt(num);
    }
    JOptionPane.showMessageDialog(null, "Program finished.\n" +
    "There are " + positive + " positive integers.\n"+
    "There are " + negative + " negative integers.");
     
     
    
    }
    }
    Like this?

    Comment

    Working...