• Index
  •  » Java
  •  » Java code - Please see this code

#1 2006-09-21 06:41:21

xreddawg909x
New member
Ranking
Registered: 2006-09-21
Posts: 2
Expertise

Java code - Please see this code

If someone could look over this code i wrote and see if its done correctly i would greatly appreciate it.. i use netbeans and when i click "run" the ms dos window doesn't come up and run the program like dev c does for c++

its supposed to ask for integers and add'em up and give a sum at the end, it shouldn't accept negative integers.

Code::

import java.util.Scanner;


public class Main 
{
    
    /** Creates a new instance of Main */
    public Main() 
    {
    
    
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        // TODO code application logic here
       Scanner input = new Scanner( System.in );
        
        int hold, sum,option,count;
        sum = 0;
        do 
        {    
        count = 0;    
        hold = 0;
        System.out.print("Please enter non-negative integer to be added: ");
        hold = input.nextInt();
            while(hold<0)
            {    
              System.out.print("That number is negative, please enter another one");
              hold = input.nextInt();
            }
        sum = hold + sum;
        System.out.print("Do you wish to add another integer?(1 - yes 2 - no: ");
        option = input.nextInt();
        count = count +1;
        }while(option == 1);
    System.out.printf("The sum of the ", count,"integer(s) is ", sum);
    
    }
    
}

Offline

 

#2 2006-09-24 05:56:16

sachin
Moderator
RankingRanking
Nepal
From: Kathmandu
Registered: 2006-09-18
Posts: 45
Expertise

Re: Java code - Please see this code

xreddawg909x wrote:

If someone could look over this code i wrote and see if its done correctly i would greatly appreciate it.. i use netbeans and when i click "run" the ms dos window doesn't come up and run the program like dev c does for c++

its supposed to ask for integers and add'em up and give a sum at the end, it shouldn't accept negative integers.

Code::

import java.util.Scanner;


public class Main 
{
    
    /** Creates a new instance of Main */
    public Main() 
    {
    
    
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        // TODO code application logic here
       Scanner input = new Scanner( System.in );
        
        int hold, sum,option,count;
        sum = 0;
        do 
        {    
        count = 0;    
        hold = 0;
        System.out.print("Please enter non-negative integer to be added: ");
        hold = input.nextInt();
            while(hold<0)
            {    
              System.out.print("That number is negative, please enter another one");
              hold = input.nextInt();
            }
        sum = hold + sum;
        System.out.print("Do you wish to add another integer?(1 - yes 2 - no: ");
        option = input.nextInt();
        count = count +1;
        }while(option == 1);
    [b]System.out.printf("The sum of the ", count,"integer(s) is ", sum);[/b]
    
    }
    
}

Please try replacing bold line with below.
System.out.println("The sum of the "+ count+" integer(s) is " + sum);

Do let me know if this works.

Thanks
Sachin

Offline

 
  • Index
  •  » Java
  •  » Java code - Please see this code

Board footer

OPML feedsRSS feeds



Powered by WWWThreads Forum
© Copyright 2006, WWWThreads