gpt4 book ai didi

java - 我需要在同一个 "equation"中声明一个变量两次

转载 作者:行者123 更新时间:2023-12-01 08:59:39 25 4
gpt4 key购买 nike

所以我有这个代码

public static void main(String[] args) 
{
final double life = 100;

Scanner input = new Scanner(System.in);

System.out.println("enter a value to damage the ennemy.");
double attack = input.nextDouble();
double life = life - attack;

System.out.println("The ennemy has" + life + "left");

我试图将生命变量基本上更新为具有不同值的新生命变量。但是程序说我不能两次声明同一个局部变量...我怎样才能让它工作,而不需要 make varaibel "life1, life2 etc..."

谢谢!

最佳答案

您可以为变量分配新值:

life = life - attack;

或者甚至:

life -= attack;

关于java - 我需要在同一个 "equation"中声明一个变量两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41785331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com