一个程序,该程序使用switch语句并计算并显示所选书籍类型的总价格

时间:2018-12-06 10:31:06

标签: java switch-statement

在线图书卖家以以下价格出售四种图书。

  • 图书类型1:£15.98
  • 图书类型2:£12.50
  • 图书类型3:11.98英镑
  • 图书类型4:17.49英镑

编写一个程序,接受如下输入。

  • 图书类型
  • 没有书

您的程序应使用switch语句,并计算并显示所选书籍类型的总价。

所以我是java的新手,只是学习而已。我已经学会了如何在Java中编写开关盒。但是我不能完全解决这个问题。任何提示或想法都将受到赞赏。

这就是我要走的这么远! 我不确定我所做的是我要问的问题。

import java.util.*;
public class onlineBookstore {
    public static void main (String [] Args)
    {
        Scanner x= new Scanner(System.in);
        int a,b,price; 
        String book;
        System.out.println("Enter the Book type");
        book = x.next();
        System.out.println("Enter the Number of books");
        book = x.next();
        
        
       switch(book) {
           case "book1" :
            System.out.println("£ 15.98");
            break;
            case "book2" :
            System.out.println("£ 12.50"); 
            break;
            case "book3":
            System.out.println("£ 11.98 ");
            break;
            case "book4":
            System.out.println("£ 17.49");
            break; 
           default :
            System.out.println("INVALID");
       }
        System.out.println("This is the end of the program");
       }
}

2 个答案:

答案 0 :(得分:0)

根据您的问题提示 这可能会帮助您

public class onlineBookstore {
public static void main(String[] Args) {
    Scanner scanner = new Scanner(System.in);

    String book;
    int noOfBooks;
    System.out.println("Available options are: [book1,book2,book3,book4]");
    System.out.println("Enter the Book type:");
    book = scanner.nextLine();
    System.out.println("Enter the Number of books");
    noOfBooks = scanner.nextInt();

    switch (book) {
    case "book1":
        System.out.println("The total price of the selected book type is : " + noOfBooks * 15.98);
        break;
    case "book2":
        System.out.println("The total price of the selected book type is : " + noOfBooks * 12.50);
        break;
    case "book3":
        System.out.println("The total price of the selected book type is : " + noOfBooks * 11.98);
        break;
    case "book4":
        System.out.println("The total price of the selected book type is : " + noOfBooks * 17.49);
        break;
    default:
        System.out.println("INVALID");
    }
    System.out.println("This is the end of the program");
}

}

如果您需要程序中的其他功能,可以对此进行进一步的更改

答案 1 :(得分:-2)

非常感谢您使用VEBBIE。 我这样的菜鸟。 这是我最终得到的代码。并且它的工作非常完美。

import java.util.*;
public class onlineBookstore {
    public static void main (String [] Args)
    {
        Scanner x= new Scanner(System.in);
        int book;
        System.out.println("Enter the Book type");
        book = x.nextInt();
        int numb;
        System.out.println("Enter the Number of books");
        numb = x.nextInt();
        
       switch(book) {
           case 1 :
            System.out.println("Total price of the book is"+" "+"£"+ numb * 15.98);
            break;
            case 2 :
            System.out.println("Total price of the book is"+" "+"£"+ numb * 12.50); 
            break;
            case 3:
            System.out.println("Total price of the book is"+" "+"£"+ numb * 11.98);
            break;
            case 4 :
            System.out.println("Total price of the book is"+" "+"£"+ numb * 17.49);
            break; 
           default :
            System.out.println("INVALID BOOK TYPE");
       }
       
       }
}