如何在 java 中使用 space 添加 int 输入?
How can I add an int input with space in java?
我试图将输入作为整数输入,例如 space
1 2 3 4 5
而不是 12345
。我需要在它们之间添加 space,这样我才能计算和处理中位数、众数、均值和标准差。请注意,我之前尝试过 Integer.parseInt()
。
import javax.sound.midi.SysexMessage;
import java.util.Arrays;
import java.util.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Text;
public class FinalProg {
Button button;
public static void main(String[] args) {
// int[] dataSet = {1,2,3,4,345,312,756,0,-234321132,234};
// int[] dataSet = {5,3,2,5,2,5,758,345,32,231,5,5,5,2,2};
// Scanner scanner = new Scanner(System.in);
// String input = scanner.nextLine();
//
//
//
//
//
//
//
//
//
// int [] dataSet = new int[input.length()];
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine(); //Numbers inputted from user in your case
String[] list; //List to store individual numbers
list = input.split(" "); // Regex to split the String so you get each number
//Mean
int sum = 0;
for (String n : list) {
sum += Integer.parseInt(n);
}
System.out.println("The mean of the data set is " +
((double) sum / list.length));
//Median
Arrays.sort(list);
if (list.length % 2 != 0) {
System.out.println("The median of the data set is: " + Double.parseDouble(list[list.length / 2]));
} else {
System.out.println("The median of the data set is: " + Double.parseDouble(list[list.length / 2] + list[list.length / 2 - 1]) / 2.0);
}
//Mode
int maxNumber = -1;
int maxAppearances = -1;
for (int i = 0; i < list.length; i++) {
int count = 0;
for (int j = 0; j < list.length; j++) {
if (list[i] == list[j]) {
count++;
}
}
if (count > maxAppearances) {
maxNumber = Integer.parseInt(list[i]);
maxAppearances = count;
}
}
System.out.println("The mode of the data set: " + maxNumber);
//STDV
double STDVsum = 0.0, standardDeviation = 0.0;
int length = list.length;
for(String num : list) {
STDVsum += Double.parseDouble( num);
}
double mean = STDVsum/length;
for(String num: list) {
standardDeviation += Double.parseDouble(Math.pow(num - mean, 2)); // It says operator - doesn't apply...
}
System.out.println( "STDV "+ Math.sqrt(standardDeviation/length));
}
}
}
我希望对 1 2 3 4 5
这样的输入进行处理和计算,而不是采用 12345
。正如您在上面的代码中看到的,
我有一个整数数组,但在这种情况下 space 缺少输入
已编辑:(新更新)
该示例的结果显示为:
2 3 4 5 6 7 6 6
数据集的均值是4.875
数据集的中位数为:32.5
数据集的众数:2
但正确的结果是:
平均值:4.875
中位数:5.5
模式:6
另外,我无法计算STDV,因为我无法转换:
standardDeviation += Double.parseDouble(Math.pow(num - mean, 2));
您可以获取字符串形式的输入,然后将字符串转换为 int 值数组。
像这样:
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
int[] numbers = new int[input.length()];
int index = 0;
for (char c : input.toCharArray()){
numbers[index] = Character.getNumericValue(c);
index++;
}
您应该能够使用数字数组进行计算。
要做到这一点,只需向用户询问数字,将它们作为一个字符串,用 .split(" ") 拆分它们,然后将它们解析为整数,得到您的总和。
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine(); //Numbers inputted from user in your case
String[] list ; //List to store individual numbers
list = input.split(" "); // Regex to split the String so you get each number
int sum = 0; //Your sum
for(String i : list) //Iterate through Array
{
sum += Integer.parseInt(i); //Parse the string value as int to your sum
}
System.out.println(sum); // to check if everything worked fine
关于您遇到的其他问题:中位数是错误的,因为这里:
System.out.println("The median of the data set is: " + Double.parseDouble(list[list.length / 2] + list[list.length / 2 - 1]) / 2.0);
您没有考虑到 list[list.length/2] 值是一个字符串。修复了这样的问题:
float median = (Integer.parseInt(list[list.length/2]Integer.parseInt(list[(list.length/2)-1])) / 2f ;
System.out.println("The median of the data set is: " + median);
关于模式问题,您只是再次忘记了您正在处理一个字符串数组,并以错误的方式检查了 on 是否等于另一个:
if (list[i] == list[j]) {
count++;
}
如果你想检查 2 个字符串是否相等,你可以这样做
if (list[i].equals(list[j])) {
count++;
}
此外,我认为您需要在检查每个数字后重置计数,因此也请在此处添加:
if (count > maxAppearances) {
maxNumber = Integer.parseInt(list[i]);
maxAppearances = count;
}
count=0;
}
STDV issue num 在这种情况下仍然是一个字符串,因此您不能对其调用 Math.pow :
standardDeviation += Double.parseDouble(Math.pow(num - mean, 2));
通过将 num 解析为 int 来修复
for(String num: list) {
int num_to_Integer = Integer.parseInt(num);
standardDeviation += Math.pow(num_to_Integer - mean, 2); //No need to parse now
}
现在检查 STDV 是否 returns 正确结果。
我试图将输入作为整数输入,例如 space
1 2 3 4 5
而不是 12345
。我需要在它们之间添加 space,这样我才能计算和处理中位数、众数、均值和标准差。请注意,我之前尝试过 Integer.parseInt()
。
import javax.sound.midi.SysexMessage;
import java.util.Arrays;
import java.util.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Text;
public class FinalProg {
Button button;
public static void main(String[] args) {
// int[] dataSet = {1,2,3,4,345,312,756,0,-234321132,234};
// int[] dataSet = {5,3,2,5,2,5,758,345,32,231,5,5,5,2,2};
// Scanner scanner = new Scanner(System.in);
// String input = scanner.nextLine();
//
//
//
//
//
//
//
//
//
// int [] dataSet = new int[input.length()];
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine(); //Numbers inputted from user in your case
String[] list; //List to store individual numbers
list = input.split(" "); // Regex to split the String so you get each number
//Mean
int sum = 0;
for (String n : list) {
sum += Integer.parseInt(n);
}
System.out.println("The mean of the data set is " +
((double) sum / list.length));
//Median
Arrays.sort(list);
if (list.length % 2 != 0) {
System.out.println("The median of the data set is: " + Double.parseDouble(list[list.length / 2]));
} else {
System.out.println("The median of the data set is: " + Double.parseDouble(list[list.length / 2] + list[list.length / 2 - 1]) / 2.0);
}
//Mode
int maxNumber = -1;
int maxAppearances = -1;
for (int i = 0; i < list.length; i++) {
int count = 0;
for (int j = 0; j < list.length; j++) {
if (list[i] == list[j]) {
count++;
}
}
if (count > maxAppearances) {
maxNumber = Integer.parseInt(list[i]);
maxAppearances = count;
}
}
System.out.println("The mode of the data set: " + maxNumber);
//STDV
double STDVsum = 0.0, standardDeviation = 0.0;
int length = list.length;
for(String num : list) {
STDVsum += Double.parseDouble( num);
}
double mean = STDVsum/length;
for(String num: list) {
standardDeviation += Double.parseDouble(Math.pow(num - mean, 2)); // It says operator - doesn't apply...
}
System.out.println( "STDV "+ Math.sqrt(standardDeviation/length));
}
}
}
我希望对 1 2 3 4 5
这样的输入进行处理和计算,而不是采用 12345
。正如您在上面的代码中看到的,
我有一个整数数组,但在这种情况下 space 缺少输入
已编辑:(新更新) 该示例的结果显示为:
2 3 4 5 6 7 6 6 数据集的均值是4.875 数据集的中位数为:32.5 数据集的众数:2
但正确的结果是:
平均值:4.875 中位数:5.5 模式:6
另外,我无法计算STDV,因为我无法转换: standardDeviation += Double.parseDouble(Math.pow(num - mean, 2));
您可以获取字符串形式的输入,然后将字符串转换为 int 值数组。
像这样:
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
int[] numbers = new int[input.length()];
int index = 0;
for (char c : input.toCharArray()){
numbers[index] = Character.getNumericValue(c);
index++;
}
您应该能够使用数字数组进行计算。
要做到这一点,只需向用户询问数字,将它们作为一个字符串,用 .split(" ") 拆分它们,然后将它们解析为整数,得到您的总和。
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine(); //Numbers inputted from user in your case
String[] list ; //List to store individual numbers
list = input.split(" "); // Regex to split the String so you get each number
int sum = 0; //Your sum
for(String i : list) //Iterate through Array
{
sum += Integer.parseInt(i); //Parse the string value as int to your sum
}
System.out.println(sum); // to check if everything worked fine
关于您遇到的其他问题:中位数是错误的,因为这里:
System.out.println("The median of the data set is: " + Double.parseDouble(list[list.length / 2] + list[list.length / 2 - 1]) / 2.0);
您没有考虑到 list[list.length/2] 值是一个字符串。修复了这样的问题:
float median = (Integer.parseInt(list[list.length/2]Integer.parseInt(list[(list.length/2)-1])) / 2f ;
System.out.println("The median of the data set is: " + median);
关于模式问题,您只是再次忘记了您正在处理一个字符串数组,并以错误的方式检查了 on 是否等于另一个:
if (list[i] == list[j]) {
count++;
}
如果你想检查 2 个字符串是否相等,你可以这样做
if (list[i].equals(list[j])) {
count++;
}
此外,我认为您需要在检查每个数字后重置计数,因此也请在此处添加:
if (count > maxAppearances) {
maxNumber = Integer.parseInt(list[i]);
maxAppearances = count;
}
count=0;
}
STDV issue num 在这种情况下仍然是一个字符串,因此您不能对其调用 Math.pow :
standardDeviation += Double.parseDouble(Math.pow(num - mean, 2));
通过将 num 解析为 int 来修复
for(String num: list) {
int num_to_Integer = Integer.parseInt(num);
standardDeviation += Math.pow(num_to_Integer - mean, 2); //No need to parse now
}
现在检查 STDV 是否 returns 正确结果。