gpt4 book ai didi

java - if-else 语句和运算符的问题

转载 作者:行者123 更新时间:2023-12-02 09:23:55 24 4
gpt4 key购买 nike

因此,对于我的家庭作业,我必须编写一个程序,允许用户输入他们愿意为房屋支付的最高金额作为整数。这就是问题所在;我必须使用 if-else 语句和 JOptionPane 对话框。

所以我想出了这段代码:

import javax.swing.JOptionPane;

public class housingDecision {

public static void main(String[] args) {
//listed things that need to be identified
int houseMoney;
String input;
input = JOptionPane. showInputDialog("How much money do you have?");
//making the input be listed by the user
houseMoney = Integer. parseInt(input);
//set up so that if its more than the parameters, itll move on to the next if else statement
if (houseMoney >= 250000 && houseMoney <= 100000 );
{
input = "You can afford a Townhouse!";
}
if(houseMoney >= 250001 && houseMoney <= 400000);
{
input = "You can afford a Single Family House!";
}

if (houseMoney >= 400001 && houseMoney <= 800000);
{
input = "You can afford a Luxury House!";
}
if (houseMoney >= 800001);
{
input = "Wow! You can Afford a mansion!";
}
}
}

但是,当输入整数时,它不会运行。我需要改变什么才能让这不再是一个问题?

最佳答案

看起来运行得很好。话虽如此,它并没有实现您想要实现的任务。

  1. 在多个方法调用中, . 后面有空格。
  2. 如果资金不在任何范围内,您就没有理由。
  3. 您根本没有打印结果。您只需将value 设置为结果消息,然后不对其执行任何操作。
  4. 您的第一个 if 语句已损坏,值似乎已交换。

除此之外,您需要删除 if 语句后面的分号,因为它不会导致根据条件调用其 block 。

工作代码(没有对话框输入)如下所示:

public class Main {
public static void main(String[] args) {
int houseMoney;
String input;
input = System.console().readLine("How much money do you have? > ");

houseMoney = Integer.parseInt(input);

if (houseMoney <= 250000 && houseMoney >= 100000) {
input = "You can afford a Townhouse!";
} else if(houseMoney >= 250001 && houseMoney <= 400000) {
input = "You can afford a Single Family House!";
} else if (houseMoney >= 400001 && houseMoney <= 800000) {
input = "You can afford a Luxury House!";
} else if (houseMoney >= 800001) {
input = "Wow! You can Afford a mansion!";
} else {
input = "You can't afford a house!";
}

System.out.println(input);
}
}

关于java - if-else 语句和运算符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58477825/

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