当我输入itemType时,它如何实现它打印出的实际类型而不仅仅是Int

时间:2017-10-31 13:22:58

标签: java

package Homework;

import java.util.Scanner;

public class Homework2LibraryFines {

public static void main(String[] args) {
    // Variables
    int cardNumber;
    int age;
    int overdue;
    int itemType;
    double overdueFine;
    int fineAdjustment;
    int books = 1;
    int magazines = 2;
    int dvd = 3;

    Scanner input = new Scanner(System.in);

    System.out.println("Enter patron's library card number");
    cardNumber = input.nextInt();

    System.out.println("Enter patron's age");
    age = input.nextInt();

    System.out.println("Enter item type 1.Book, 2.Magazine, 3.DVD");
    itemType = input.nextInt();

    System.out.println("Enter number of days overdue");
    overdue = input.nextInt();

    System.out.println("Enter overdue fine");
    overdueFine = input.nextDouble();

    System.out.println("Enter fine adjustment");
    fineAdjustment = input.nextInt();
    if (overdueFine == books) {
        overdueFine += 0.50;
    } else if (overdueFine == magazines) {
        overdueFine += 0.25;
    } else if (overdueFine == dvd) {
        overdueFine += 1.50;
    } else if (age > 70) {
        overdueFine += 0;
    } else if (age >= 6 && age <= 17) {
        overdueFine += 1.00;
    }

    System.out.println(cardNumber + " is " + age + " years old " + "and has an overdue " + itemType + "." + "The "
            + itemType + " is " + overdue + " days overdue");

}

}

  

输出

     

输入顾客的图书馆卡号
      222222个
      输入顾客的年龄
      30个
      输入项目类型1.Book,2.Magazine,3.DVD
      3
      输入逾期天数
      10个
      输入逾期罚款
      2.00
      输入微调
      3
  222222年龄为30岁,已逾期3天.3岁是逾期10天

5 个答案:

答案 0 :(得分:1)

不是很漂亮,但它有效:

string itemName = "";

if (itemType == 1) itemName = "Book";
else if (itemType == 2) itemName = "Magazine";
else if (itemType == 3) itemName = "DVD";

当打印出来时,只需用 itemName 替换 itemType

答案 1 :(得分:1)

您可以添加switch语句以获取实际的String值,具体取决于输入的数字

String item="Book";//making Book the default option
        switch (itemType) {
            case 1:
                item = "Book";
                break;
            case 2:
                item = "Magazine";
                break;
            case 3:
                item = "DVD";
                break;
        }

并在item声明中打印print

甚至有点丑陋的ternary条件

String item = itemType == 1 ? "Book" : itemType == 2 ? "Magazine" : itemType == 3 ? "DVD" : "Book";

答案 2 :(得分:0)

一种选择是制作switch声明。

itemType变量存储选项,第二个变量存储string

String itemT = "";
switch(itemType)
{
    case 1:
        itemT = "book"
        break;
    case2:
        ...
}

答案 3 :(得分:0)

除了已建议的ifswitch解决方案外,您还可以创建enum。这是一种价值有限的特殊类。您可以覆盖toString()方法以打印出您想要的任何内容。

答案 4 :(得分:0)

您可以声明itemTypes和数组,并在打印时将用户输入用作数组中的偏移量。

用于存储,

String itemTypesArr[] = {"", "Book", "Magazine", "DVD"};

现在,itemTypesArr[1]引用"Book",依此类推。

用于打印,

System.out.println(cardNumber + " is " + age + " years old " + "and has an overdue " + itemTypesArr[itemType] + "." + "The " + itemTypesArr[itemType] + " is " + overdue + " days overdue");