让Java识别用户输入

让Java识别用户输入,java,Java,我正在尝试制作一个虚拟商店程序。概要是如果用户在任何时候输入“q”,程序应该退出 用户输入“c”后,要求用户输入两个字符的状态,如CA、NV、WA。如果代码是其他的 如果输入这三个,则属于“其他”。然后显示购物车中的内容和计算出的 基于折扣和含税的总价 问题是程序会向用户询问一次商品和数量,然后进入结帐模式。每当我输入“q”时,它就转到下一行 代码如下: import java.util.Scanner; import java.text.DecimalFormat; import java.t

我正在尝试制作一个虚拟商店程序。概要是如果用户在任何时候输入“q”,程序应该退出

用户输入“c”后,要求用户输入两个字符的状态,如CA、NV、WA。如果代码是其他的

如果输入这三个,则属于“其他”。然后显示购物车中的内容和计算出的 基于折扣和含税的总价

问题是程序会向用户询问一次商品和数量,然后进入结帐模式。每当我输入“q”时,它就转到下一行

代码如下:

import java.util.Scanner;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;
import java.io.*;


public class virtualStore {


    public static void main(String [] args) throws IOException{

        /* If the user enters an item number, the program should ask the user to enter how many of that item
        they want, and then print the menu again. If at any time the user enters 'q', the program should quit.
        Once the user enters 'c', ask the user to enter a 2-character state such as CA, NV, WA. If a code other
        than these three is entered, it falls under "other". Then display what is in their cart and the calculated
        total price based on discounts and include the tax. You should use the Math.round() and
        System.out.printf methods to round numbers and to display to 2 decimal places. */

        Scanner keyboard = new Scanner (System.in);
        String input;
        char c = ' ';
        char q = ' ';

        //tax rates
        double CAtaxRate = .09;
        double NVtaxRate = .07;
        double WAtaxRate = .065;
        double other = .06;
        double tax = 0; 

        //checkout
        double checkout = 0;
        double total;
        double cash = 0;
        double change = 0;
        //items
        double mushrooms = 0.3;
        double onions = 0.6;
        double watermelon = 2.5;
        double cookies = 1;
        int item = 0;

        //number of items
        int numMushrooms = 0;
        int numOnions = 0;
        int numWatermelon = 0;
        int numCookies = 0;



        DecimalFormat dollar = new DecimalFormat("#,##0.00");
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to Alex's Store."
                + " Here is the menu. "
                + "\nEnter the item number to add it to your cart,"
                + " enter \'c\' to checkout or \'q\' to quit. ");

        while(true)
        //while(c != 'c' && c != 'q')

        {

            for (item = 1; item < 4; item ++){
            System.out.println("Item\t\t\tPrice\t\t\tQuantity");
            System.out.println("---------------------------------------------------------");
            System.out.println("1. Mushrooms\t\t($0.30/$0.25)\t\t" + numMushrooms);
            System.out.println("2. Onions\t\t($0.60/$0.50)\t\t" + numOnions );
            System.out.println("3. Watermelon\t\t($2.50/$2.00)\t\t" + numWatermelon);
            System.out.println("4. Cookies\t\t($1.00/$0.75)\t\t" + numCookies);
            System.out.println("---------------------------------------------------------");


            System.out.println("Enter item number between 1 through 4");
            System.out.println("or enter 'c' for checkout or 'q' for quit.");
            input = keyboard.nextLine();
            item = Integer.parseInt(input);




            if (input.equals("1")) 
            {
                System.out.println("Enter how many items you want.");
                String m = keyboard.nextLine();
                numMushrooms = Integer.parseInt(m); 

            }
            else if (input.equals("2"))
            {
                System.out.println("Enter how many items you want.");
                String o = keyboard.nextLine();
                numOnions = Integer.parseInt(o);

            }
            else if (input.equals("3"))
            {
                System.out.println("Enter how many items you want.");
                String w = keyboard.nextLine();
                numWatermelon = Integer.parseInt(w);                

            }
            else if(input.equals("4"))
            {
                System.out.println("Enter how many items you want.");
                String co = keyboard.nextLine();
                numCookies = Integer.parseInt(co);

            }

            }





        if (numMushrooms  > 10)
        {
            mushrooms = 0.25;
        }

        else if (numOnions > 10)
        {
            onions = 0.5;
        }
        else if (numWatermelon > 10)
        {
            watermelon = 2;
        }
        else if (numCookies > 10)
        {
            cookies = .75;
        }

        // quit option
        String quit = scanner.nextLine();
        q = quit.charAt(0);

        if (quit.equalsIgnoreCase("q"))
        {   
            System.out.println("Goodbye");
            scanner.close();
            System.exit(0);
        }

        // checkout option
        String ch = scanner.nextLine();
        c = ch.charAt(0);

        if (ch.equalsIgnoreCase("c"))
        {


            //checkout
            checkout =  numMushrooms * mushrooms + numOnions * onions + numWatermelon * watermelon + numCookies * cookies;


            //tax
            total = tax * checkout + checkout;


            System.out.print("Enter state abbreviations: ");
            input = keyboard.nextLine();


            PrintWriter outputfile = new PrintWriter("receipt.txt");
            outputfile.println("Your cart: ");
            outputfile.println("Sub total:$ " + dollar.format(checkout));

            if (input.equalsIgnoreCase("CA"))
            {
                total = CAtaxRate * checkout + checkout;
                outputfile.println("Tax Rate: " + CAtaxRate);
                outputfile.println("Tax: $" + dollar.format(CAtaxRate * checkout));
                outputfile.println("Total is: $" + dollar.format(total));
            }   

            else if (input.equalsIgnoreCase("NV"))
            {
                total = NVtaxRate * checkout + checkout;
                outputfile.println("Tax Rate: " + NVtaxRate);
                outputfile.println("Tax: $" + dollar.format(NVtaxRate * checkout));
                outputfile.println("Total is: $" + dollar.format(total));

            }

            else if (input.equalsIgnoreCase("WA"))
            {
                total = WAtaxRate * checkout + checkout;
                outputfile.println("Tax Rate: " + WAtaxRate);
                outputfile.println("Tax: $" + dollar.format(WAtaxRate * checkout));
                outputfile.println("Total is: $" + dollar.format(total));

            }

            else if (input.equalsIgnoreCase(input))
            {
                total = other * checkout + checkout;
                outputfile.println("Tax Rate: " + other);
                outputfile.println("Tax: $" + dollar.format(other * checkout));
                outputfile.println("Total is: $" + dollar.format(total));
            }

            outputfile.println("-----------------------------------------------------");
            input = keyboard.nextLine();
            cash = Integer.parseInt(input);
            outputfile.println("Enter amount of cash: $" + dollar.format(cash));

            change = cash - total;
            outputfile.println("Change due: $" + dollar.format(change));


            outputfile.println("Thank you for shopping at Alex's store!");

            outputfile.close();

        FileWriter fw = new FileWriter("receipt.text", true);
        PrintWriter pw = new PrintWriter(fw);
        pw.println("C:\\Desktop\\Receipt.txt ");
        pw.close();
        } 







    }

}

}
import java.util.Scanner;
导入java.text.DecimalFormat;
导入java.text.NumberFormat;
导入java.util.Random;
导入java.io.*;
公共类虚拟存储{
公共静态void main(字符串[]args)引发IOException{
/*如果用户输入项目编号,程序应要求用户输入该项目的数量
他们需要,然后再次打印菜单。如果用户在任何时候输入“q”,程序应退出。
用户输入“c”后,要求用户输入两个字符的状态,如CA、NV、WA。如果代码为其他
如果输入了这三个,则它将落在“其他”下。然后显示他们购物车中的内容和计算的结果
基于折扣和含税的总价。应使用Math.round()和
System.out.printf方法将数字四舍五入并显示到小数点后2位*/
扫描仪键盘=新扫描仪(System.in);
字符串输入;
字符c='';
字符q='';
//税率
双卡他速率=.09;
双倍税率=.07;
双瓦税率=.065;
双倍其他=.06;
双重税=0;
//结帐
双重检验=0;
双倍总数;
双倍现金=0;
双变=0;
//项目
双蘑菇=0.3;
双洋葱=0.6;
双西瓜=2.5;
双cookies=1;
int项=0;
//项目数
int numMushrooms=0;
整数=0;
int numwaterwall=0;
int numCookies=0;
DecimalFormat dollar=新的DecimalFormat(“0.00”);
扫描仪=新的扫描仪(System.in);
System.out.println(“欢迎来到Alex的商店。”
+“这是菜单。”
+\n输入项目编号以将其添加到购物车中
+“输入'c'结帐或'q'退出。”);
while(true)
//而(c!=“c”&&c!=“q”)
{
用于(项目=1;项目<4;项目++){
System.out.println(“Item\t\t\tPrice\t\t\tQuantity”);
System.out.println(“------------------------------------------------------------------”;
System.out.println(“1.蘑菇\t\t($0.30/$0.25)\t\t“+numMushrooms);
System.out.println(“2.Onions\t\t($0.60/$0.50)\t\t“+numOnions);
System.out.println(“3.西瓜\t\t($2.50/$2.00)\t\t“+numwatermellow);
System.out.println(“4.Cookies\t\t($1.00/$0.75)\t\t“+numCookies);
System.out.println(“------------------------------------------------------------------”;
System.out.println(“输入介于1到4之间的项目编号”);
System.out.println(“或输入'c'表示签出,或输入'q'表示退出”);
输入=键盘.nextLine();
item=Integer.parseInt(输入);
如果(输入等于(“1”))
{
System.out.println(“输入您想要的项目数量”);
字符串m=keyboard.nextLine();
numMushrooms=Integer.parseInt(m);
}
else if(输入等于(“2”))
{
System.out.println(“输入您想要的项目数量”);
字符串o=keyboard.nextLine();
numOnions=Integer.parseInt(o);
}
else if(输入等于(“3”))
{
System.out.println(“输入您想要的项目数量”);
字符串w=keyboard.nextLine();
numwatermellow=Integer.parseInt(w);
}
else if(输入等于(“4”))
{
System.out.println(“输入您想要的项目数量”);
String co=keyboard.nextLine();
numCookies=Integer.parseInt(co);
}
}
如果(numMushrooms>10)
{
蘑菇=0.25;
}
否则,如果(numOnions>10)
{
洋葱=0.5;
}
否则,如果(数值大于10)
{
西瓜=2;
}
否则如果(numCookies>10)
{
饼干=0.75;
}
//退出选项
String quit=scanner.nextLine();
q=退出字符(0);
if(退出相等信号案例(“q”))
{   
System.out.println(“再见”);
scanner.close();
系统出口(0);
}
//签出选项
字符串ch=scanner.nextLine();
c=ch.charAt(0);
if(ch.equalsIgnoreCase(“c”))
{
//结帐
结帐=香菇+香菇+洋葱+香瓜*西瓜+香菇*饼干;
//税
合计=税金*结帐+结帐;
系统输出打印(“输入状态缩写:”);
输入=键盘.nextLine();
PrintWriter outputfile=新的PrintWriter(“receipt.txt”);
println(“您的购物车:”);
println(“小计:$”+dollar.format(签出));
if(输入.相等信号情况(“CA”))
{
总计=CAtaxRate*结帐+结帐;
println(“税率:“+CAtaxRate”);
println(“Tax:$”+dollar.format(CAtaxRate*checkout));
println(“总计为:$”+dollar.for
while(true) {

    // Print the menu messages
    String input = scanner.readLine();
    if(input.equals("c")) {

        // Read in and handle state abbreviation

    } else if(input.equals("q")) {
        System.exit(0);
    } else {
        int itemNumber = Integer.parseInt(input);

        // Parse and handle item number
    }
}