用java从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。
时间: 2024-02-17 12:01:13 浏览: 126
好的,以下是使用 Java 代码实现该功能:
```java
import.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
String s = scanner.nextLine();
if (s.toLowerCase().equals("e") || s.toLowerCase().equals("exit")) {
System.out.println("程序已退出。");
break;
} else {
System.out.println(s.toUpperCase());
}
}
}
}
```
这个代码中,我们使用了 `java.util.Scanner` 类来读取用户输入的字符串。在每次循环中,我们将读取到的字符串转换为小写,并检查是否等于“e”或“exit”。如果用户输入了这两个字符串中的任意一个,程序就会退出循环并输出一条消息。否则,我们将读取到的字符串转换为大写并输出。
相关问题
从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。java
好的,下面是示例 Java 代码:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = "";
while (true) {
input = scanner.nextLine();
if (input.equalsIgnoreCase("e") || input.equalsIgnoreCase("exit")) {
break;
}
System.out.println(input.toUpperCase());
}
scanner.close();
}
}
```
这段代码会不断读取用户的输入,将其转换成大写并打印到控制台上。如果用户输入了 `e` 或者 `exit`,则会退出程序。请注意,`Scanner` 对象在使用完毕后需要手动关闭,因此我们在程序结束前调用了 `scanner.close()` 方法。
用java实现从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然 后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。
可以使用Scanner类来读取键盘输入的字符串,并使用toUpperCase()方法将字符串转换为大写。然后使用while循环不断读取用户输入,直到用户输入“e”或“exit”,程序退出。
以下是实现代码:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input;
while (true) {
System.out.print("请输入字符串:");
input = scanner.nextLine();
if (input.equalsIgnoreCase("e") || input.equalsIgnoreCase("exit")) {
break;
}
System.out.println("转换后的字符串为:" + input.toUpperCase());
}
scanner.close();
}
}
```
运行程序后,可以看到如下输出:
```
请输入字符串:hello world
转换后的字符串为:HELLO WORLD
请输入字符串:Java is cool!
转换后的字符串为:JAVA IS COOL!
请输入字符串:e
```
当用户输入“e”或“exit”时,程序退出。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![dsp](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)