简体   繁体   中英

Having trouble with java.lang.Math(i get an error stating I'm using an Int and it needs a String)

import java.util.Scanner;

import java.lang.Math;

public class TestMax {

    int minNum = 1, maxNum = 5;

    public int inputNum() {
        Scanner num = new Scanner(System.in);
        return inputNum();

    }

    public void displayNum(int userNum) {
        System.out.printf(Math.min(userNum, maxNum));
        System.out.printf(Math.max(userNum, minNum));
    }

    public static void main(String[] args) {
        TestMax input;
        input = new TestMax();
        int userNum = input.inputNum();
        input.displayNum(userNum);

    }


}

Sorry about any incorrect formatting, it's my first time posting. I'm creating a program for a school in which a user enters a number and the program then outputs the 'maxNum' or 'minNum' depending on whether the number entered is less than or greater than the min/maxNum. I think I have everything right except for when it comes to the math function. I want it to compare the userNum to the min/maxNum but its saying I'm using an int and need a string. Any help would be greatly appreciated.

You need to return a number from Scanner like this:

 public int inputNum() {
        Scanner s = new Scanner(System.in);
        int number = s.nextInt();
        return number;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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