简体   繁体   English

需要有关带有键盘扫描器逻辑的 If-else 语句的帮助

[英]Need help on If-else statement with keyboard scanner logic

import java.util.Scanner;

public class Section3_Projects {
    public static void main (String[] args){
        Scanner keyboard = new Scanner(System.in);
        int Package;
        int Course;
        int Total;

        System.out.println("Which of the packages do you want? \t "  +"1,2, or 3?");
        Package = keyboard.nextInt();

        System.out.println("How many courses did you enroll in this month?");
        Course = keyboard.nextInt();



        if (Package == 1 && Course <= 2) {
            System.out.println("Total cost is " + "$10");
        }
            else  {
                System.out.println("Total cost is " + "$" + (10+ (6 * (Course-2))));
                //If-else 1
            }


        if (Package == 2 && Course <= 4) {
            System.out.println("Total cost is " + "$12");
        }
            else {
                System.out.println("Total cost is " + "$" +(12 + (4 * (Course-4))));
            }//If-else 2



        if(Package == 3 && Course <= 6) {
            System.out.println("Total cost is " + "$15");
        }
            else {
                System.out.println("Total cost is " + "$" +(15+(3*(Course-6))));
        }//If-else 3


    }//end main
}

Hi all, first post on this forum and hoping someone code help me with my a work problem below.大家好,首先在这个论坛上发帖,希望有人能帮助我解决下面的工作问题。 When I ran this code with the following input, I get three "Total cost" coming out:当我使用以下输入运行此代码时,我得到三个“总成本”:

Input/Output输入输出

Which of the packages do you want?   1,2, or 3?
3
How many courses did you enroll in this month?
10
Total cost is $58
Total cost is $36
Total cost is $27

I think I am missing something so that only one correct "Total cost" should be showing.我想我遗漏了一些东西,所以应该只显示一个正确的“总成本”。 Any help is appreciated.任何帮助表示赞赏。 Thanks!谢谢!

You have 3 outputs because you have 3 else cases which will always execute, I think you could try this:你有 3 个输出,因为你有 3 个总是执行的 else 案例,我想你可以试试这个:

if (Package == 1) {
    if(Course <= 2){
         System.out.println("Total cost is " + "$10");
    }else{
         System.out.println("Total cost is " + "$" + (10+ (6 * (Course-2))));
    }
}


if (Package == 2 ) {
     if(Course <= 4){
         System.out.println("Total cost is " + "$12");
     }else{
         System.out.println("Total cost is " + "$" +(12 + (4 * (Course-4))));
     }
}

if(Package == 3 ) {
    if(Course <= 6){
        System.out.println("Total cost is " + "$15");
    }else{
        System.out.println("Total cost is " + "$" +(15+(3*(Course-6))));
    }
}

The following switch statement would have the same result, but, as mentioned in the comments, it also handles the case where Package ,= {1,2:3}:下面的switch 语句会有相同的结果,但是,正如评论中提到的,它还处理Package ,= {1,2:3} 的情况:

switch(Package){
    case 1:
        if(Course <= 2){
             System.out.println("Total cost is " + "$10");
        }else{
             System.out.println("Total cost is " + "$" + (10+ (6 * (Course-2))));
        }
        break;
    case 2:
        if(Course <= 4){
             System.out.println("Total cost is " + "$12");
        }else{
             System.out.println("Total cost is " + "$" +(12 + (4 * (Course-4))));
        }
        break;
    case 3:
        if(Course <= 6){
             System.out.println("Total cost is " + "$15");
        }else{
             System.out.println("Total cost is " + "$" +(15+(3*(Course-6))));
        }
        break;
    default:
        System.out.println("Invalid package number");
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM