简体   繁体   English

JAVA:我如何以不同于创建用户输入的方法使用用户输入?

[英]JAVA: How do i use user inputs in a different method from the one in which it was created?

I'm making a simple program that takes 2 scores from a user, does some basic sums with he scores and provides the user with their "fitness age".我正在制作一个简单的程序,它从用户那里获取 2 个分数,对他的分数进行一些基本的求和,并为用户提供他们的“健身年龄”。 The problem is, i cannot use the data from the user as it is in different method from the calculation.问题是,我不能使用来自用户的数据,因为它与计算的方法不同。 I have tried to copy what other code on this website looks like but i think i might have messed up my own code, any help?我试图复制本网站上其他代码的样子,但我想我可能弄乱了自己的代码,有什么帮助吗?

import java.util.Scanner;

class Health {

    public static void main(String[] args) {

        int in = firstScore();
        int in = secondScore();
        revealAge(in);
        System.exit(0);

    }

    public static void firstScore() {

        Scanner firstScores = new Scanner(System.in);

        System.out.println("What score did you recieve in your first exercise out of 40?");

        int score1 = firstScores.nextInt();

        return score1;

    }

    public static void secondScore() {

        Scanner secondScores = new Scanner(System.in);

        System.out.println("What score did you recieve in your second exercise out of 40?");

        int score2 = secondScores.nextInt();

        return score2;

    }

    public static void revealAge(int score1, int score2) {

        int average = (score1 + score2) / 2;

        int fitnessAge = (((average * 8) / 5) + 10);

        System.out.println("Your average score is " + average + ".");

        System.out.println("Your PC Fit age is" + fitnessAge + ".");

        return;

    }

}

You have several problems:你有几个问题:

  • Methods firstScore() and secondScore() are both defined to return nothing (that's what the void does in the method definition) - based on how you're using them, you probably want to return int instead.方法firstScore()secondScore()都被定义为不返回任何内容(这就是方法定义中的void所做的) - 根据您使用它们的方式,您可能希望返回int
  • You're trying to define in twice in your main method.你试图定义in你的两次main方法。 You need to name these variables differently.您需要以不同的方式命名这些变量。 How about int firstScore and int secondScore ? int firstScoreint secondScore怎么int secondScore
  • Calling revealAge from your main method should pass both those parameters, not just one.从您的main方法调用revealAge应该传递这两个参数,而不仅仅是一个。

Fixing these issues should help your code compile.解决这些问题应该有助于您的代码编译。 I strongly recommend stepping through some java tutorials, so that errors like this become more obvious to you in the future.我强烈建议逐步完成一些 Java 教程,以便将来您可以更明显地看到此类错误。

  • Nit: your formatting is rather wonky. Nit:你的格式很奇怪。 Look into best practices for formatting java code.查看格式化 java 代码的最佳实践。

You need to return int in both secondScore() and firstScore() methods.您需要在secondScore()firstScore()方法中返回int And you have one name( in ) for many variable declarations in your main method.并且您的main方法中有许多变量声明的名称( in )。 Made some modifications to your code as per to the problems I stated:根据我所说的问题对您的代码进行了一些修改:

import java.util.Scanner;

class Health {

    public static void main(String[] args) {

        int in1 = firstScore();
        int in2 = secondScore();
        revealAge(in1, in2);
        System.exit(0);

    }

    public static int firstScore() {

        Scanner firstScores = new Scanner(System.in);

        System.out.println("What score did you recieve in your first exercise out of 40?");

        int score1 = firstScores.nextInt();

        return score1;

    }

    public static int secondScore() {

        Scanner secondScores = new Scanner(System.in);

        System.out.println("What score did you recieve in your second exercise out of 40?");

        int score2 = secondScores.nextInt();

        return score2;

    }

    public static void revealAge(int score1, int score2) {

        int average = (score1 + score2) / 2;

        int fitnessAge = (((average * 8) / 5) + 10);

        System.out.println("Your average score is " + average + ".");

        System.out.println("Your PC Fit age is " + fitnessAge + ".");

    }

}

暂无
暂无

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

相关问题 如何在java中使用来自不同方法的变量? - How do I use variables from different method in java? 如何从 Java 中的扫描仪读取不同的输入? - How do I read different inputs from scanner in Java? 如何在另一种方法中使用已经创建的 object? - How do I use an already created object in one method in another? 根据用户输入在 Java 中创建一个框,但如何使用与其边框不同的输入替换框的内部? - Creating a box in Java from user inputs, but how do I replace the interior of the box with a different input than its borders? 如何以一种方法获取从Java中的CSV文件读取的值,并以另一种方法使用它们? - How do I take values read from a CSV file in java in one method and use them in another method? JAVA-如何使用没有参数的方法将用户输入保存到数组中? - JAVA - how do I save user inputs into array with a method that has no parameters? 如何使用 JOptionPane 使用从不同类中的一个类接收到的用户输入? - How do I use the input received from the user from one class in a different class, using JOptionPane? 在 Java 中,如何使用 Scanner Class 在没有循环语句的情况下在一行中接收多个输入? - In Java, how do do I use Scanner Class to take in multiple inputs in one line without Loop Statements? 如何从不同的方法访问以某种方法创建的对象? - How to access an object which is created in some method, from different method? 如何在不同的Java项目中使用相同的方法 - How do I use same method in different Java projects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM