gpt4 book ai didi

java - 想要循环但变量未初始化

转载 作者:行者123 更新时间:2023-12-01 08:09:19 27 4
gpt4 key购买 nike

我的问题从第一个 do-while 循环开始。我希望它询问“请输入 0 到 90 之间的角度:”,如果用户确实输入 0 到 90 之间的数字,则必须输入火药的量。他们的输入将经过物理计算,如果他们的结果不是“它很成功!”然后我要他们回去再次输入角度和火药。但问题在于

while (距离 - 距离2 != 0 || 距离 - 距离2 != 1 || 距离2 - 距离!= 1); System.out.println("成功了!");

The error says: variable distance2 might not have been initialized.

<小时/>
    import java.util.*;
import java.text.*;
public class BombsAway {
private static double ZERO_THOUSAND = 1000;
private static double KG_TO_VELOCITY = 50;
private static double GRAVITY = -9.81;
public static void main(String[] args) {

Scanner input = new Scanner(System.in);
DecimalFormat threeDec = new DecimalFormat("#.##");
Random gen = new Random();
BombsAway distanceAn = new BombsAway();
boolean invalidInput = true;
double distance, distance2;

System.out.println("Please enter a positive integer seed value: ");
while(invalidInput) {
try {
int seedValue = Integer.valueOf(input.nextLine());
if (seedValue <= 0) {
System.out.println("Please enter a positive integer seed value:"
+ " ");
}
else {
distance = gen.nextDouble() * ZERO_THOUSAND;
System.out.println("That target is " + threeDec.format(distance)
+ "m away.");
do {
System.out.println("Please enter an angle between 0 and 90 " +
"degrees: ");
while (invalidInput) {
try {
double angle = Double.valueOf(input.nextLine());
if (angle < 0 || angle > 90) {
System.out.println("Please enter an angle between " +
"0 and 90 degrees: ");
}
else {
System.out.println("Please enter the amount of " +
"gunpowder in kilograms: ");
try {
double gunpowder =
Double.valueOf(input.nextLine());
//physics part
double initialV = gunpowder * KG_TO_VELOCITY;
double vX = initialV * Math.cos(angle);
double vY = initialV * Math.sin(angle);
double airBorn = (-vY - vY) / GRAVITY;
distance2 = (vX * airBorn);

if (distance > distance2) {
double finalDist = distance - distance2;
System.out.println("It's " +
threeDec.format(finalDist) + "m short.");
}
else if (distance < distance2) {
double finalDist = distance2 - distance;
System.out.println("It's " +
threeDec.format(finalDist) + "m long.");
}
}
catch(NumberFormatException e) {
System.out.println("Please enter the amount of " +
"gunpowder in kilograms: ");
}
}
}
catch(NumberFormatException e) {
System.out.println("Please enter an angle between " +
"0 and 90 degrees: ");
}
}
}while (distance - distance2 != 0 || distance - distance2 != 1
|| distance2 - distance != 1);
System.out.println("It's a hit!");
}
}
catch(NumberFormatException e) {
System.out.println("Please enter a positive integer seed value: ");
}
}
}
}

最佳答案

在您的代码中 - distance2 在条件 block 之一(在 while 循环内的 else block 中)初始化,可能存在以下情况:您的 whileelse 条件将无法满足,在这种情况下,distance2 未初始化,并且局部变量无法在没有初始化的情况下使用,因此只需使用任何有意义或所需的默认值初始化 distance2 即可:

double distance2 = 0.0; 或使用代码逻辑适用的某个特定值

关于java - 想要循环但变量未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18807324/

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