gpt4 book ai didi

java - 如何在java中添加带有空格的int输入?

转载 作者:行者123 更新时间:2023-12-02 00:56:22 25 4
gpt4 key购买 nike

我试图将输入作为整数,之间有空格,例如 1 2 3 4 5 而不是 12345。我需要在它们之间留有空间,以便我可以计算和处理中位数、众数、均值和标准差。请注意,我之前尝试过 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。正如您在上面的代码中看到的,我有一个整数数组,但在这种情况下缺少带空格的输入

编辑:(新更新)该示例的结果显示为:

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 - 平均值, 2));

最佳答案

要做到这一点,只需向用户询问数字,将它们作为字符串,用 .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 问题编号仍然是一个字符串,因此您无法对其调用 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 现在是否返回正确的结果。

关于java - 如何在java中添加带有空格的int输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57815133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com