Java 在IF条件下挣扎

Java 在IF条件下挣扎,java,jcreator,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

我想问的问题是,我无法制作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));
                    }
                }
            }
    }
import java.util.Scanner;
公务舱服务{
公共静态void main(字符串参数[]){
字符串服务;
浮式monthV、usaV、ausV、rusV、callV;
浮子山,美国,澳大利亚,诡计,卡莱;
扫描仪输入=新扫描仪(System.in);
System.out.print(“您在通话中使用了哪种服务?:”;
service=input.nextLine();
if(service.charAt(0)!=“E”)
if(service.charAt(0)!=“V”){
System.out.println(“感谢您抽出时间……再见”);
}否则{
if(服务字符(0)='V'){
System.out.print(“\n请输入当月的呼叫总数:”);
monthV=input.nextFloat();
System.out.print(“\n请输入呼叫美国的分钟数:”;
usaV=input.nextFloat();
System.out.print(“\n请输入呼叫澳大利亚的分钟数:”;
ausV=input.nextFloat();
System.out.print(“\n请输入打电话给俄罗斯的分钟数:”;
rusV=input.nextFloat();
callV=((usaV*0.06f)+(ausV*0.08f)+(rusV*0.24f));
System.out.println(“本月使用Vartec服务的总成本为”
+格式(“%.2f”,callV));
}否则{
if(service.charAt(0)='E'){
System.out.print(“\n请输入当月的呼叫总数:”);
monthE=input.nextFloat();
System.out.print(“\n请输入呼叫美国的分钟数:”;
usaE=input.nextFloat();
System.out.print(“\n请输入呼叫澳大利亚的分钟数:”;
ausE=input.nextFloat();
System.out.print(“\n请输入打电话给俄罗斯的分钟数:”;
rusE=input.nextFloat();
调用者=((usaE*0.19f)+(ausE*0.85f)+(rusE*0.92f));
System.out.println(“本月使用Vartec服务的总成本为”
+格式(“%.2f”,callE));
}
}
}
}

我想你是指

if (service.charAt(0)=='E')
如果是,这一个永远不会为真,因为它嵌套在这一个中

if (service.charAt(0)!='E')
简单方法

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
}
更高级一点(使用更多选项更容易理解):

你的问题是做了如下事情

if (ch != 'E') {
    // something else
    if (ch == 'E') {
        // will never enter here
     }
}
注意:您还可以使用带字符串的开关(除非使用非常旧的java版本):


代码中根本不需要嵌套的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
}

将正常工作。

因此
切换
否则如果
方法可行,并且易于调试和阅读。但是要了解问题中的问题:
if(service.charAt(0)!=“E”)
没有匹配的
else

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

案例1当您输入
V
时,上述条件变为真,流程在此
if
中进行,执行流程与
V
的预期流程相同

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

因此,如果您倾向于仅使用
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  
        }

我会使用一个开关,其中有“V”和“E”的大小写,以及输入另一个字符时的默认大小写。对于每个
if
语句,都应该有
{}
。逻辑非常不清楚,目前的编写方式。实际上,在当前嵌套条件下,您最终会检查
if(service.charAt(0)=='E')
if(service.charAt(0)!='E')
块中。这永远不会是
true
。首先,为什么要这样做?它永远不会输入第二个if。您正在检查相同的事情。是的,您的逻辑是错误的。请重新考虑一下。注意:最终为每种情况创建方法,如
doVartec(扫描仪输入)
doEircom(扫描仪输入)
,因此整个结构非常复杂clearer@Arnaud你能给我几秒钟吗…[:-)只是开玩笑,谢谢
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
}
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  
        }