[英]How do i add the or use While loop in my code
I want to use the while loop so it can catch any invalid input like letters or random %%$@@...etc 我想使用while循环,以便它可以捕获任何无效输入,例如字母或随机%% $ @@ ...等
Im new to java...THANKS alot for ur guys help :) here is what im working on: 我是Java的新手...非常感谢你们的帮助:)这是我正在从事的工作:
import java.util.Scanner;
public class AreaCircle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
System.out.println("Enter radius:");//Print to screen
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
}
}
put a try-catch block around nextDouble ... the doc describes what the scanner throws: http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextDouble%28%29 在nextDouble周围放置一个try-catch块...该文档描述了扫描仪抛出的内容: http : //docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextDouble%28%29
while(True) {
try {
do_parsing()
break;
} catch (EvilException e) {
continue;
}
}
If the input isn't valid, nextDouble
will throw InputMismatchException
. 如果输入无效,则nextDouble
将抛出InputMismatchException
。 You could surround your code in a do/while loop where you catch the exception and break the loop upon receiving valid input. 您可以将代码围绕在do / while循环中,在该循环中捕获异常并在收到有效输入后中断循环。
boolean error = false;
do {
try {
System.out.println("Enter val: ");
Scanner kbd = new Scanner(System.in);
double r = kbd.nextDouble();
error = false;
} catch (InputMismatchException e) {
error = true;
}
}while(error);
It is really simple to handle the user input... 处理用户输入非常简单...
I recently have a program that detects invalid user input... 我最近有一个检测无效用户输入的程序...
Here is what I did using the loop: 这是我使用循环执行的操作:
static String[] letters = {"a","b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"};
//declare an array of letters or number for your case
//and make a method to check if input key is present at the array..
public int getIndexOf(char key){
int charindex = -1;
for(int index=0; index<letters.length; index++){
if(symbols[index] == key){
charindex = index;
}
}
return charindex;
}
Then for your input: //so you could iterate though your input 然后输入://这样就可以遍历输入
boolean sentinel = false;
char[] numbervalue= input.getText().toString().toCharArray();
for (int z = 0; z < numbervalue.length; z++) {
int m = bal.getIndexOf(plaintext[z]);
if(m == -1){
sentinel = false;
break;
}
}
Then you can do the checking... 然后您可以进行检查...
if(sentinel){
//prompt the user that the input contains invalid characters
}else{
//continue with the processing....
}
Personally, I would not use a while loop, I would use a try/catch exception handler. 就个人而言,我不会使用while循环,而会使用try / catch异常处理程序。 You could put a while loop around it, but I'm not sure why you would do that. 您可以绕一会儿循环,但是我不确定为什么会这样做。 There's a built in exception called NumberFormatException and it's made for errors like the one you said. 有一个称为NumberFormatException的内置异常,它是针对像您所说的错误之类的。 All you do is 所有你要做的就是
try {
double r = sc.nextDouble();
}
catch( NumberFormatException e ) {
//put a message or anything you want to tell the user that their input was weird.
}
Literally, all it's doing is, if whatever entered is not a number, then go into the catch block and print your message. 从字面上看,它所要做的就是,如果输入的内容不是数字,则进入catch块并打印您的消息。 If you'd like, put everything there in a while loop for the sake of having a while loop. 如果愿意,可以将所有内容放入while循环中,以进行while循环。 Hope this works! 希望这有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.