在 IF 条件下挣扎
Struggling on IF conditions
我想问的问题是我不能制作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));
}
}
}
}
如果
我假设你指的是这个
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
}
更高级(更多选项更容易理解):
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!
...
}
您的代码中根本不需要嵌套 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
}
会很好用。
所以 switch
和 else-if
方法可行,并且易于调试和阅读。但是要知道问题中的问题是:
if (service.charAt(0) != 'E')
没有匹配的 else
.
所以假设案例指的是条件 if (service.charAt(0) != 'E')
:
案例 1。当您输入 V
时,上述条件变为真,流程进入此 if
并且执行流程与 V
.
的预期一致
案例 2。当您输入 E
时,上述条件变为假,因为输入确实是 E
- 因此流程不会进入此 if
并且为 E
编写的代码永远不会得到已执行(因为它写在 if
中,流程不在其中),并且由于我们没有任何 else
部分,因此程序将终止。
因此,如果您倾向于仅使用 if
而不是 switch,那么为了更正实现,您应该将 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
}
我想问的问题是我不能制作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));
}
}
}
}
如果
我假设你指的是这个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
}
更高级(更多选项更容易理解):
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!
...
}
您的代码中根本不需要嵌套 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
}
会很好用。
所以 switch
和 else-if
方法可行,并且易于调试和阅读。但是要知道问题中的问题是:
if (service.charAt(0) != 'E')
没有匹配的 else
.
所以假设案例指的是条件 if (service.charAt(0) != 'E')
:
案例 1。当您输入 V
时,上述条件变为真,流程进入此 if
并且执行流程与 V
.
案例 2。当您输入 E
时,上述条件变为假,因为输入确实是 E
- 因此流程不会进入此 if
并且为 E
编写的代码永远不会得到已执行(因为它写在 if
中,流程不在其中),并且由于我们没有任何 else
部分,因此程序将终止。
因此,如果您倾向于仅使用 if
而不是 switch,那么为了更正实现,您应该将 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
}