2

在处理这段代码一段时间并接近尾声后,我遇到了一个意想不到的问题。选项“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”,然后输入小时,则屏幕上没有输出。这是什么原因造成的?

-编辑-我在检查响应之前弄乱了代码,发现如果删除 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);}

...程序运行正常。我猜其他语句是不必要的并导致了问题。

4

6 回答 6

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'。如果没有 - 您的代码不会做任何事情。

于 2013-10-02T23:23:56.563 回答
3

你有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; 
 }
于 2013-10-02T23:21:06.220 回答
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);
于 2013-10-02T23:21:32.833 回答
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);
}
于 2013-10-02T23:28:53.100 回答
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);}
于 2013-10-02T23:21:12.377 回答
-1

从看它,

if (choice == 'A')
if (hours <= 10) { 

两者没有缩进,意思if (hours <= 10) {是不管第一个语句的结果如何都会运行。因此,当用户输入B,然后输入小时(小于10)时,第一个语句读取false,第二个语句读取true,因此运行该语句。

尝试放置{}所有 if 语句并尝试再次运行它。

我可能是不正确的。

于 2013-10-02T23:20:20.387 回答