从int输入中获取字符串

时间:2014-04-23 17:18:00

标签: 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);
}
}

4 个答案:

答案 0 :(得分:1)

您只需使用

即可
int yourValue;
String yourStringFromTheValue = yourValue+"";

答案 1 :(得分:0)

Integer.toString(int);有一个表示每种基本类型的类。它们包含工具。如果你必须把它们作为字符串抓住并转换为int:

将扫描仪从nextInt()切换为next(),然后使用Integer.parseInt(string);

答案 2 :(得分:0)

您可以执行以下操作:String str = theint +"";

或baseball.next()从Scanner获取字符串

答案 3 :(得分:0)

您可以使用:

 int val = 3 ; 
 String myStrVal = Integer.toString(val);

同样,对于双值,您可以使用:

  double dVal = 3.4;
  String myStrVal = Double.toString(dVal);
相关问题