【发布时间】:2021-10-28 20:12:07
【问题描述】:
我正在尝试编写一个循环,当用户输入 Y 时,循环继续,当用户输入 N 时,循环停止。但是,当我尝试分配变量时,我收到错误“无法从 void 转换为 char”,我显然在某处搞砸了,但我不确定在哪里。
import java.util.Scanner;
public class SimpleList {
public static void main(String[] args) {
System.out.println("Welcome to the Simple List Class");
getData();
}
private static void getData() {
Scanner input = new Scanner(System.in);
float[] numbers = new float[10];
System.out.println("Enter a non-negative floating point value: ");
for(int i = 0; i < 10; i++) {
float x = input.nextFloat();
if (x > 0) {
numbers[i] = x;
char ans = System.out.print("Would you like to input another value? (Y or N)? ");
}
else {
System.out.println("That is not a valid. Try Again.");
}
}
System.out.println(Arrays.toString(numbers));
}
} ```
【问题讨论】:
-
System.out.print不返回任何内容。您需要使用您的Scanner来获得答案。