gpt4 book ai didi

java - 如果在 Java 中,我该如何处理 else?

转载 作者:行者123 更新时间:2023-11-30 07:51:51 25 4
gpt4 key购买 nike

我是 Java 的新手,我需要编写一个代码来评估您是否有责任按照这 3 个条件进行减税

  • 如果您住的地方离工作地点超过 50 公里
  • 独居者的工作分配最长为 1 年
  • 同居者的工作分配最长为 3 年

我的代码可以编译,但它读出错误,就像如果你活得少,工作 50 岁,并且是单例,任务是一年,它仍然读取非单例者的条件,我如何让它读取哦,你是单例让我们跳过非单例的条件吗?或者如果您有任何线索,它是如何工作的。

我的代码:

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

最佳答案

与您收到的反对票相反,我认为您的问题很棘手,因为您似乎希望针对每个潜在的逻辑流程提供精细的反馈,从而导致可能的税收减免。这是一个非常真实的问题,像 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");
}

请注意,理想情况下,我们会将这段代码放在一个实际的方法中。如果我们担心语言兼容性,可以从资源包中读取错误字符串。

关于java - 如果在 Java 中,我该如何处理 else?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46424874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com