简体   繁体   English

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

[英]Problems with if-else statements and operators

So, for my homework, I have to make a program that allows the user to enter in the highest amount of money they are willing to pay for a house as a whole number.因此,对于我的家庭作业,我必须编写一个程序,允许用户输入他们愿意为一所房子支付的最高金额作为一个整数。 Here's the catch;这是问题所在; I have to use if-else statements and JOptionPane dialog boxes.我必须使用 if-else 语句和 JOptionPane 对话框。

So I came up with this code:所以我想出了这个代码:

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

However, it does not run when a whole number is input.但是,当输入整数时它不会运行。 What do I need to change so this isn't a problem anymore?我需要改变什么,所以这不再是问题了吗?

It seems to run just fine.它似乎运行得很好。 That being said, it does not achieve the task you are trying to achieve.话虽如此,它并没有实现您想要实现的任务。

  1. You have spaces after . 后面有空格 . in several of your method calls. 在您的几个方法调用中。
  2. You have no case for if the money isn't within any of the ranges.如果钱不在任何范围内,您就没有理由。
  3. You are not printing out the result at all.您根本没有打印出结果。 You're just setting value to the result message and then doing nothing with it.您只是为结果消息设置value ,然后什么也不做。
  4. Your first if statement is broken, the values seem to be swapped.您的第一个 if 语句被破坏,值似乎被交换了。

On top of this, you need to remove those semicolons after the if statements as it isn't resulting in their blocks being called on the condition.最重要的是,您需要在if语句之后删除这些分号,因为它不会导致在条件下调用它们的块。

The working code (without the Dialog input), would look like this:工作代码(没有 Dialog 输入)如下所示:

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

There are 4 issues in this code:这段代码有4个问题:

  1. An if is immediately followed by the statement to run if the clause is true.如果子句为真,则 if 后面紧跟要运行的语句。 Just a lone semicolon ( ; ) is itself a statement (the do-nothing statement).只是一个单独的分号 ( ; ) 本身就是一个语句(什么都不做的语句)。 Furthermore, in java just braces is also legal, so, in your code, ALL the code runs.此外,在 java 中,仅大括号也是合法的,因此,在您的代码中,所有代码都会运行。

  2. All you're doing is setting the input variable, and that's it.您所做的只是设置input变量,仅此而已。 Setting the input variable doesn't magically print things;设置输入变量不会神奇地打印东西; try System.out.println , for exmaple.试试System.out.println ,例如。

  3. Your first if is broken;你的第一个 if 坏了; you have a 0 too many or too few (the condition asks if the input is both larger than 250k and smaller than 100k; thats of course always false).您的 0 太多或太少(条件询问输入是否既大于 250k 又小于 100k;那当然总是错误的)。

  4. Because there is no 'else' clause, if none of the conditions hold, nothing would happen once you fix the other problems.因为没有“else”子句,如果条件都不成立,那么一旦你解决了其他问题,什么都不会发生。

Fix ALL of those things and your code will work.修复所有这些问题,您的代码将正常工作。

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

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