如何确保在控制台上输入数字

时间:2015-03-15 00:47:00

标签: java input

当用户输入“L”或“%”作为示例时出现问题(错误消息显示正确,因为只允许输入数字),但在此之后下一条输入消息“请输入平均值”冲动传递“立即出现。我想让它停止,直到用户输入一个数字然后下一个出现。对不起,如果我不清楚,请随时问。

// UserEntry类

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class UserEntry{

public double totalImpulse ;
public double averageImpulse;


//Declaring all user input, with exception handling
double totalImpulse(){

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

// String for Total Impulse 
String strtotalImpulse = null;
int    inttotalImpulse = 0;

try {
strtotalImpulse = in.readLine();
} 
catch (IOException ioe) {  
}

// convert it to integer
try {
double totalImpulse=Double.valueOf(strtotalImpulse); //Exception handling (Only integers)
}
catch (Exception e) { 
    System.out.println("Please enter an Integer for Total Impulse!"); //Print this when "L" or "%" , not integer has been entered.
}
return inttotalImpulse;

}

   double averageImpulse(){ 

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

// String for Total Impulse 
String straverageImpulse = null;
int    intaverageImpulse = 0;

try {
straverageImpulse = in.readLine();
} 
catch (IOException ioe) {  
}
// convert it to integer
try {
double averageImpulse=Double.valueOf(straverageImpulse); //Exception handling (Only integers)
}
catch (Exception e) { 
    System.out.println("Please enter an Integer for Average Impulse!"); //Print this when "L" or "%" , not integer has been entered.
}
return intaverageImpulse;

} }

//大师班(输入)

 public class Master { //Master class

 public static void main( String args[] ) //Standard header for main method
 {


//User inputs
UserEntry input = new UserEntry(); //Creating object from UserEntry class

System.out.print("\nPlease enter Total impulse delivered: "); 
input.totalImpulse();

System.out.print("Please enter Average impulse delivered: ");
input.averageImpulse();
}
}

2 个答案:

答案 0 :(得分:0)

while(true){
    try { 
        strtotalImpulse = in.readLine(); 
    } catch (IOException ioe) { }
    try{
        double yourReturnValue=Double.valueOf(strtotalImpulse);
        return yourReturnValue; //we get a double
    }catch (Exception e) { //oops... there is something wrong when trying to cast string to double. So it must be wrong with user input.
        System.out.println("please enter a number:");
    }
}

答案 1 :(得分:0)

此解决方案与Turtle one非常相似,但删除了一些重复的代码,并强制MasterClass等待值。 我改变了一点逻辑,对于读取输入使用了扫描仪,如问题How can I read input from the console using the Scanner class in Java?

所示

大师班

public static void main( String args[] ) //Standard header for main method
 {
            //User inputs
UserEntry input = new UserEntry(); //Creating object from UserEntry class


Double totalImpulse = null;
while(totalimpulse==null){

    System.out.print("\nPlease enter Total impulse delivered: ");
    try { 
     totalimpulse=  input.getDoubleFromConsole();
 }
 catch(Exception e){
System.out.println("please enter a number:");
}
}
Double averageImpulse=null;
while(averageImpulse==null){
    System.out.print("Please enter Average impulse delivered: ");

    try{
        averageImpulse= input.getDoubleFromConsole();
    }
    catch(Exception e){
System.out.println("please enter a number:");
}
}
}

UserEntry:

  double getDoubleFromConsole(){

             Scanner sc = new Scanner(System.in);
            double i = sc.nextDouble();

    }