如何在java

时间:2017-08-22 21:49:53

标签: java eclipse netbeans console java.util.scanner

我已经搜索了很长时间,以便在我完成一个'客户后,用java清除我的控制台。我正在谈论的是我制作了一些代码,我可以让某人购买'使用我的java代码的东西,但我无法清除控制台或再次启动我的扫描仪。完成使用后。
我的代码在这里:

package restaurantmanagement;

import static java.lang.System.in;
import static java.lang.System.out;
import java.util.Scanner;

public class RestaurantManagement {
    public static void main(String[] args) {
        Scanner sc = new Scanner(in);
        double beefsandwhichcombo = 9.00;
        double chickensandwhichcombo = 10.00;
        double frenchfries = 2.99;
        double eggsandwhich = 6.99;
        double sDrink = 1.50;
        double mDrink = 2.50;
        double lDrink = 3.50;
        out.println("Our menu:");
        out.println("1. Beef Sandwhich Combo - " + beefsandwhichcombo);
        out.println("2. Chicken Sandwhich Combo - " + chickensandwhichcombo);
        out.println("3. French Fries - " + frenchfries);
        out.println("4. Egg Sandwhich - " + eggsandwhich);
        out.println("5. Small Drink - " + sDrink);
        out.println("6. Medium Drink - " + mDrink);
        out.println("7. Large Drink - " + lDrink);
        double Total = 0;
        String whatbuy;
        out.println("What do you want to buy?");
        whatbuy = sc.nextLine();
        if (null != whatbuy) switch (whatbuy) {
            case "1":
                Total = Total + beefsandwhichcombo;
                break;
            case "2":
                Total = Total + chickensandwhichcombo;
                break;
            case "3":
                Total = Total + frenchfries;
                break;
            case "4":
                Total = Total + eggsandwhich;
                break;
            case "5":
                Total = Total + sDrink;
                break;
            case "6":
                Total = Total + mDrink;
                break;
            case "7":
                Total = Total + lDrink;
                break;
            case "No":
                Total = Total;
                break;
            default:
                break;
        }
        String anyelse;
        out.println("Do you want anything else?");
        anyelse = sc.nextLine();
        if (null != anyelse) switch (anyelse) {
            case "1":
                Total = Total + beefsandwhichcombo;
                break;
            case "2":
                Total = Total + chickensandwhichcombo;
                break;
            case "3":
                Total = Total + frenchfries;
                break;
            case "4":
                Total = Total + eggsandwhich;
                break;
            case "5":
                Total = Total + sDrink;
                break;
            case "6":
                Total = Total + mDrink;
                break;
            case "7":
                Total = Total + lDrink;
                break;
            case "No":
                Total = Total;
                break;
            default:
                break;
        }
        String anyelse1;
        out.println("Do you want anything else?");
        anyelse1 = sc.nextLine();
        if (null != anyelse1) switch (anyelse1) {
            case "1":
                Total = Total + beefsandwhichcombo;
                break;
            case "2":
                Total = Total + chickensandwhichcombo;
                break;
            case "3":
                Total = Total + frenchfries;
                break;
            case "4":
                Total = Total + eggsandwhich;
                break;
            case "5":
                Total = Total + sDrink;
                break;
            case "6":
                Total = Total + mDrink;
                break;
            case "7":
                Total = Total + lDrink;
                break;
            case "No":
                Total = Total;
                 break;
            default:
                 break;
        }
        String coupon;
        out.println("Do you have a coupon? Y/N");
        coupon = sc.nextLine();
        if (null != coupon) switch (coupon) {
            case "Y":
                Total = Total / 1.5;
                break;
            case "N":
                Total = Total;
                break;
        }
        double tax = 1.24;
        Total = Total * tax;
        double Totald1 = Total;
        double w1 = Totald1;
        double m1 = w1;
        double y1 = m1;
        out.println("Your total : $" + Total);
        Total = 0;
    }

}

我得到了什么:

Our menu:
1. Beef Sandwhich Combo - 9.0
2. Chicken Sandwhich Combo - 10.0
3. French Fries - 2.99
4. Egg Sandwhich - 6.99
5. Small Drink - 1.5
6. Medium Drink - 2.5
7. Large Drink - 3.5
What do you want to buy?
6
Do you want anything else?
2
Do you want anything else?
3
Do you have a coupon? Y/N
Y
Your total : $12.805066666666667

但我想要的是,如果我完成此操作,我的java代码会看到它说Your total并将在那里保持几秒钟,然后重置。知道怎么做吗?
修改
另外,要想一举两得,任何想法如何让一个双重超过2个小数位进行java测试,然后再将其转到第二个,例如,13.424将变为13.42。 顺便说一下,我正在使用NetBeans和Eclipse。

4 个答案:

答案 0 :(得分:1)

如果使用linux,则可以执行该命令 Runtime.getRuntime().exec("clear");

如果你想设置定时器。阅读有关Timer和TimerTask类的信息

答案 1 :(得分:1)

在Windows上使用Runtime#exec将无效,因为它会在您的终端之外创建一个单独的进程。
它什么都不做。

如果您的终端支持ANSI序列,则只需打印此String"\u001Bc" 大多数终端都会识别这个序列,并清除它自己。

答案 2 :(得分:1)

你可以使用这个:

pick the larger n

答案 3 :(得分:1)

您可以使用它来改变2个十进制数字。

DecimalFormat df = new DecimalFormat("##.##");
total = Double.parseDouble(df.format(total));