Scanner scanner=new Scanner(System.in); String s = scanner.nextLine(); int choose = Integer.parseInt(s);
这个程序是要可以让用户重复执行的。Scanner对象怎么在while循环中使用?
这么写的话,程序在1个switch分支(譬如说输入1并且输入一串字符)运行结束的时候会发生错误。 System.out.println("enter 1 to encrypt.\tenter 2 to decrept.\tenter 0 to quit."); Scanner scanner = null; int choose = 0; while(true) { scanner = new Scanner(System.in); choose = scanner.nextInt(); switch(choose) { case 1: String source = input_source(); String encrypted_source = encrypt(source); System.out.println("encrypted String is:\t"+encrypted_source); write_tofile(encrypted_source); break; case 2: String str; BufferedReader in = new BufferedReader(new FileReader("encryptedSource.txt")); str = in.readLine(); in.close(); String decrypted_source = decrypt(str); System.out.println("decrypted String is:\t"+decrypted_source); break; case 0: System.out.println("program exit."); System.exit(0); default: System.out.println("bad input!"); } }
之前是在myEclipse中运行的。 现在在命令行中运行。问题的确是出在Scanner上。但是不会解决_(:з」∠)_。 f:\test> f:\test>java DESDemo enter 1 to encrypt. enter 2 to decrept. enter 0 to quit. 1 please enter a string to encrypt. hello this is the source string you entered: hello encrypted String is: XrHHuXaT+lw= successfully saved encrypted_source to file. Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at DESDemo.main(DESDemo.java:55)f:\test>
import java.util.Scanner;public class ScannerExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); usage(); while(scanner.hasNextInt()){ int i = scanner.nextInt(); switch (i) { case 1: System.out.println(i); break; case 2: System.out.println(i); break; case 0: System.exit(0); break; default: usage(); continue; } } } private static void usage() { System.out.printf("enter 1 to encrypt.%nenter 2 to decrept.%nenter 0 to quit.%n"); } } 输入非数字也退出。
问题最后是自己这么解决的。show_usage();
Scanner scanner = new Scanner(System.in);; int choose = 0; while(scanner.hasNextInt()) { choose = scanner.nextInt(); switch(choose) { case 1: String source = input_source(); String encrypted_source = encrypt(source); System.out.println("encrypted String is:\t"+encrypted_source); write_tofile(encrypted_source); show_usage(); continue; case 2: String str; BufferedReader in = new BufferedReader(new FileReader("encryptedSource.txt")); str = in.readLine(); in.close(); String decrypted_source = decrypt(str); System.out.println("decrypted String is:\t"+decrypted_source); show_usage(); continue; case 0: System.out.println("program exit."); System.exit(0); default: System.out.println("bad input!input again!"); show_usage(); continue; } } } 但是看着怪别捏。
String s = scanner.nextLine();
int choose = Integer.parseInt(s);
System.out.println("enter 1 to encrypt.\tenter 2 to decrept.\tenter 0 to quit.");
Scanner scanner = null;
int choose = 0;
while(true)
{
scanner = new Scanner(System.in);
choose = scanner.nextInt();
switch(choose)
{
case 1:
String source = input_source();
String encrypted_source = encrypt(source);
System.out.println("encrypted String is:\t"+encrypted_source);
write_tofile(encrypted_source);
break;
case 2:
String str;
BufferedReader in
= new BufferedReader(new FileReader("encryptedSource.txt"));
str = in.readLine();
in.close();
String decrypted_source = decrypt(str);
System.out.println("decrypted String is:\t"+decrypted_source);
break;
case 0:
System.out.println("program exit.");
System.exit(0);
default:
System.out.println("bad input!");
}
}
现在在命令行中运行。问题的确是出在Scanner上。但是不会解决_(:з」∠)_。
f:\test>
f:\test>java DESDemo
enter 1 to encrypt. enter 2 to decrept. enter 0 to quit.
1
please enter a string to encrypt.
hello
this is the source string you entered: hello
encrypted String is: XrHHuXaT+lw=
successfully saved encrypted_source to file.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at DESDemo.main(DESDemo.java:55)f:\test>
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
usage();
while(scanner.hasNextInt()){
int i = scanner.nextInt();
switch (i) {
case 1: System.out.println(i); break;
case 2: System.out.println(i); break;
case 0: System.exit(0); break;
default:
usage(); continue;
}
}
} private static void usage() {
System.out.printf("enter 1 to encrypt.%nenter 2 to decrept.%nenter 0 to quit.%n");
}
}
输入非数字也退出。
Scanner scanner = new Scanner(System.in);;
int choose = 0;
while(scanner.hasNextInt())
{
choose = scanner.nextInt();
switch(choose)
{
case 1:
String source = input_source();
String encrypted_source = encrypt(source);
System.out.println("encrypted String is:\t"+encrypted_source);
write_tofile(encrypted_source);
show_usage();
continue;
case 2:
String str;
BufferedReader in
= new BufferedReader(new FileReader("encryptedSource.txt"));
str = in.readLine();
in.close();
String decrypted_source = decrypt(str);
System.out.println("decrypted String is:\t"+decrypted_source);
show_usage();
continue;
case 0:
System.out.println("program exit.");
System.exit(0);
default:
System.out.println("bad input!input again!");
show_usage();
continue;
}
}
}
但是看着怪别捏。