我想将输入值打印到方法中

时间:2017-02-04 12:22:52

标签: java treemap

我想打印"指向"进入" printMethod"方法。如"玩家名称是 迈克尔点是2000年,球员位置1"我怎样才能做到这一点?有没有什么方法可以轻松地做到这一点。我可以通过创建一个新方法来实现它,但它是如此潦草地写。

      public class Test{
        public static void main(String[] args){
        int returnPosition = calculatePosition(2000);
        printmethod("Michael" , returnPosition);

        returnPosition = calculatePosition(900);
        printmethod("Mark"  , returnPosition);

        returnPosition = calculatePosition(800);
        printmethod("Halk" , returnPosition);

        returnPosition = calculatePosition(700);
        printmethod("Albion" , returnPosition);

        returnPosition = calculatePosition(600);
        printmethod("Cristine" , returnPosition);

        returnPosition = calculatePosition(500);
        printmethod("Emma" , returnPosition);

        returnPosition = calculatePosition(400);
        printmethod("Prince" , returnPosition);

        returnPosition = calculatePosition(300);
        printmethod("Jason" , returnPosition);

        returnPosition = calculatePosition(200);
        printmethod("Tim" , returnPosition);

        returnPosition = calculatePosition(100);
        printmethod("Bob" , returnPosition);

        returnPosition = calculatePosition(50);
        printmethod("Jackson" , returnPosition);
    }
    public static void printMethod(String playerName, int position){
        System.out.println("Player name is "+playerName+  " Point was " +point+ " and player position " +position );
    }
    public static int calculatePosition(int position){
        if (position > 1000){
            return 1;
        }else if (position > 500 && position < 1000 ){
            return 2;
        }else if (position > 100 && position < 400){
            return 3;
        }else{
            return 4;
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您可以在方法中传递另一个属性,如下所示:

public static void printMethod(String playerName, int position, Integer point){
    System.out.println("Player name is " + playerName + " Point was " + point + " and player position " + position );
}

注意

不要为您的班级命名public class main {更改名称或在大写字母中设置第一个字母public class Main {,这可能会产生问题。

所以在你的main方法中你应该像这样调用你的方法:

    int returnPosition = calculatePosition(2000);
    printmethod("Michael" , returnPosition, 2000);

这将打印例如:

  

球员名称是Michael Point是2000并且球员位置是xxxx

修改

如果你不想在你的方法中传递另一个属性,你可以像你这样的属性一样存储:

public class Help {

    //attribute to put value of your point
    public static int point;

    public static void main(String args[]) throws IOException {
        int returnPosition = calculatePosition(2000);
        point = 200;
        printMethod("Michael", returnPosition);
    }

    public static void printMethod(String playerName, int position){
        System.out.println("Player name is " + playerName + " Point was " + point + " and player position " + position );
    }
}

答案 1 :(得分:0)

我会这样做

coverage_plot <- ggplot(m2, aes(x=variable, y=value, group=conf, color=conf)) +
      geom_errorbar(aes(ymin=value-ci, ymax=value+ci), width=0.1, position=pd) +
      geom_line(position=pd) + geom_point(position=pd) +
      scale_x_discrete(labels = c(1:60))

答案 2 :(得分:0)

经过深思熟虑后,我得到了我的问题解决方案并审核了您的代码。谢谢大家。

    import java.util.Scanner;
    public class Practise {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter player name: ");
    String playerName = input.nextLine();
    System.out.println("Please enter player score: ");
    int score = input.nextInt();
    int finalScore = calculateSection(score);
    displayM(playerName , finalScore , score);
}

public static void displayM(String playerName,int finalScore, int score) {
    System.out.println("Player name is " + playerName + " score was " + score+ " position is "+finalScore);
}

public static byte calculateSection(int score) {
    if (score >= 1000) {
        return 1;
    } else if (score >= 500 && score < 1000) {
        return 2;
    } else if (score >= 400 && score < 500) {
        return 3;
    } else {
        return 4;
    }

}