gpt4 book ai didi

java - 收到 Java 无法访问代码

转载 作者:行者123 更新时间:2023-12-01 22:37:15 26 4
gpt4 key购买 nike

我在输入此代码时收到无法访问的代码错误。我不知道为什么,是因为我这个 If 语句中没有 Else 吗?

import java.util.InputMismatchException;
import java.util.Scanner;

public class pickNumbers {
Scanner readInput = new Scanner(System.in);

float[] pickNumbers(int choice) {
float[] myFloats = new float[2];
do { // do loop will continue until user enters correct response
System.out.print("Please enter 2 numbers separated by a space in the formats of floats: "); // will prompt user to enter 2 floats
try {
myFloats[0] = readInput.nextFloat(); // will read first float entered
myFloats[1] = readInput.nextFloat(); // will read second float entered
if (choice == 4 && myFloats[1] == 0.0f)
;
{
System.out.println("Cannot complete calculation. Cannot divide by 0, please try again.");
myFloats[0] = myFloats[1] = 0.0f;
continue;
}
break; // i am receiving an error here saying unreachable code and i don't know why

} catch (final InputMismatchException e) {
System.out.println("You have entered an invalid input. Try again.");
readInput.nextLine(); // discard input that is not a float
continue; // loop will continue until the correct answer is found
}
} while (true);
return myFloats; // an an unreachable error here also
}
}

最佳答案

看看你的 if 语句,它有一个分号不合适,应该是:

if (choice == 4 && myFloats[1] == 0.0f) {

关于java - 收到 Java 无法访问代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26707952/

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