0
我的程序大部分运行平稳。只有不正确的部分是变量newsub的最后一个输入不起作用。程序跳过它并继续前进。我试图评论最后一部分,但后来我仍然不被允许输入任何内容。所以显然日食是跳过线?我看到了一些帖子,但是当我将第一部分从键盘更改为输入时,调用了一个错误,因为他们将其视为新的未调用变量。任何帮助都会很棒! (另外我完全知道我的程序比需要的更长)谢谢!java扫描仪跳过最后输入
import java.util.Scanner;
public class ScannerTest1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a long string: ");
String lin = keyboard.nextLine();
System.out.print("Enter a substring: ");
String sub = keyboard.nextLine();
int leng = lin.length();
System.out.println("Length of your string: " + leng);
int leng2 = sub.length();
System.out.println("Length of your substring: " + leng2);
int mid = lin.indexOf(sub);
System.out.println("Starting position of your substring in string: " + lin.indexOf(sub));
System.out.println(lin.substring(0, mid));
System.out.println(lin.substring((mid + leng2 + 1), leng));
System.out.print("Enter a position between 1 and 43: ");
int pos = keyboard.nextInt();
System.out.println("The character at position " + pos + " is " + lin.charAt(pos));
System.out.print("Enter a replacement string: ");
String newsub = keyboard.nextLine();
System.out.println("Your new string is: " + lin.substring(0, mid) + newsub + lin.substring((mid + leng2 +1), leng));
keyboard.close();
}
}