Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从int输入获取字符串_Java_Java.util.scanner - Fatal编程技术网

Java 从int输入获取字符串

Java 从int输入获取字符串,java,java.util.scanner,Java,Java.util.scanner,我需要得到字符串形式的值。我该怎么做?在第一组方向上,我得到了七个整数。所以我把所有的变量都设置为整数,除了player之外,但是我还想把所有的值都设置为字符串 import java.util.Scanner; public class Baseball { public static void main(String[] args) { //opens a new scanner instance Scanner baseball = new Scanner(Sy

我需要得到字符串形式的值。我该怎么做?在第一组方向上,我得到了七个整数。所以我把所有的变量都设置为整数,除了player之外,但是我还想把所有的值都设置为字符串

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);
}
}
Integer.toString(int)有一个类表示每个基元类型。它们包含工具。如果必须将它们作为字符串抓取并转换为int:


将扫描仪从
nextInt()
切换到
next()
,并使用
Integer.parseInt(字符串)

您只需执行以下操作:String str=theint+“”

或者棒球。next()要从扫描仪获取字符串,只需使用

int yourValue;
String yourStringFromTheValue = yourValue+"";
您可以使用:

 int val = 3 ; 
 String myStrVal = Integer.toString(val);
  double dVal = 3.4;
  String myStrVal = Double.toString(dVal);
类似地,对于双值,您可以使用:

 int val = 3 ; 
 String myStrVal = Integer.toString(val);
  double dVal = 3.4;
  String myStrVal = Double.toString(dVal);

不看代码,只看问题的上半部分,您可能在寻找类似
String.valueOf(someInt)
的内容。通过这种方式,您可以将输入接受为整数,但随后将其转换为字符串。只需使用
barball.next()
azurefrog-它会出错,因为我的所有变量都是整数,我必须将它们更改为字符串