Java 公共静态布尔方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26240250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:03:50  来源:igfitidea点击:

Java public static boolean method

javamethodsboolean

提问by Rick

I want to make a little program which will ask for two numbers first, checks whether they are good or not using a method, and then, after having the last number, prints out the result of the method. (yes you win, no you lose).

我想制作一个小程序,它会首先要求两个数字,使用方法检查它们是否好,然后,在获得最后一个数字后,打印出方法的结果。(是的,你赢了,不,你输了)。

I compared this with other questions on here, but somehow I can't get it to work. I made the luckyNumbers inside the method, outside the method, even bought "Big Java" but I'm stuck on this now.

我将此与此处的其他问题进行了比较,但不知何故我无法让它发挥作用。我在方法内部和方法外部制作了luckyNumbers,甚至买了“Big Java”,但我现在坚持这个。

Help would be very appreciated!

帮助将不胜感激!

package wtf;

import Java.util.Scanner;
/**
 *
 * @author Darl
 */
public class WTF {
public static int luckyNumber;
public static int secondLuckyNumber;
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Give me a number between 0 and 100 to win a prize. ");
    luckyNumber = sc.nextInt();

    System.out.println("Give me another number between 100 and 200.");
    secondLuckyNumber = sc.nextInt();

}

public static boolean hasLuckyNumber(int luckyNumber, int min, int max){
    min = 20; //all winning tickets from 20 till 60
    max = 60;
    if (luckyNumber > min || luckyNumber < max) {
        System.out.println("Unfortunatly, no prize");
    }else{
        System.out.println("You've got a prize!");
    }
}

public static boolean hasSecondLuckyNumber(int secondLuckyNumber, int min, int max){
    min = 140; // all winning tickets from 140 till 175
    max = 175;
    if (secondLuckyNumber > min || secondLuckyNumber < max){
        System.out.println("Sad face");
    }else{
        System.out.println("We've got a winner!");
    }
    }
}

回答by user4070442

are you getting compile errors because you're not returning the boolean from your methods?

您是否因为没有从方法中返回布尔值而收到编译错误?

public static boolean hasSecondLuckyNumber(int secondLuckyNumber, int min, int max){
    min = 140; // all winning tickets from 140 till 175
    max = 175;
    if (secondLuckyNumber > min || secondLuckyNumber < max){
        System.out.println("Sad face");
        return false;
    }else{
        System.out.println("We've got a winner!");
        return true;
    }
    return false;
}

maybe? looks like you don't need the boolean, since you're using the console to write out if you win or lose though.

也许?看起来你不需要布尔值,因为你正在使用控制台写出你是赢还是输。

回答by user265732

Well first of all you don't return anything from your methods. You can either change the return value of the functions to void since you print the result inside the function, or make the printing in the main, based on the return value.

首先,你不会从你的方法中返回任何东西。您可以将函数的返回值更改为 void,因为您在函数内部打印结果,或者根据返回值在 main 中进行打印。

Second, you don't call the methods anywhere in the code, so nothing will happen.

其次,你不会在代码的任何地方调用方法,所以什么都不会发生。

*notice that there is no need in the min/max parameters if you initialize them inside the function.

*请注意,如果在函数内部初始化它们,则不需要最小/最大参数。

回答by SpaceCowboy

I think the issue you are having (amongst other things i.e. boolean methods not returning boolean values etc..) is this:

我认为您遇到的问题(其中包括布尔方法不返回布尔值等)是这样的:

(luckyNumber > min || luckyNumber < max)

should be this:

应该是这样的:

(luckyNumber > min && luckyNumber < max)

Also the same for:

也同样适用于:

secondLuckyNumber > min || secondLuckyNumber < max

Should be:

应该:

secondLuckyNumber > min && secondLuckyNumber < max

With an ||a value greater than max would return true because the first statement luckynumber > minwould return true. Replacing this with an &&will only return true if the luckynumberis more than minand less than max

随着||一个值大于最大,因为第一条语句将返回trueluckynumber > min将返回true。用 an 替换它&&只会在luckynumber大于min和小于时返回 truemax

回答by Yoel Nunez

For the Methods hasLuckyNumber(...) and hasSecondLuckyNumber(...) you have to return a boolean value, either true or false. Then inside main you have to change your implementation.

对于方法 hasLuckyNumber(...) 和 hasSecondLuckyNumber(...),您必须返回一个布尔值,true 或 false。然后在 main 中你必须改变你的实现。

public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);
  System.out.println("Give me a number between 0 and 100 to win a prize. ");
  luckyNumber = sc.nextInt();

  if(hasLuckyNumber(luckyNumber)) {
      System.out.println("Give me another number between 100 and 200.");
      secondLuckyNumber = sc.nextInt();

      if(hasSecondLuckyNumber(secondLuckyNumber)) {
        System.out.println("We've got a winner!");
      }
      else {
        System.out.println("Sad face");
      }
  }
  else {
    System.out.println("Unfortunatly, no prize");
  }

}

public static boolean hasSecondLuckyNumber(int secondLuckyNumber){
    if (secondLuckyNumber > 140 || secondLuckyNumber < 175){
        return false;
    } else{
        return true;
    }
}

public static boolean hasLuckyNumber(int luckyNumber){
    if (luckyNumber > 20 && luckyNumber < 60) {
        return true;
    }else{
        return false;
    }
}