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