【问题标题】:Input a Special Character then use in a switch case [closed]输入一个特殊字符,然后在开关盒中使用[关闭]
【发布时间】:2014-02-24 19:04:47
【问题描述】:

我正在尝试在分数之间做一个计算器,如果用户输入 +、-、* 或 / 它将在这种情况下对应。这是我到目前为止的代码:

import java.util.Scanner;
public class calculator 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String x,y;


        System.out.println("Enter first fraction in a / b form: ");
        x = input.nextLine();

        System.out.println("Enter operation: ");
        char z = input.next().charAt(0);

        System.out.println("Enter second fraction in c / d form: ");
        y = input.nextLine();

        String aString = x.substring(0,1);
        String bString = x.substring(4,5);
        String cString = x.substring(0,1);
        String dString = x.substring(4,5);

        int a = Integer.parseInt(aString);
        int b = Integer.parseInt(bString);
        int c = Integer.parseInt(cString);
        int d = Integer.parseInt(dString);


        int answer = 0;
        switch (z)
        {
            case '+':
                answer = (a/b) + (c/d);
                break; 
            case '-':
                answer = (a/b) - (c/d);
                break;
            case '*':
                answer = (a/b) * (c/d); 
                break;
            case '/':
                answer = (a/b) /(c/d);
                break;
            default:
                System.out.println("ERROR");
                break;              
        }
        System.out.println("Answer = " + answer);       
    }
}

输出应该是

Enter first fraction in a / b form: 
1 / 2
Enter operation: 
+
Enter second fraction in c / d form: 
2 / 5
answer = 9/10

【问题讨论】:

  • 您遇到了什么问题?输出错误?错误信息?请分享。
  • 特别是,这里没有实际问题的迹象...
  • 你的开关没问题,@AJ。指出了一个逻辑错误,但是您有一个更大的问题,即要输出一个分数,您需要执行fraction arithmetic。您可以使用 double 进行计算,这样您就不会以 0 结尾,但会得到一个十进制数。
  • 我做了@AJ 所说的更改,但是当我输入“+”时我得到了这个:线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:java.lang 中的 1 .String.substring(String.java:1907) 在calculator.main(calculator.java:33)

标签: java input char


【解决方案1】:

问题来了

 String aString = x.substring(0,1);
 String bString = x.substring(4,5);
 String cString = x.substring(0,1);
 String dString = x.substring(4,5);

a=cb=d

这两行

 String cString = x.substring(0,1);
 String dString = x.substring(4,5);

应该是

String cString = y.substring(0,1);
 String dString = y.substring(4,5);

switch之前打印abcdz的值然后你就会知道你的正确性了。

还有一件事是,您正在对 int 变量进行除法,因此结果将仅在 int 中。

我建议你将abcanswer的类型更改为double,并使用Double.parseDouble()string转换为double

【讨论】:

  • 没错,但 OP 的代码有不止一个问题。例如他们的输出显示他们期望一个分数,但 answer 被计算为一个 int。
  • @Radiodef 是的,没错。让我把这个包括在答案中。
  • 谢谢!!我做了@AJ所说的更改,但是当我输入“+”时我得到了这个:线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:java.lang.String.substring(String.java)中的1 :1907) 在calculator.main(calculator.java:33)
  • @user3348064 哪一行在33
  • String cString = y.substring(0,1);
【解决方案2】:

这个问题非常适合OOP,一个额外的类:

public class Q {

    public final int numerator;
    public final int denominator;

    public static Q valueOf(String representation) {
        Pattern pattern = Pattern.compile("([-]?\\d+) *[/:] *([-]?\\d+)");
        Matcher matcher = pattern.matcher(representation.trim());
        if (!matcher.matches()) {
            throw new IllegalArgumentException(
                "Not a quotient (like '3/4'): " + representation);
        }
        int num = Integer.parseInt(matcher.group(1));
        int den = Integer.parseInt(matcher.group(2));
        return new Q(num, den);
    }

    public Q(int num, int den) {
        if (den < 0) {
            den = -den;
            num = -num;
        }
        int c = gcd(Math.abs(den), Math.abs(num));
        denominator = den / c;
        numerator = num / c;
    }

    @Override
    public String toString() {
        return numerator + "/" + denominator;
    }

    public Q add(Q rhs) {
        int c = gcd(denominator, rhs.denominator);
        int den = (denominator / c) * rhs.denominator;
        int num = numerator * (rhs.denominator / c)
                + rhs.numerator * (denominator / c);
        return new Q(num, den);
    }

    public Q mult(Q rhs) {
        int den = denominator * rhs.denominator;
        int num = numerator * rhs.numerator;
        return new Q(num, den);
    }

    public static int gcd(int x, int y) {
        assert x >= 0 && y >= 0;
        while (x != y) {
            if (x > y) {
                x -= y;
            } else {
                y -= x;
            }
        }
        return x;
    }
}

使用这个类,几个小的有问题的代码部分消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 2015-08-14
    • 1970-01-01
    相关资源
    最近更新 更多