gpt4 book ai didi

java - 如何测试循环中是否有多个字母

转载 作者:行者123 更新时间:2023-11-30 07:18:46 25 4
gpt4 key购买 nike

我的作业说明如下

如果学生输入的成绩不是一个字母,请使用 System.out.println() 方法告诉学生重试。

这是我正在使用的代码

import java.io.IOException;
import java.util.Scanner;
public class MyGradeLoops {

public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);

for (int x = 1; x <= 5; x++) {

System.out.println("Please enter Your Class Grades");
String A;
char B;
A = in.next();
System.out.println("Thank You");
if (**TRYING TO TEST FOR MORE THAN ONE LETTER**) {
System.out.println("Only One Letter At A Time");
}
}
System.out.println("Keep Up The Good Work")
}
}

感谢任何帮助

最佳答案

import java.io.IOException;

import java.util.Scanner;

public class MyGradeLoops {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
for (int x = 1; x <= 5; x++) {
System.out.println("Please enter Your Class Grades");
String A;
char B;
A = in.next();
System.out.println("Thank You");
if (A.length() != 1) {
System.out.println("Only One Letter At A Time");
}
}
System.out.println("Keep Up The Good Work");
}
}

关于java - 如何测试循环中是否有多个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37957112/

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