为错误输入创建错误消息?

时间:2014-02-23 17:28:58

标签: java

我正在尝试创建和错误消息,并在用户输入double以外的任何内容时重新提示用户。由于无法取消引用double使其成为String,反之亦然。我如何创建错误消息?这是我的代码:

public class Project1
{
public static void main(String[] args){
    String prompt;
    boolean promptB;
    do{
        Scanner input = new Scanner (System.in);
        System.out.println("Enter a number, a space and F to convert from degF to degC and C to convert to degC to degF");
        double deg = input.nextDouble();
        String letter = input.next().toUpperCase();
        char dg = letter.charAt(0);

        tempLoop:
       if( dg == 'C'){

            double cTemp= (double)(deg-32)*(5.0/9.0);
            System.out.printf("%f degC converted to degF is %.2f%n ", deg, cTemp );

        }
        else if(dg == 'F'){
            double fTemp = (double) (deg*9/5) + 32;
            System.out.printf("%f degF converted to degC is %.2f%n ", deg, fTemp );
        }
        else{
            System.out.println("That character does not correspond to a valid unit of measure ");
        }

        do{
            System.out.println("Would you like to continue?(yes/no)");
            prompt = input.next();
            if(prompt.equalsIgnoreCase("yes")){
                promptB = false;
            } else if (prompt.equalsIgnoreCase("no")){
                break;
            }else{
                promptB = false;
                System.out.println("Would you like to continue?(yes/no)");
                prompt = input.next();
            }

        }while(promptB);

    }while(prompt.equalsIgnoreCase("yes"));
}

}

0 个答案:

没有答案