如何实现键盘输入字符串
实现,编写程序实现:从键盘输入一个字符,输出加密后的字符。
加密规则是对每个字母转换为下一个字母表示,原来是 a 转换为 b,原来是 B 转换为C。小写的 z转换为小写的 a,大写的 Z 转换为大写的 A,每个数字不变。
会报错,
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method charAt(int) is undefined for the type Scanner
The left-hand side of an assignment must be a variable
Syntax error, insert "AssignmentOperator Expression" to complete Assignment
Syntax error, insert ";" to complete Statement
import java.util.*;
public class T1_3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
char x=sc.charAt(0);
if(x=='z')
x='a';
else if(x=='Z')
x='A';
else if(x>='a'&&x<'z')
x=(char)(x+1);
else(x>='A'&&x<'Z')
x=(char)(x+1);
System.out.println(x);
}
}
import java.util.*;
public class T1_3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char x = sc.next().charAt(0);
if (x == 'z')
x = 'a';
else if (x == 'Z')
x = 'A';
else if (x >= 'a' && x < 'z')
x = (char)(x + 1);
else if (x >= 'A' && x < 'Z')
x = (char)(x + 1);
System.out.println(x);
}
}
不知道你这个问题是否已经解决, 如果还没有解决的话:package com.example.javatest.theardTest.MultiThreadAlgorithm;
/**
* 要求线程a执行完才开始线程b, 线程b执行完才开始线程
*
* join()解释:https://blog.csdn.net/qq_18505715/article/details/79795728
*
* wait() 和 notify() 解释:https://blog.csdn.net/chaozhi_guo/article/details/50249177
*
* join()的作用:主要作用是同步,它可以使得线程之间的并行执行变为串行执行。在A线程中调用了B线程的join()方法时,表示只有当B线程执行完毕时,A线程才能继续执行。
*
* public void joinDemo(){
* //....
* Thread t=new Thread(payService);
* t.start();
* //....
* //其他业务逻辑处理,不需要确定t线程是否执行完
* insertData();
* //后续的处理,需要依赖t线程的执行结果,可以在这里调用join方法等待t线程执行结束
* t.join();
* }
*
*/
public class T1T2T3 {
public static class PrintThread extends Thread{
PrintThread(String name){
super(name);
}
@Override
public void run() {
for(int i = 0; i < 100; i++){
System.out.println(getName() + " : " + i);
}
}
}
public static void main(String[] args){
PrintThread t1 = new PrintThread("a");
PrintThread t2 = new PrintThread("b");
PrintThread t3 = new PrintThread("c");
try {
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 我对于join方法的理解:
*
* join() 的源码:
* public final void join(long millis) throws InterruptedException {
* synchronized(lock) {
* ...
*
* while (isAlive()) {
* lock.wait(0);
* }
* ...
* }
* }
*
* 其实就是main()线程调用join()后,synchronized(lock)语句块,获得lock的锁,
*
* 然后判断如果t1线程isAlive(), 就一直lock.wait(), 让自己(main()线程)阻塞住,
*
* 直到t1线程 !isAlive 后才不wait, 等待着被notify(), 然后t1 die后会调用lock.notifyAll()。
*
*
* 注意:这里lock.wait(0)虽然在t1.join()内,但是join()内的代码不是运行在t1线程中,而是运行在main()线程中,
* t1线程中运行的是其run()方法内的代码。
*
*/
您好,我很乐意为您提供Java键盘输入字符的解决方案。以下是具体步骤:
首先需要导入Java的io库:import java.io.*;
然后需要创建一个Scanner对象来接收输入的字符:Scanner scanner = new Scanner(System.in);
接下来可以使用scanner.nextLine()
方法来获取用户的输入,并将输入赋值给一个字符串变量:String input = scanner.nextLine();
如果您需要获取单个字符,可以使用scanner.next().charAt(0)
方法,其中charAt(0)
表示获取字符串的第一个字符,即用户输入的第一个字符。
这里为您提供一个简单的样例代码,用于读取用户输入的字符串并将其打印出来:
import java.util.Scanner;
public class KeyboardInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String input = scanner.nextLine();
System.out.println("您输入的字符串是:" + input);
}
}
如果您有任何其他关于Java键盘输入字符的问题,可以在提供更多具体信息后再与我联系。
至于报错内容,如果您遇到任何问题,请尽可能提供具体的错误信息和代码段,以便更好地帮助您解决问题。否则,我将无法提供有效的帮助。