如何增加集成商的价值?

How do I add to the value of an integrer?

提问人:quasireal 提问时间:10/12/2023 最后编辑:E_net4quasireal 更新时间:10/19/2023 访问量:58

问:

对于爪哇的学校作业,我们假装在水疗接待处编写代码,客户应该添加他们选择的治疗方法,并且应该将其添加到总成本中。我们得到了一个代码基础(我已经编写了 switch 语句)。在计算成本时,我们应该使用代码中声明的文字。我无法弄清楚如何将治疗的价值添加到总成本中(请参阅粗体的**部分)。我一直在试图找到一种方法来增加成本的价值,但我似乎无法弄清楚......

到目前为止,代码如下:

import java.util.Scanner;
public class SpaReception{
  public static void main(String[] args) {
    double cost = 0;
    int treatment = 0;

    final double faceMask = 600.00;
    final double footMassage = 300.00;
    final double fullBodyMassage = 1500.00;

    Scanner input = new Scanner(System.in);  //create scanner for input
    System.out.println("Type in which treatment you've gotten ");
    System.out.println("Face mask: 1");
    System.out.println("Foot massage: 2");
    System.out.println("Full body massage: 3");
    System.out.println("Cancel: -1");
    treatment = input.nextInt();

    switch (treatments) {
      case 1: 
        ***add the value of faceMask to cost***
        break;
      case 2:
       ***add the value of footMassage to cost***
        break;
      case 3:
        ***add the value of fullBodyMassage to cost***
        break;
      case 4:
        break;
    }

    System.out.println("The cost is: "+cost);
  }
}

由于我无法添加一个新常量(例如“sumOne = sum + faceMask”)并以这种方式计算它,因此我真的不知道该做什么以及如何增加成本值。

Java 数组整 数双精度 java.util.scanner

评论

2赞 JayC667 10/12/2023
cost += faceMask;
2赞 QBrute 10/12/2023
“因为我无法添加新常量” - 您不必这样做。只需使用cost = cost + whatever

答:

1赞 Yash Kolte 10/12/2023 #1

由于您无法向代码中添加新变量,因此我们只需将处理值添加到实际成本中即可。由于它是“切换案例”语句,除非我们需要再次重复此过程,否则所选处理的值将添加到成本中,即 0。

import java.util.Scanner;
public class SpaReception{
  public static void main(String[] args) {
    double cost = 0;
    int treatment = 0;

    final double faceMask = 600.00;
    final double footMassage = 300.00;
    final double fullBodyMassage = 1500.00;

    Scanner input = new Scanner(System.in);  //create scanner for input
    System.out.println("Type in which treatment you've gotten ");
    System.out.println("Face mask: 1");
    System.out.println("Foot massage: 2");
    System.out.println("Full body massage: 3");
    System.out.println("Cancel: -1");
    treatment = input.nextInt();

    switch (treatment) {
      case 1: 
      cost += faceMask;
        break;
      case 2:
      cost += footMassage;
        break;
      case 3:
        cost += fullBodyMassage;
        break;
      case 4:
        break;
    }

    System.out.println("The cost is: "+cost);
  }
}

这就是我认为可以解决不添加另一个变量并且仍然获得菜单计算的问题。我希望这是你需要的和帮助的。

P.S:变量“处理”在开关案例或声明时以不同的方式提及。

评论

0赞 quasireal 10/13/2023
天哪,是的,谢谢!似乎我已经完全忘记了 += 运算符的存在,这正是我一直在寻找的——非常感谢。^^
0赞 Reilas 10/13/2023 #2

检查多个输入。

下面是一个示例。

double cost = 0;
int treatment = 0;

final double faceMask = 600.00;
final double footMassage = 300.00;
final double fullBodyMassage = 1500.00;

Scanner input = new Scanner(System.in);  //create scanner for input
System.out.println("Type in which treatment you've gotten ");
System.out.println("Face mask: 1");
System.out.println("Foot massage: 2");
System.out.println("Full body massage: 3");

input = new Scanner(input.nextLine());
for (String s : input.tokens().toArray(String[]::new))
    switch (s) {
        case "1" -> cost += faceMask;
        case "2" -> cost += footMassage;
        case "3" -> cost += fullBodyMassage;
    }

System.out.println("The cost is: "+cost);

输出

Type in which treatment you've gotten 
Face mask: 1
Foot massage: 2
Full body massage: 3
1 2 3
The cost is: 2400.0