gpt4 book ai didi

java - 我不知道为什么这段代码会自己换行

转载 作者:行者123 更新时间:2023-11-29 08:27:03 25 4
gpt4 key购买 nike

我将这段代码分为三部分;主要,DisSystem,DisIndexerUI。其目的是计算不适指数。计算系统运行完美,但我发现打印结果有问题。这是结果:

input temperature
36
input humidity
42
Discomfort Index:96.75999999999999
Very very uncomfortable
It is

我觉得应该打印“It is very very uncomfortable”,但是我打印的结果是

Very very uncomfortable    
It is

这是一个非常奇怪的情况。我想知道为什么会这样。

我当然知道,如果我这样修复它:

    System.out.println("Discomfort Index:"+ ui.input());
System.out.print("It is ");
ui.status();

它将打印“这非常非常不舒服”。但我认为这只是一个临时解决方案。为什么会断线?

public class Main {
public static void main(String[] args) {

double temp = 0;
double humidity = 0;

DisSystem dis = new DisSystem();
DisIndexerUI ui = new DisIndexerUI(dis);

System.out.println("Discomfort Index:"+ ui.input());
System.out.println("It is" + ui.status());

}
}
==================================
public class DisSystem {

public static double Formula(double temp, double humidity) {
double formula = 0.72 * (temp + humidity) + 40.6;
return formula;
}

}
=================================
import java.util.Scanner;

public class DisIndexerUI {
DisSystem disSystem;
double formula = 0;

public DisIndexerUI(DisSystem disSystem) {
this.disSystem = disSystem;
}

public double input() {
Scanner scanner = new Scanner(System.in);
System.out.println("input temperature");
double temp = Double.parseDouble(scanner.nextLine());
System.out.println("input humidity");
double humidity = Double.parseDouble(scanner.nextLine());
formula = disSystem.Formula(temp, humidity);
return formula;
}

public String status() {
String str = "";
if (formula >= 86) {
System.out.println("Very very uncomfortable");
} else if (formula >= 83) {
System.out.println("Very uncomfortable");
} else if (formula >= 80) {
System.out.println("uncomfortable");
} else if (formula >= 75) {
System.out.println("nomal");
} else if (formula >= 70) {
System.out.println("good");
} else if (formula >= 68) {
System.out.println("very good");
}
return str;

}

}

最佳答案

您只是在 status 方法中返回一个空字符串,但打印实际值。

public String status() {
if (formula >= 86) {
return "Very very uncomfortable";
} else if (formula >= 83) {
return "Very uncomfortable";
} else if (formula >= 80) {
return "uncomfortable";
} else if (formula >= 75) {
return "nomal";
} else if (formula >= 70) {
return "good";
} else if (formula >= 68) {
return "very good";
} else {
return "";
}
}

进一步检查 else 语句中的空字符串是否适合您的用例。

关于java - 我不知道为什么这段代码会自己换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51840078/

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