Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Print Goodbye

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Print Goodbye

    Hi I was wondering if someone could tell me how to get my program to System.out.println ('"Goodbye") if a negative number is entered. It does everything perfectly for the average but when the user enters a negative number I want it to display the goodbye message. I tried placing it in the program but then it displays "goodbye" and also "the average is Nan" which I don't want it to that just "goodbye." Any help would be appreciated. Thanks.
    import java.util.Scanner;
     
    public class While {
     
      public static void main(String[] args) {   
        Scanner input = new Scanner(System.in); 
     
        int num; // 
        int numOfAmount = 0; 
        float totalAmout = 0;    float average;		
        System.out.println("Enter a score or a negative number to end the program: "); //prompt the user for an input
        num = input.nextInt();
     
        while(num >= 0)
        {
    	numOfAmount++; //
    	totalAmount = totalAmount + score; 
     
    	System.out.println("Enter a number or a negative number to end the program: "); //
    	num = input.nextInt();
        }
     
        average = totalAmount / numOfAmount; //calculate the average score
        System.out.println("The average score is " + average); 
      }
    Last edited by pslick29; December 12th, 2010 at 03:24 AM.


  2. #2
    Junior Member
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Print Goodbye

    I don't quite understand what you're trying to do, but from what i understand this is what you're looking for:

    import java.util.Scanner;
     
    public class While {
     
      public static void main(String[] args) {   
        Scanner input = new Scanner(System.in); 
     
        int num; // 
        int numOfAmount = 0; 
        float totalAmount = 0;
        float average;     
        System.out.println("Enter a score or a negative number to end the program: "); //prompt the user for an input
        num = input.nextInt();
     
        while(num >= 0)
        {
        numOfAmount++; //
        totalAmount = totalAmount + num; 
     
        System.out.println("Enter a number or a negative number to end the program: "); //
        num = input.nextInt();
    	    if (num >=0) {
    	    	average = totalAmount / numOfAmount; //calculate the average score
    	    	System.out.println("The average score is " + average);
    	    }
        }
        System.out.println("Goodbye");
      }
    }

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Print Goodbye

    Hi sorry I should clarify. I need the program to calculate the average only when at least one number is entered and shows “goodbye” if no numbers are entered (the user enters a negative number for the first input). Right now it calculates the average but I need it to output "goodbye" if the first input is a negative number.

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Print Goodbye

    oh, ok then try this:
     
    import java.util.Scanner;
     
    public class While {
     
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		int num;
    		int numOfAmount = 0;
    		float totalAmount = 0;
    		float average;
    		System.out.println("Enter a score or a negative number to end the program: ");
    		num = input.nextInt();
     
    		while (num >= 0) {
    			numOfAmount++; //
    			totalAmount = totalAmount + num;
    			System.out.println("Enter a number or a negative number to end the program: ");
    			num = input.nextInt();
    		}
    		if (numOfAmount >= 1) {
    			average = totalAmount / numOfAmount;
    			System.out.println("The average score is " + average);
    		} else {
    			System.out.println("Goodbye");
    		}
    	}
    }

Similar Threads

  1. 500 Ways to Print 1 to 10
    By Freaky Chris in forum The Cafe
    Replies: 132
    Last Post: Yesterday, 06:47 AM
  2. Print out a JFrame
    By ellias2007 in forum AWT / Java Swing
    Replies: 8
    Last Post: June 17th, 2010, 06:15 AM
  3. print space
    By brainTuner in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 1st, 2010, 06:09 PM
  4. Can someone please tell me why my code doesn't print out anything?
    By deeerek in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 6th, 2010, 07:35 AM
  5. print hex decimal value
    By ran830421 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 25th, 2009, 06:23 PM