java - 从 int 输入获取字符串

标签 java java.util.scanner

我需要获取字符串形式的值。我该怎么做呢?在第一组指示中,我得到了七个整数。因此,除了玩家之外,我将所有变量设置为整数,但我也想将所有值获取为字符串。

import java.util.Scanner;

public class Baseball {

public static void main(String[] args) {    
    //opens a new scanner instance
    Scanner baseball = new Scanner(System.in);

    // Allows a person to enter players name
    System.out.print("Enter the Players Name: ");
    // reads the users entry and assigns it the String "players"
    String players = baseball.nextLine();

    // Allows a person to enter the amount of at bats
    System.out.print("Enter the amount of at bats: ");
    // reads the users entry and assigns it the int "atBats"
    int atBats = baseball.nextInt();

    // Allows a person to enter home runs
    System.out.print("Enter the amount of homeruns: ");
    // reads the users entry and assigns it the int "homeruns"
    int homeruns = baseball.nextInt();

    // Allows a person to enter walks
    System.out.print("Enter the amount of walks: ");
    // reads the users entry and assigns it the int "walks"
    int walks = baseball.nextInt();

    // Allows a person to enter Doubles
    System.out.print("Enter the amount of doubles: ");
    // reads the users entry and assigns it the int "doubles"
    int doubles = baseball.nextInt();

    // Allows a person to enter Triples
    System.out.print("Enter the amount of triples: ");
    int triples = baseball.nextInt();

    // Allows a person to enter hits
    System.out.print("Enter the amount of hits: ");
    int hits = baseball.nextInt();

    baseball.close();

    //calculates singles
    int singles = (hits - homeruns - doubles - triples);

    battingAverage(hits, atBats);
    onBasePercentage(hits, atBats, walks);
    sluggingPercentage(singles, doubles, triples, homeruns, atBats);
}
//method that calculates battingAverage
public static void battingAverage(int hits, int atBats) {
    Double average = (double) (hits / atBats);
    System.out.println("The batting average is: " + average);
}
//method that calculates onBasePercentage
public static void onBasePercentage(int hits, int atBats, int walks) {
    Double basePercentage = (double) (hits + walks) / (atBats + walks);
    System.out.println("The on base percentage is: " + basePercentage);

}
//method that calculates sluggingPercentage
public static void sluggingPercentage(int singles, int doubles,int triples, int homeruns, int atBats) {
     Double slugPercentage = (double) (singles + 2 * doubles + 3 * triples + 4 * homeruns)/ atBats;
    System.out.println("The slugging percentage is: " + slugPercentage);
}
}

最佳答案

您可以简单地使用

int yourValue;
String yourStringFromTheValue = yourValue+"";

关于java - 从 int 输入获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251103/

相关文章:

java - 如何禁用 JToolbar 的拖动?

java - Scanner.hasNext 上的无限循环,从文件中读取

Java 扫描器不从文件开头开始

java - 为什么我的代码不起作用?

java - 使用 Scanner(System.in) Java 将每个单词大写

java - 扫描仪可以用来写入文件吗?

Java单向链接整数列表,编写函数返回对列表中索引为I的元素的引用

java - 将 Ctrl+C 发送到进程

java - 身份验证 token 存储建议

java - 哪个会使类文件变大?导入 java.awt.*,还是一堆或单个导入语句?