在处理完这段代码一段时间后,我已经遇到了意想不到的问题。选项'B'和'C'的if和else语句在输入用户输入时不会在屏幕上显示任何内容。我尝试过一些东西,但我似乎无法弄明白。
import java.util.Scanner;
public class Internet_Service_Provider
{
public static void main(String[] args)
{
double hours;
char choice;
Scanner input = new Scanner(System.in);
System.out.println ("Enter the letter of your package: ");
choice = input.nextLine().charAt(0);
System.out.println ("Enter the number of hours used: ");
hours = input.nextDouble();
double chargesa, chargesb, chargesc;
chargesa = 9.95;
chargesb = 13.95;
chargesc = 19.95;
double chargesa2, chargesb2;
chargesa2 = (hours - 10) * 2;
chargesb2 = (hours - 20);
double extrafeesA, extrafeesB;
extrafeesA = chargesa + chargesa2;
extrafeesB = chargesb + chargesb2;
if (choice == 'A')
if (hours <= 10) {
System.out.println ("Your total charges are: " + chargesa);
}
else if (choice == 'A')
if (hours > 10){
System.out.println ("your total charges are: " + extrafeesA);
}
else if (choice == 'B')
if (hours <= 20) {
System.out.println ("Your total charges are: " + chargesb);
}
else if (choice == 'B')
if (hours > 20){
System.out.println ("Your total charges are: " + extrafeesB);
}
else if (choice == 'C'){
System.out.println ("Your total charges are: " + chargesc);
}
}
}
当用户键入“A”然后键入小时数时,程序运行完美并为我提供所需的输出。如果输入“B”或“C”然后输入小时数,则屏幕上没有输出。造成这种情况的原因是什么?
CNC中 在检查响应之前我弄乱了代码并发现如果删除else并创建此代码:
if (choice == 'A')
if (hours <= 10) {
System.out.println ("Your total charges are: " + chargesa);
}
if (choice == 'A')
if (hours > 10){
System.out.println ("your total charges are: " + extrafeesA);
}
if (choice == 'B')
if (hours <= 20) {
System.out.println ("Your total charges are: " + chargesb);
}
if (choice == 'B')
if (hours > 20){
System.out.println ("Your total charges are: " + extrafeesB);
}
if (choice == 'C'){
System.out.println ("Your total charges are: " + chargesc);}
......程序运行正常。我想其他的陈述是不必要的并且引起了问题。
答案 0 :(得分:5)
if (choice == 'A'){
if (hours <= 10) {
System.out.println ("Your total charges are: " + chargesa);}
else if (choice == 'A')
if (hours > 10){
System.out.println ("your total charges are: " + extrafeesA);}
else if (choice == 'B')
if (hours <= 20) {
System.out.println ("Your total charges are: " + chargesb);}
else if (choice == 'B')
if (hours > 20){
System.out.println ("Your total charges are: " + extrafeesB);}
else if (choice == 'C'){
System.out.println ("Your total charges are: " + chargesc);}
}
}
只是添加一些适当的表格显示,除了“A”之外,你实际上并没有涵盖任何其他情况。制表使代码看起来更清晰,因此更容易找到错误。您需要正确使用大括号,因为现在您只检查选择=='A'。如果没有 - 你的代码什么都不做。
答案 1 :(得分:2)
你有if(choice == 'A')
给你带来麻烦。还要检查另一个不正确缩进的大括号。这很难读。代码应该人类可读
请参阅:
if (choice == 'A')// this is giving trouble to you
if (hours <= 10) {
.
.
此代码相当于,(增加了大括号)
if (choice == 'A'){
if (hours <= 10) {
.
.
}
所以你必须删除它。你想要做的更清楚的是这个。您可以使用switch
switch(choice){
case 'A':
System.out.println ("Your total charges are: " + (hours <=10) ?chargesa:extrafeesA);
break;
case 'B':
System.out.println ("Your total charges are: " + (hours <=10) ?chargesB:extrafeesB);
break;
case 'C':
System.out.println ("Your total charges are: " + chargesc);
break;
}
答案 2 :(得分:2)
尝试正确使用大括号:)
if (choice == 'A') {
if (hours <= 10)
System.out.println ("Your total charges are: " + chargesa);
if (hours > 10)
System.out.println ("your total charges are: " + extrafeesA);
}
else if (choice == 'B') {
if (hours <= 20)
System.out.println ("Your total charges are: " + chargesb);
if (hours > 20)
System.out.println ("Your total charges are: " + extrafeesB);
}
else if (choice == 'C')
System.out.println ("Your total charges are: " + chargesc);
答案 3 :(得分:2)
您可以简单地使用“AND”以使您的条件正常工作。
if (choice == 'A' && hours <= 10) {
System.out.println ("Your total charges are: " + chargesa);
}
else if (choice == 'A' && hours > 10) {
System.out.println ("your total charges are: " + extrafeesA);
}
else if (choice == 'B' && hours <= 20) {
System.out.println ("Your total charges are: " + chargesb);
}
else if (choice == 'B' && hours > 20) {
System.out.println ("Your total charges are: " + extrafeesB);
}
else if (choice == 'C'){
System.out.println ("Your total charges are: " + chargesc);
}
答案 4 :(得分:0)
改变这个:
if (choice == 'A')
if (hours <= 10) {
System.out.println ("Your total charges are: " + chargesa);}
到
if (hours <= 10) {
System.out.println ("Your total charges are: " + chargesa);}
答案 5 :(得分:-1)
从看着它,
if (choice == 'A')
if (hours <= 10) {
这两个没有缩进,意味着无论第一个语句的结果如何,if (hours <= 10) {
都会运行。因此,当用户键入B
,然后输入小时数(小于10
)时,第一个语句将显示false
,第二个语句将显示true
,因此运行该语句
尝试将{}
放在所有if语句周围,然后再次尝试运行它。
我可能不正确。