java—一个使用switch语句计算和显示所选图书类型总价的程序

ldfqzlk8  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(328)

一个在线图书销售商以以下价格出售四种图书类型。
书籍类型1:£ 15.98
书籍类型2:£ 12.50
书籍类型3:£ 11.98
书籍类型4:£ 17.49
编写一个程序,输入如下。
图书类型
书籍数量
您的程序应该使用switch语句,计算并显示所选图书类型的总价。
所以我对java还很陌生,只是在学习。我已经学会了如何用java编写switch case。但我还是不太明白。任何提示或想法都将不胜感激。
到目前为止我得到的是!我不确定我所做的是问题在问我什么。

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");
       }
}
vojdkbi0

vojdkbi01#

非常感谢你,维比。我真是个笨蛋。这是我最后得到的密码。而且它工作得完美无瑕。

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");
       }

       }
}
m4pnthwp

m4pnthwp2#

根据你的问题,这可能对你有帮助

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");
}

}
如果您需要从程序中获得更多功能,可以对此进行进一步的更改

相关问题