exercise3_7.java

来自「java程序设计 机械工业出版社 书籍代码」· Java 代码 · 共 41 行

JAVA
41
字号
// Exercise3_5.java: Find total and average of input values
import javax.swing.JOptionPane;

public class Exercise3_7 {
  public static void main(String[] args) {
    int countPositive=0, countNegative = 0;
    int count = 0, total = 0, num;
		 
    do {
      // Read the next data
      String dataString = JOptionPane.showInputDialog(null, 
        "Enter an int value, \nthe program exits if the input is 0",
        "Exercise3_5 Input", JOptionPane.QUESTION_MESSAGE);

      num = Integer.parseInt(dataString);
      
      if (num > 0)
        countPositive++;
      else if (num < 0)
        countNegative++;

      total += num;
      count++;
    }
    while (num != 0);

    // Adjust count
    count--; 
    
    if (count == 0) 
      System.out.println("You didn't enter any number");
    else {
      System.out.println("the number of postives is " + countPositive);
      System.out.println("the number of negatives is " + countNegative);
      System.out.println("the total is " + total);
      System.out.println("the average is " + total * 1.0 / count);
    }
    
    System.exit(0);
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?