在中频条件下挣扎

时间:2018-10-17 09:54:12

标签: java jcreator

我想问的问题是我无法制作E部分,但是V部分正在工作,我尽了一切努力弄清楚我的逻辑发生了什么... 请帮我使“ E”有效。

import java.util.Scanner;

public class Service {

    public static void main(String args[]) {
        String service;
        float monthV, usaV, ausV, rusV, callV;
        float monthE, usaE, ausE, rusE, callE;

        Scanner input = new Scanner(System.in);

        System.out.print("Which service did you use for the calls?<V - Vartec, E - Eircom> :   ");
        service = input.nextLine();

        if (service.charAt(0) != 'E')
            if (service.charAt(0) != 'V') {
                System.out.println("Thank you for your time...good bye.");
            } else {
                if (service.charAt(0) == 'V') {
                    System.out.print("\nPlease enter the total number of calls made in the month:   ");
                    monthV = input.nextFloat();

                    System.out.print("\nPlease enter the number of minutes spent calling the USA:   ");
                    usaV = input.nextFloat();
                    System.out.print("\nPlease enter the number of minutes spent calling the Australia:   ");
                    ausV = input.nextFloat();
                    System.out.print("\nPlease enter the number of minutes spent calling the Russia:   ");
                    rusV = input.nextFloat();
                    callV = ((usaV * 0.06f) + (ausV * 0.08f) + (rusV * 0.24f));
                    System.out.println("The total cost of using the Vartec service for the month is"
                            + String.format("%.2f", callV));
                } else {
                    if (service.charAt(0) == 'E') {
                        System.out.print("\nPlease enter the total number of calls made in the month:   ");
                        monthE = input.nextFloat();

                        System.out.print("\nPlease enter the number of minutes spent calling the USA:   ");
                        usaE = input.nextFloat();
                        System.out.print("\nPlease enter the number of minutes spent calling the Australia:   ");
                        ausE = input.nextFloat();
                        System.out.print("\nPlease enter the number of minutes spent calling the Russia:   ");
                        rusE = input.nextFloat();
                        callE = ((usaE * 0.19f) + (ausE * 0.85f) + (rusE * 0.92f));
                        System.out.println("The total cost of using the Vartec service for the month is"
                                + String.format("%.2f", callE));
                    }
                }
            }
    }

4 个答案:

答案 0 :(得分:1)

简单方法

char ch = service.charAt(0);

if (ch == 'E') {
   // do E
} else if (ch == 'V') {
    // do V
} else {
    // do neither E nor V - that is, BYE
}

进阶(更易于理解,提供更多选项):

char ch = service.charAt(0);

switch (ch) {
    case 'E':
    case 'e':  // also handle lowercase
        // do E
        break;
    case 'V':
    case 'v':
        // do V
        break;
    // more options if needed
    default:
        // do neither of the above
        break;
}

您的问题正在做类似

的操作
if (ch != 'E') {
    // something else
    if (ch == 'E') {
        // will never enter here
     }
}

注意:您还可以使用带字符串的开关(除非使用非常老的Java版本):

// changed to uppercase, we don't mind the case of input
switch (service.toUpperCase()) {
    case "VARTEC":
        ....
    case "EIRCOM":
        ...
    default:
        // I don't understand the user!
        ...
}

答案 1 :(得分:0)

我假设您是在以下情况下引用

if (service.charAt(0)=='E')

如果是的话,这永远是不正确的,因为它嵌套在其中

if (service.charAt(0)!='E')

答案 2 :(得分:0)

根本不需要在代码中嵌套if语句。

简单

if(service.charAt(0)=='V') {
     // First char is V
} else if (service.charAt(0)=='E') {
     // First char is E
} else {
     // First char is neither V nor E
}

就可以了。

答案 3 :(得分:0)

因此,switchelse-if方法将可行,并且易于调试和读取。但是要知道所问问题中的问题是: if (service.charAt(0) != 'E')没有匹配的else

因此,假设案例涉及条件if (service.charAt(0) != 'E')

案例1 。当您输入V时,上述条件变为true,并且流程进入此if,执行流程如预期的那样V

案例2 。当您输入E时,上述条件变为假,因为输入确实为E-因此流程不会在此if中进行,并且为E编写的代码永远不会已执行(由于它没有写在if中,因此流程不会进行),并且由于我们没有任何else部分,因此程序也会终止。

因此,如果您倾向于仅使用if而不进行切换,那么要更正实现,您应该将

替换为if (service.charAt(0) != 'E')
if (service.charAt(0) != 'E' && service.charAt(0) != 'V'){
            System.out.println("Thank you for your time...good bye.");
        } else {
             ... rest of the code as usual  
        }