简体   繁体   English

如果在Java中,如何继续进行其他操作?

[英]How do I proceed with else if in java?

I´m fairly new to Java and I need to make a code that evaluates if you are liable for a tax deduction following these 3 conditions 我对Java还是很陌生,我需要编写一个代码来评估您是否应遵循以下3个条件来承担减税责任

  • If you live more than 50 km from work 如果您的下班时间超过50公里
  • The work assignment is max 1 year for people living alone 一个人的工作任务最长为1年
  • The work assignment is max 3 years for people who live together 共同生活的人最多可以工作3年

My code compiles but it reads it out wrong like if you live less and 50 from work and is single and the assignment is one year it still reads the conditions for the ones that are not single how do I make it read oh you are single let's skip the conditions for the non single persons? 我的代码可以编译,但是它会读出错误的信息,例如,如果您的寿命少于50岁,并且单身,并且任务为一年,那么它仍会读取非单身人士的条件。我该如何读取它?跳过非单身人士的条件? or is that how it works if you have any clue. 或者,如果您有任何线索,它是如何工作的。

My code: 我的代码:

import java.util.Scanner;

public class U72C {

    public static void main (String[]args) {
        Scanner input = new Scanner(System.in);
        System.out.println("What is your distance to work?");
        int distance = input.nextInt();
        System.out.println("Do you live alone = 1 or do you live togheter with someone = 2");
        int living = input.nextInt();
        System.out.println("How long are your work assigment");
        int work = input.nextInt();
        if ( distance > 50 ) {
            System.out.println("You do not get tax dedeuction because of your distance to your work place");
        }
        else if (living == 1 && living == 2) {     
        }
        else if (work > 1) {
            System.out.println("You do not get tax deduction because you live alone and your work assigment exceeds 1 year");
        }
        else if (living == 2 ) {
        }
        else if (work > 3) {
            System.out.println("You do not get tax deduction because your work assigment exceeds 3 years and you don't live alone");
        }
        else {
            System.out.println("You get tax deduction");
        }
    }
}

Contrary to the downvote your received, I think your question is tricky, because it seems that your want fine grain feedback for each potential logical flow leading up to a possible tax deduction. 与您收到的不赞成票相反,我认为您的问题很棘手,因为您似乎希望针对每个潜在的逻辑流程都提供细粒度的反馈,从而可能导致税收减免。 This is very much a real world type question, and companies like Intuit (the maker of TurboTax) actually employ engineers who specialize in this sort of thing. 这是一个非常现实的问题,Intuit(TurboTax的制造商)之类的公司实际上聘用了专门从事此类工作的工程师。

boolean hasDeduction = false;

if (distance > 50) {
    if (living == 1) {
        if (work > 1) {
            String error = "You do not get tax deduction because you live alone " +
                           "and your work assigment exceeds 1 year.";
            System.out.println(error); 
        }
        else {
            hasDeduction = true;
        }
    }
    else if (living == 2) {
        if (work > 3) {
            String error = "You do not get tax deduction because you live together ";
                   error += "and your work assigment exceeds 3 years.";
            System.out.println(error); 
        }
        else {
            hasDeduction = true;
        }
    }
}
else {
    String error = "You do not get tax deduction because of your distance ";
           error += "to your work place.";
    System.out.println(error);
}

if (hasDeduction) {
    System.out.println("You get tax deduction");
}

Note that ideally we would have this code inside an actual method. 请注意,理想情况下,我们会将这段代码包含在实际方法中。 The error strings could be read from a resource bundle if we were worried about language compatibility. 如果我们担心语言兼容性,则可以从资源包中读取错误字符串。

import java.util.Scanner;

public class TaxCalculator {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("What are your living standard please answear the questions");
        System.out.println("What is your distance to work?");
        int distance = input.nextInt();

        System.out.println("Do you live alone = 1 or do you live togheter with someone = 2");
        int living = input.nextInt();

        System.out.println("How long are your work assigment");
        int work = input.nextInt();

        if (living == 1 && work > 1 && distance > 50) {
            System.out.println("You are eligible for TAX");
        } else if (living == 2 && work > 3 && distance > 50) {
            System.out.println("You are eligible for TAX");
        } else {
            System.out.println("Enjoy your life without TAX");
        }
    }

}

You condition for Tax eligibility is multiple condition, You need to use logical condition like && 您的税收资格条件是多个条件,您需要使用&&之类的逻辑条件

Eligibility condition is : 资格条件为:

a) User should be alone and should leave more than 50 KM and has worked for atleast 1 years a)用户应该独自一人,并且应该离开超过50公里,并且至少工作了1年

ie living ==1 && work>1 &&distance>50 即生活== 1 &&工作> 1 &&距离> 50

b) User should not be alone and should leave more than 50 KM and has worked for atleast 3 years b)用户不应独自一人,并且应离开超过50公里,并且至少工作了3年

ie living ==2 && work>3 &&distance>50 即生活== 2 &&工作> 3 &&距离> 50

else if (living == 1 && living == 2) {  

}

This condition you need to use like this 您需要像这样使用这种情况

else if (living == 1 || living == 2) {  

}

&& -> means both needed to be true. && ->表示两个都必须为真。 || -> only one of them needed to be true ->只有其中之一需要为真

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

相关问题 构造函数重载,我该如何进行? - Constructor overloading, how do i proceed? 对于 Java,如果我想做一个 if 语句以确保输入只包含一个字符,我应该如何进行? - For Java, if I want to do an if statement to make sure the input only takes in one character, how should I proceed? 我如何继续实施子菜单? Java 终端菜单 - How do I proceed to implement sub menu's? Java terminal Menu 签出与文件冲突。 我该如何进行? - Checkout conflict with files. How do I proceed? 我如何在不等待输出的情况下进行CompletableFuture - How do I proceed with CompletableFuture without waiting for output 在没有其他情况下,如何解决此Java递归? - How do I solve this Java recursion without an else case? 如何在 Java 中的 if else 语句中检测所有特殊字符? - How do I detect all special characters in an if else statement in Java? 我如何在 Java 中编写 IF-ELSE 语句 - How do i write IF-ELSE Statement in Java 如何在Android / java中的2个类别的for循环中添加if else条件 - How do I add an if else condition to a for loop with 2 categories in android/java 如何将此 if-else 语句转换为 java 中的 switch 语句? - How do I convert this if-else statement into a switch statement in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM