输入不是“q”时如何输出错误,扫描仪不能加倍

时间:2017-02-22 17:19:22

标签: java

public class CurrencyConverter
{
double dollar;
double rate; //constructors
double euro;

// constuctors

public CurrencyConverter()
{
    dollar=0;
    rate=0.95;
}

//对象

public void setDollar(double d)
{
     dollar= d;
}
public double getCurrency()
{
 euro= rate*dollar;
 return euro;

如果输入不是“q”时如何输出错误而在我的代码中不会加倍 我尝试使用if方法,但它不起作用

import java.util.Scanner;

public class CurrencyTester {

    private static Scanner in;

    public static void main(String[] args) {

        CurrencyConverter one = new CurrencyConverter();
        System.out.println("Convert dollar to euro");
        System.out.println("");

        do {
            System.out.println("Enter Dollars:");
            in = new Scanner(System.in);
            String d = in.next();
            if ("Q".equalsIgnoreCase(d)) {
                System.out.println("Stop!");
                break;
            } else {
                double ds = Double.parseDouble(d);
                one.setDollar(ds);
                System.out.println("Euro:");
                System.out.println(one.getCurrency());
            }
        } while (true);

    }
}

3 个答案:

答案 0 :(得分:0)

当您检查输入是否为q时,第一部分是正确的,您需要检查您的输入是否是双倍,因此您可以使用try{}catch(){}这样:

do {
    System.out.println("Enter Dollars:");
    Scanner in = new Scanner(System.in);
    String d = in.next();
    if ("Q".equalsIgnoreCase(d)) {
        System.out.println("Stop!");
        break;
    } else {
        //check if your input is valid double or not, if not show an error message
        try {
            double ds = Double.parseDouble(d);
            one.setDollar(ds);
            System.out.println("Euro:");
            System.out.println(one.getCurrency());
        } catch (NumberFormatException e) {
            System.out.println("*********Not double*********");
        }
    }
} while (true);

答案 1 :(得分:0)

Scanner类有一些有用的方法,如:

使用这些方法,您可以轻松检查输入是否与double,字符'q''Q'匹配,或者不是上述任何一种:

try (Scanner in = new Scanner(System.in)) {
    while (true) {
        System.out.println("Enter a double (q to quit)");
        if (in.hasNextDouble()) {
            double ds = in.nextDouble();
            System.out.printf("ds = %f%n", ds);
        } else if (in.hasNext("[qQ]")) {
            in.nextLine();  // clear out from 'q' to end of line
            System.out.println("Stop!");
            break;
        } else {
            String bad_input = in.next();
            System.out.println("Not a double: " + bad_input);
            in.nextLine();  // clear out all invalid input to end of line
        }
    }
}

注意使用in.nextLine();清除输入流,直到\n字符被消耗,无论是遇到错误输入还是单个{遇到了{1}}字。

答案 2 :(得分:-1)

你说d不是" Q" AND ds不是双倍的。我会从字面上理解这一点。如果不是" Q",您的当前实现将在评估double之前突破while循环。

  1. 你需要改变它。
  2. 尝试从输入字符串中解析NumberFormatException时,使用try ... catch块处理double
  3. 这是while循环的新剪辑:

    do { 
        boolean isQ = true;
        boolean isDouble = true;
        System.out.println("Enter Dollars:");
        in = new Scanner(System.in);
        String d = in.next();
    
        isQ = ("Q".equalsIgnoreCase(d)) {   
        if(isQ) {
            try {
                double ds = Double.parseDouble(d);
                one.setDollar(ds);
                System.out.println("Euro:");
                System.out.println(one.getCurrency());
            } catch (NumberFormatException e) {
                isDouble = false;
            }
        } 
        if (!isQ && !isDouble) {
            // some error message
        } 
    } while(true);