Java 如果 else 语句不起作用 - 为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19148331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:30:45  来源:igfitidea点击:

If else statements not working- why?

javaif-statementstatements

提问by Dingles

After working on this code for a while and coming very close to the end, I've run into an unexpected problem. The if and else statements for choices 'B' and 'C' display nothing on the screen when they are typed in as user input. I've tried a few things but I cannot seem to figure it out.

在处理这段代码一段时间并接近尾声后,我遇到了一个意想不到的问题。如果选择'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);
        }
    }
}

When the user types 'A' and then types the hours, the program runs perfectly and gives me the desired output. If typing 'B' or 'C' and then the hours, there is no output onto the screen. What is causing this?

当用户输入“A”然后输入小时时,程序会完美运行并提供所需的输出。如果输入“B”或“C”,然后输入小时,则屏幕上没有输出。这是什么原因造成的?

-EDIT- I messed around with the code before checking the responses and discovered that if removing the else and creating this code:

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

... the program runs properly. I guess the else statements were unnecessary and caused the problem.

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

采纳答案by 3yakuya

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);}
    }
}

Just adding some proper tabulation reveals, that you do not actually cover any other case than "A". Tabulation makes the code look much clearer, and therefore it is easier to find an error. You need to use your braces properly, as for now you only check if choice == 'A'. If it does not - your code doesn't do anything.

只需添加一些适当的表格即可显示,除了“A”之外,您实际上并未涵盖任何其他情况。制表使代码看起来更清晰,因此更容易发现错误。您需要正确使用大括号,因为现在您只检查是否选择 == 'A'。如果没有 - 您的代码不会执行任何操作。

回答by SirRan

From looking at it,

从看它,

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

The two are not indented, meaning if (hours <= 10) {will run regardless of the outcome of the first statement. So when the user types in B, and then the hours (less than 10), the first statement reads false, and the second reads true, therefore running that one.

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

Try putting {}around all the if statements and try running it again.

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

I may be incorrect though.

不过我可能是不正确的。

回答by nachokk

You have if(choice == 'A')that is giving trouble you. Also check another braces that are not correct indented. It's difficult to read. Code should be readable by human beings

你有if(choice == 'A')这给你带来麻烦。还要检查另一个缩进不正确的大括号。很难阅读。代码应该是人类可读的

See:

看:

if (choice == 'A')// this is giving trouble to you
if (hours <= 10) { 
  .
  .

This code is equivalent to,(braces added)

此代码等效于,(添加大括号)

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

So you have to remove it. What you want to do to be more clear is this. You can use switch

所以你必须删除它。你想要做的更清楚的是这个。您可以使用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; 
 }

回答by Foo Bar User

change this:

改变这个:

if (choice == 'A')
if (hours <= 10) { 
  System.out.println ("Your total charges are: " + chargesa);}

to

if (hours <= 10) { 
  System.out.println ("Your total charges are: " + chargesa);}

回答by hrv

Try to use braces correctly :)

尝试正确使用大括号:)

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);

回答by eyettea

You could simply use 'AND' in order to get your conditions work correctly.

您可以简单地使用“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);
}