所以我试图制作一个像收银员一样的程序,该程序不断增加取决于用户购买产品的数量,但是我的程序不会增加价值,但它会不断替换它
班级:
idx=any(cell2mat(cellfun(@isempty,raw,'UniformOutput',false)),2)
raw(idx,:)=[]
idx=any(cell2mat(cellfun(@isnan,raw,'UniformOutput',false)),2)
raw(idx,:)=[]
主类:
public class Class {
private double drink, pdrink;
public void SetDrink (double drink)
{
this.drink = drink;
}
public void SetPDrink (double pdrink)
{
this.pdrink = pdrink;
}
public double getDrink()
{
if (drink==1)
drink=500;
else if (drink==2)
drink=1000;
else if (drink==3)
drink=3000;
else
drink=0;
return drink;
}
public double getPDrink()
{
double total = getDrink();
return total;
}
}
我正在考虑使用“ do while”,但是我不确定如何做到这一点
编辑:我正在使用oop,我正在尝试使用switch on class,但没有成功
答案 0 :(得分:-2)
我正在使用扫描仪,您可以使用任何其他库从控制台读取输入。不能固定您的业务逻辑,例如打印“总”价值。
char mi;
Scanner sc = new Scanner(System.in);
double total = 0.0;
do {
System.out.println("do you want to buy drinks?");
System.out.println("1. A (500)");
System.out.println("2. B (1000)");
System.out.println("3. C (1500)");
System.out.println("0 to not choose");
System.out.println("Please choose you drink");
//p.SetDrink(sc.nextDouble()); // not working unknown p in OP, so commented
char drink = sc.next().charAt(0);
System.out.println(drink); // just printing user's choice for drink
switch (drink) {
case 'A':
total += 500;
break;
case 'B':
total += 1000;
break;
case 'C':
total += 1500;
break;
default:
total += 0;
}
System.out.println("do you want to buy more?");
mi = sc.next().charAt(0);
} while (mi=='y');
System.out.println("total : " + total);
输出:
do you want to buy drinks? 1. A (500) 2. B (1000) 3. C (1500) 0 to not choose Please choose you drink C C do you want to buy more? y do you want to buy drinks? 1. A (500) 2. B (1000) 3. C (1500) 0 to not choose Please choose you drink A A do you want to buy more? n total : 2000.0