[英]Want to loop but variable is not initialized
我的問題始於第一個do-while循環。 我要問“請輸入一個介於0到90之間的角度:”,如果用戶確實輸入了一個介於0到90之間的數字,他們必須輸入槍粉的數量。 他們的輸入將經過物理計算,如果結果不是“這很成功!”。 然后我想讓他們返回並再次輸入角度和火葯。 但是事情在
while (distance - distance2 != 0 || distance - distance2 != 1 || distance2 - distance != 1); System.out.println("It's a hit!");
該錯誤顯示:變量distance2可能尚未初始化。
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
內部條件塊中的一個初始化(在else
塊內while
循環)可能存在這樣的情況,當你while
還是else
條件將不會被滿足,在這種情況下, distance2
保留未初始化和局部變量不能無需初始化即可使用,因此只需使用任何有意義的或所需的默認值將該distance2
初始化為:
double distance2 = 0.0;
或具有您的代碼邏輯可以使用的某些特定值
distance2
變量在try / catch塊中初始化。 可能會引發異常,並在初始化distance2
之前將控制流放入catch
塊中。 在catch塊之后,distance2在while
循環的末尾用於計算中,因此是的:它可能未在此行初始化:
while (distance - distance2 == 0 || distance - distance2 == 1
|| distance2 - distance == 1)
如果在讀取input
的gunpowder
或angle
值時拋出了NumberFormatException
。 還有一個if條件(對angle
邊界檢查)可能會使distance2保持不變。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.