Select Page

Big Java学习笔记—— 输入输出input and output

Big Java学习笔记—— 输入输出input and output
[danger]态度声明:本站所有原创学习类文章,只为分享学习乐趣、拓展知识广度、提供讨论平台,不以应试,实用、探讨答案为目的,本站中所有文章中的引用会要求作者尽量标出出处,所有提供下载文件均为个人整理分享学习资料,如果有版权异议,请与博主沟通。本站每学习文必置顶此项,望大家共同监督。乐于分享,支持版权,转载请列明出处,想了解更多……[/danger]


[title]读取输入[/title]

当一个程序要求输入时,应该首先告诉用户要输入什么,这样的信息我们叫做[mark]prompt(命令行界面)[/mark]。

// because java was designed not much attention to reading keyboard input.
import java.util.Scanner;

// Create a Scanner object to read keyboard input.
Scanner in = new Scanner(System.in);

// Display a prompt in the console window.
System.out.print(“Please enter the number of bottles: ”);

// The program waits for user input, then places the input into the variable.
// Define a variable to hold the input value.
int bottles = in.nextInt();

[title]输出的格式[/title]

输出格式的声明格式:System.out.printf(“prompt: format specifier”, variable);

[info]关于格式声明:%预存总位数.显示总位数显示类型。d表示整形,s表示字符串,f表示浮点型[/info][warning]注意:数位要从零开始。[/warning]

下面,我们结合日常生活中常用的单价计算,进行可乐大小包装间的比价事例:

import java.util.Scanner;

/**
   This program prints the price per liter for a six-pack of cans and a two-liter bottle.
*/
public class Volume
{
   public static void main(String[] args)
   {
      // read price per pack.
      Scanner in = new Scanner(System.in);
      
      System.out.print(“Please enter the price for a six-pack ”);
      double packPrice = in.nextDouble();
      
      // read price per bottle.
      System.out.print(“Please enter the price for a two-liter bottle: ”);
      double bottlePrice = in. NextDouble();
      
      // constant Deklaration.
      final double CANS_PER_PACK = 6;
      final double CAN_VOLUME = 0.355;
      final double BOTTLE_VOLUME = 2;
      
      // Compute and print price per liter.
      double packPricePerLiter = packPrice / (CANS_PER_PACK * CAN_VOLUME);
      double bottlePricePerLiter = bottlePrice / Bottle_VOLUME;
      
      System.out.printf(“Pack price per liter: %8.2f”, packPricePerLiter);
      System.out.println();
      
      System.out.printf(“Bottle price per liter: %8.2f”, bottlePricePerLiter);
      System.out.println();
   }
}

很多程序都需要数学计算,这一部分我们介绍如何把问题描述编程原始需求和程序。我们以一个自动售卖机的算账和找零功能,并实现具体硬币的找零,假设最小单位都是25分。

  1. 首先,理解这个问题,什么是输入,什么是想要的输出?在这个例子里,存在两部分的输入:买的东西的价值和顾客付款的价值;输出也主要是两个部分:一个是整币的数量,一个是分币的数量。
  2. 自己手动计算这个问题,很简单了;
  3. 转换成描述语言进行算法描述:dollar coins = change due / 100 without remainder, change due = change due – 100* dollar coins然后再除以,取整留余。
  4. 进入之前介绍过的环节了,我们来确定变量和常量了,我们需要5个变量:billValve,itemPrice,changeDue,dollarCoins, quarters. 和两种币值的常量。
  5. 用变量表示算法,确定函数的核心部分,
  6. 按照流程设计输入输出的环节:
  7. 最后实现环节:
    import java.util.Scanner;
    
    /**
       This program simulates a vending machine and give changes.
    */
    public class VendingMachine
    {
       public static void main(String[] args)
       {
          Scanner in = new Scanner(System.in);
          
          final int PENNIES_PER_DOLLAR = 100;
          final int PENNIES_PER_QUARTER = 25;
          
          System.out.print(“Enter bill value (1 = $1 bill, 5 = $5 bill, etc.):”);
          int billValue = in.nextInt();
          System.out.print(“Enter item price in pennies: ”);
          int itemPrice = in.nextInt();
          
          // Compute change due
          
          int changeDue = PENNIES_PER_DOLLAR * billValue - itemPrice;
          int dollarCoins = changeDue / PENNIES_PER_DOLLAR;
          changeDue = changeDue % PENNIES_PER_DOLLAR;
          int quarters = changeDue / PENNIES_PER_QUARTER;
          
          // Print change due
          System.out.printf(“Dollar coins: %6d”, dollarCoins);
          System.out.println();
          System.out.printf(“Quarters:      %6d”, quarters);
          System.out.println();
       }
    }

    [success]重要的是步骤和思路,步骤和思路,步骤和思路。[/success]

About The Author

乐观的无头苍蝇

无论什么情况下,每天都要对自己说:“Guten Morgen!”