所以我正在尝试制作一个像收银员这样的程序,该程序不断增加取决于用户购买产品的数量,但我的程序不会增加价值,但它会不断取代它
班级:
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;
}
}
主要类:
import java.util.Scanner;
/**
*
*
*/
public class MainClass {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
char ch = 0;
char mi = 0;
Scanner Class = new Scanner (System.in);
Class p = new Class();
int choice;
char repeat = 'y';
char drink = 'y' ;
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(Class.nextDouble());
System.out.println("do you want to buy more?");
mi = Class.next().charAt(0);
} while (mi == 'y');
System.out.println("total= "+p.getDrink());
}
}
我正在考虑使用“do while”,但我不确定该怎么做
编辑:我正在使用 oop,我正在尝试在课堂上使用开关,但它没有用
我正在使用 Scanner,您可以使用任何其他库从控制台读取输入。没有固定您的业务逻辑,例如打印“总”值。
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句