Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Ohh so simply problem????

Options
  • 17-12-2003 7:34pm
    #1
    Closed Accounts Posts: 1,637 ✭✭✭


    All sorted:

    Thanks OfflerCrocGod.....


Comments

  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    show us your attempts so far


  • Registered Users Posts: 2,671 ✭✭✭Darwin


    Hi,
    I suggest you first construct an algorithm to solve the problem and post the
    results of your efforts. The worst thing you can do is sit down and start
    hacking away at a solution without thinking clearly about the task at hand.
    A well written algorithm can be mapped to any programming language
    with the minimum of effort (the amount of effort expended here depends on your
    proficiency with the language).


  • Closed Accounts Posts: 1,637 ✭✭✭joePC


    OK heres the code for the total, This works grand,

    //creator: joe90kane

    import javax.swing.JOptionPane;

    public class Q3 {


    public static void main(String[] args) {


    String item,NUM;
    int i;
    int itemint;
    int total;
    int totalint;

    totalint = 0;

    item = JOptionPane.showInputDialog(
    "Enter number of Items ");

    itemint = Integer.parseInt( item );

    for(i = 0; i < itemint; i++){

    NUM = JOptionPane.showInputDialog(
    "Enter Cost of item:" );

    total = Integer.parseInt( NUM );

    totalint = totalint + total;

    }

    JOptionPane.showMessageDialog( null,
    "Total Cost " + totalint, "Results",
    JOptionPane.PLAIN_MESSAGE );

    System.exit( 0 );
    }

    }


    getting the number of items & lowest costing item is giving me the problem.

    Thanks joe.......


  • Registered Users Posts: 6,310 ✭✭✭OfflerCrocGod


    Originally posted by joePC

    NUM = JOptionPane.showInputDialog("Enter Cost of item:" );

    total = Integer.parseInt( NUM );

    totalint = totalint + total;



    getting the number of items & lowest costing item is giving me the problem.

    Thanks joe.......

    Initialise total as 0 then make a variable called let's say 'smallest'; int smallest;(initiliase it with the first value from when i is 0) then make a temp variable to place the current item value into;-> int temp;

    temp = Integer.parseInt( NUM );

    is what it will be now. Then just compare temp (present item value) with smallest if its less then place it into smallest and add it if not thenjust add it.


  • Registered Users Posts: 6,310 ✭✭✭OfflerCrocGod


    import javax.swing.JOptionPane;

    public class Q3 {

    public static void main(String[] args) {

    String item,NUM;
    int itemint;
    int totalint = 0;
    int smallest;
    int temp;

    item = JOptionPane.showInputDialog("Enter number of Items ");
    itemint = Integer.parseInt( item );

    NUM = JOptionPane.showInputDialog("Enter Cost of item:" );
    smallest = Integer.parseInt( NUM );
    totalint = totalint + smallest;

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

    NUM = JOptionPane.showInputDialog("Enter Cost of item:" );
    temp = Integer.parseInt( NUM );
    if(temp < smallest) smallest = temp;

    totalint = totalint + temp;

    }

    JOptionPane.showMessageDialog( null, "Total Cost: " + totalint + " Smallest " + smallest ,"Results ", JOptionPane.PLAIN_MESSAGE );
    System.exit( 0 );
    }

    }


    That should do it tidy it up, it's a bit messy.


  • Advertisement
  • Closed Accounts Posts: 1,637 ✭✭✭joePC


    Thanks for that, I see where I was going wrong

    Thanks joe........


  • Closed Accounts Posts: 358 ✭✭CH


    Java Ohh so simply problem????
    1. what sort of a subject title is that?
    2. use the [ c o d e ] tags!
    3. don't delete your original question!


  • Closed Accounts Posts: 1,637 ✭✭✭joePC


    It got you attention didn't it?????????????


Advertisement