Java >> Java チュートリアル >  >> Java

ユーザーが望むまで数値を入力するJavaプログラムを作成し、最後に正、負、ゼロの数を表示する必要があります

はじめに

このデモでは、デバッグ目的で NetBeans IDE 8.2 を使用しました。ただし、可用性に応じて任意の Java プログラミング言語コンパイラを使用できます..

 
import java.util.Scanner;
 
public class JavaLoopExcercise
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
 
        int number,          
            countPositive = 0, 
            countNegative = 0,
            countZero = 0;
 
        char choice;
 
        do
        {
            System.out.print("Enter the number ");
            number = console.nextInt();
 
            if(number > 0)
            {
                countPositive++;
            }
            else if(number < 0)
            {
                countNegative++;
            }
            else
            {
                countZero++;
            }
 
            System.out.print("Do you want to continue y/n? ");
            choice = console.next().charAt(0);
 
        }while(choice=='y' || choice == 'Y');
 
        System.out.println("Positive numbers: " + countPositive);
        System.out.println("Negative numbers: " + countNegative);
        System.out.println("Zero numbers: " + countZero);
    }  
}

結果


Java タグ