NEWS: Total Picture Views: 151536 | Total Article Views: 161021 | Top 5 Most Popular Article: 1. Am I Stuck Algorithm with Becker Robot | 2. PickUpThings with Becker Robot | 3. How to read a JSON and work with it | 4. Encrypted Data GUI (Download .Zip) | 5. Create Wall and Things with Becker

Ternary - Print total of positive and negative Java

Code:

package ternary;
import java.util.Scanner;
/**
 * Program:Ternary - Print total of positive and negative
 * Author: QuanZhi Huang(Jackie)
 * Date: 2014-10-16
 * Description: 
 *  This program ask user for number in INT, when 0 is entered, the program output
 * the number of positive and negative, also the total
 */
public class Ternary{

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int number;    //the number user enter
        int temp;    //temp, use for nothing
        int pos = 0;    //positive
        int neg = -1;    //number of negative number

        
        
        do{                        //loop
            System.out.print("Enter a number(0 to exit): ");   //promote
            number = input.nextInt();
            
            temp = (number >= 1)? pos++ :neg++;    //if number >= 1 ,pos ++
                                                    //if number < 1, neg ++
            
        }while(number != 0);   //while user input is not 0
        
        System.out.println("Total number entered: "+ (pos+neg) );   //total is equal to pos + neg
        System.out.println("Positive: "+ pos );
        System.out.println("Negative: "+ neg);

       /* Output Results:
       run:
Enter a number(0 to exit): 3
Enter a number(0 to exit): 5
Enter a number(0 to exit): -2
Enter a number(0 to exit): -5
Enter a number(0 to exit): 2
Enter a number(0 to exit): 0
Total number entered: 5
Positive: 3
Negative: 2
       *   End Output Results
       */ 
    }

}
Attachments:

Total Views: 841

jackie588727Created on 10/16/14 and updated on 10/16/14


affiliate_link

Disclosure: We are a website that needs compensation to operate like any other website on the internet.
We may receive consideration for our reviews but we are totally unbiased and do not accept paid reviews or fake reviews claiming to be something they are not.